Home > Software design >  My script's output of a command is different to the usual output
My script's output of a command is different to the usual output

Time:01-24

I have a txt file called "test"

This is contents of the file "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:     $

I want to do tail -n 1 test to show the line

        GrandTotal:     $

and thats the output I get when directly writing it in Bash, however when I do the same thing in my script it gives me the wrong output instead.

echo $(tail -n 1 ~/test ) This is what I wrote in my script, and the output I get from it is this

07 Jan Sunday 7

I made a different script just to test the tail command and it worked as expected so I must be doing something wrong in my script. but I can not for the life of me see it.

Here is my entire script, I literally have no idea what I could be doing but hopefully someone might be able to spot it

#!/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

CodePudding user response:

Because you are writing to the file before tail command start.

    echo "$firstday ${month[j-1]} ${day[c-1]}       ${N[c]}" | sed 's/\<0\>/FREE DAY/' | sed 's/\<[0-9]\>/0&/' >> $Output

You can simply declare and assign previousgrandtotal variable in the beginning of the script or you can search for grandtotal:

previousgrandtotal=$( grep 'GrandTotal' $Output |  awk '{print $2}' )
  •  Tags:  
  • Related