1

I am working on a project to calculate my overtime at work with a shell script. I have two inputs and want to find if my number is over 800 = 8 hours; if it is bigger, then it has to print out the result to my text file. It has to print out my difference.

if [ $var1 -gt 800 ]; then
`expr $var1-800`
echo Overtime: "" >> $path 

and then I'm lost because I don't how to print out the result of my calculation.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
peter
  • 17
  • 4

2 Answers2

1

Try this using modern bash (don't use backticks or expr) :

if ((var1 > 800)); then
    overtime=$((var1 - 800)) # variable assignation with the arithmetic 
    echo "Overtime: $overtime"
fi

Or simply :

if ((var1 > 800)); then
    overtime="Overtime: $((var1 - 800))" # concatenation of string + arithmetic
fi

Check bash arithmetic

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
0
if (( var1 > 800 )); then
    printf 'Overtime: %d\n' "$(( var1 - 800 ))" >>"$path"
fi

expr and backticks are antiquated, and you don't even need a command substitution here.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936