我的脚本的命令输出与通常的输出不同

My script's output of a command is different to the usual output

我有一个名为“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:          7.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=, 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 }' )
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

因为你在 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 }' )