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.

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

  • Hi, sorry think my explaining was wrong.. I type my checkin: 700 = 7:00 am then checkout: 1800 = 6:00pm Then I gotta find the difference from 1800-700 if over 800 it gotta print out the result – peter Feb 22 '18 at 13:53
  • 1
    And so ? Can't you just adapt the command ? You have all what you need. It's simple arithmetic. Doesn't this snippet get clear ? – Gilles Quenot Feb 22 '18 at 13:56
  • I'll try as I wrote im way to new to this... to make a project like this – peter Feb 22 '18 at 13:58
  • I do, im just new to programming with arithmetic and how its done in scripts – peter Feb 22 '18 at 14:00
  • Added comments in my snippet – Gilles Quenot Feb 22 '18 at 16:28
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.