我有一個名為“test”的txt檔案
這是檔案“test”的內容
-----------------------
Week 1 | Year
01 Jan Monday 1
02 Jan Tuesday 2
03 Jan Wednesday 3
04 Jan Thursday 4
05 Jan Friday 5
06 Jan Saturday 6
07 Jan Sunday 7
Total: $597.95
GrandTotal: $
我想做的就是tail -n 1 test展示這條線
GrandTotal: $
這就是我直接在 Bash 中撰寫它時得到的輸出,但是當我在腳本中做同樣的事情時,它反而給了我錯誤的輸出。
echo $(tail -n 1 ~/test ) 這是我在腳本中寫的,我從中得到的輸出是這樣的
07 Jan Sunday 7
我制作了一個不同的腳本只是為了測驗 tail 命令,它按預期作業,所以我的腳本一定是做錯了什么。但我不能為我的生活看到它。
這是我的整個腳本,我真的不知道我能做什么,但希望有人能發現它
#!/bin/sh
# Dependencies: calc
# Config
WeekdayRate="19.54"
WeekendRate="23.45"
Output=~/test
day=(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)
month=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
read -p "Week: " week
read -p "First date: " firstdate
read -p "Month number: " MonthNumber
read -p "Type 1 if Leap Year: " lp
read -p "Year: " year
read -p "Input hours: " hours
# Ensures Month Number by user is seen as an integer
declare -i j=$MonthNumber
# If no input is given, then the leap year variable defaults to no leap year (0)
if [[ -z $lp ]]; then
lp=0
fi
# This for-loop assigns the hour to N[i] array
for (( i=1; i<8; i )) ; do
# awk -v j=$1, this creates a new variable "j" which awk recognizes, because it cant recognise "i"
N[i]=$(echo $hours | awk -v j=$i {'print $j'})
# If F is the input then make it equal 0 since its a free day
if (( N[i] == "F" )); then
N[i]=0
fi 2>/dev/null # This is hear so it doesn't give an error for floating point numbers
done
echo "
-----------------------
Week $week | Year $year" >> $Output
# How many days there are in each month
case $j in
"1") fday=31;;
"2") fday=28;;
"3") fday=31;;
"4") fday=30;;
"5") fday=31;;
"6") fday=30;;
"7") fday=31;;
"8") fday=31;;
"9") fday=30;;
"10") fday=31;;
"11") fday=30;;
"12") fday=31;;
esac
# If its a leap year and is the month Feb, then the max days is 29 days instead of 28
if (( $lp == 1 && $j == 2 )); then
fday=29
fi
# Ensures it's processed as an int
declare -i firstday=$firstdate
# Prints out the days, month, worded day, and the hours each day
for (( c=1; c<8; c ))
do
echo "$firstday ${month[j-1]} ${day[c-1]} ${N[c]}" | sed 's/\<0\>/FREE DAY/' | sed 's/\<[0-9]\>/0&/' >> $Output
firstday =1
# If its past the final day of month, then go to next month and rest the days
if (( $firstday > $fday )); then
j =1
firstday=1
# If past december, reset to januaray
if (( $j >= 12 )); then
j=1
fi
fi
done
total=$(echo "((${N[1]} ${N[2]} ${N[3]} ${N[4]} ${N[5]})*$WeekdayRate) ((${N[6]} ${N[7]})*$WeekendRate)" | bc)
# The get value of grandtotal from the previous week
previousgrandtotal=$( tail -n 1 $Output | awk '{print $2}' )
echo $( tail -n 1 ~/test )
if [[ -z $previousgrandtotal ]]; then
previousgrandtotal=0
fi
grandtotal=$( echo "($total $previousgrandtotal)" | bc )
echo "
Total: $ $total
GrandTotal: $ $grandtotal" | sed 's/$ /$/' >> $Output
# Display the newly calculated week
tail -n 13 $Output
uj5u.com熱心網友回復:
因為您在 tail 命令開始之前寫入檔案。
echo "$firstday ${month[j-1]} ${day[c-1]} ${N[c]}" | sed 's/\<0\>/FREE DAY/' | sed 's/\<[0-9]\>/0&/' >> $Output
您可以簡單地在腳本開頭宣告和分配previousgrandtotal 變數,也可以搜索grandtotal:
previousgrandtotal=$( grep 'GrandTotal' $Output | awk '{print $2}' )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420214.html
標籤:
