Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linux bash shell scripting question: Write a script to compute payroll informati

ID: 3589354 • Letter: L

Question

Linux bash shell scripting question:

Write a script to compute payroll information for the people in the employees.txt file that you downloaded. Each row of this file consists of a person’s last name, hours and wages. To compute pay, use an if-then-else statement using this formula:
Hours <= 40: pay = hours * wages
Hours > 40: pay = 40 * wages + 2 * (hours – 40) * wages
We can’t use time and a half (1.5) for overtime, so we use 2. Output for each employee their pay and also output the total number of employees processed and the average pay computed.

employees.txt file: https://www.nku.edu/~foxr/CIT371/employees.txt

Explanation / Answer

fn="$1"
cnt=0
sum=0
if [ ! -e $fn ] ; then
echo "$fn is not exists ...."
exit 1
fi
echo "Employee_Name | No.of_Hours | Wages | Pay"
while read -r line
do
if [ `echo $line | wc -c` -ne 1 ] ; then   # This line checks for the fetched line was empty line or not, if the fetched line was not empty then out requriment logic follows
   name=`echo $line | cut -d" " -f1`         # retrieve the name from the variable line, which is fetched from the given input file, by extacting the 1st field of the variable line
                                             # this can be done through cut command, based on the delimiter as space and with the field option -f1
   hours=`echo $line | cut -d" " -f2`        # retrieve the no.of hours into hours variable
   wages=`echo $line | cut -d" " -f3`        # retrieve the wages
   if [ $hours -le 40 ] ; then
     pay=`expr $hours * $wages`             # calculating pay if hours <= 40
   else
     t=`expr $hours - 40`
     pay=`expr 40 * $wages + 2 * $t * $wages` # calculating the pay if hours >=40
   fi

   echo "$name | $hours | $wages | $pay"     # displaing the last name, no.of hours, wages and computed pay
   cnt=`expr $cnt + 1`                       # finding out the no. of employees processed
   sum=`expr $sum + $pay`                    # summing up all the empolyees pays, to find the average pay
fi
done < "$fn"
echo "the total number of employees processed : $cnt"
echo "the average pay computed : `expr $sum / $cnt`"


<<'COMMENT'
lenovo@lenovo-Vbox:~/chegg$ chmod 777 payroll.sh
lenovo@lenovo-Vbox:~/chegg$ ./payroll.sh employees.txt
Employee_Name | No.of_Hours | Wages | Pay
Zappa | 36 | 12 | 432
Keneally | 44 | 14 | 672
Underwood | 46 | 15 | 780
Mars | 25 | 10 | 250
Martin | 55 | 12 | 840
Preston | 15 | 16 | 240
Duke | 49 | 14 | 812
Bozzio | 40 | 10 | 400
Belew | 29 | 18 | 522
the total number of employees processed : 9
the average pay computed : 549
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./payroll.sh employees.tx
employees.tx is not exists ....
lenovo@lenovo-Vbox:~/chegg$ ./payroll.sh employes.txt
employes.txt is not exists ....
lenovo@lenovo-Vbox:~/chegg$

COMMENT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote