Help edit simple payroll script?

I'm trying to write a simple script to figure pay with overtime...I got the first part to work, but I can't seem to get the second if statement's syntax right...:confused:I want it to take the 40 hours times 10 dollars, but then i want whatever is left over (like 7 of 47 hours) and take that times 15 dollars and then add the two figures...here's what I've got:

#!/bin/bash
# Script for figuring pay with overtime
# 5/28/08
hours=$1
normal=10
over=15
ntimepay=400
time=40
if [ $hours -le 40 ]
then
        pay=$[$hours*$normal]
        else
                if [ $hours -gt 40 ]
                then
                pay=$[expr $ntimepay + [$hours-$time]*$over]
                fi
fi
echo "Total pay is $pay"
echo "done"

Any help would be greatly appreciated! Thanks!:confused:

I'd suggest you use modulo arithmatic here via expr:

...
leftover=`expr $hours % $time`
basepay=`expr $hours - $leftover '*' $normal`
overpay=`expr $leftover '*' $over
pay=`expr $basepay + $overpay`
...

(Untested)

Tested :slight_smile:

#!/bin/sh
test $1 -gt 40 && echo $(( $1 % 40 * 15 + 40 * 10 )) || echo $(( $1 * 10 ))

Does $(()) work in sh?

You can try :slight_smile:

Sounds like a Linux thing. /bin/sh -> /bin/bash.

Then bash pretends it's sh, but it doesn't do a great job.

#!/bin/bash
# Script for figuring pay with overtime
# 5/28/08
hours=$1
normal=10
over=15
ntimepay=400
time=40
if [ $hours -le 40 ]
then
pay=`expr $hours \* $normal`
else
xtime=`expr $hours - $time`
pay=`expr $ntimepay + $xtime \* $over`
fi
echo "Total pay is $pay"
echo "done"