Help - Bug: A script to compile two types of data files into two temporary files

Dear other forum members,

I'm writing a script for my homework, but I'm scratching all over my head and still can't figure out what I did wrong. Please help me. I just started to learn about bash scripting, and I appreciate if anyone of you can point out my errors. I thank you in advance.

***************************
-bash-3.2$ cat payinfo
#!/bin/bash
#
#This shell script compiles two types of data files into two temporary files:
#$$depts and $$payroll 
#It matches the employee IDs from the two files 
#and extracts the regular/overtime worked hours and pay rates
#to calculate each employees' total wages earned 
#from January to June 2004
#It generates an error message and excludes an employee from the calculation 
#if the ID does not exist in both files 
#It calculates the total number of employee paid and the total regular/overtime 
#paid out amount in the end
#
#usage: payinfo -d directory_with_data_files
#
if [ $# -ne 2 ]; then
   echo "payinfo:ERROR: at least teo arguments required"
   echo "usage: payinfo -d directory_with_data_files"
   exit 1
fi

if [ ! -d "$2" -o ! -r "$2" -o ! -x "$2" ]; then
   echo "payinfo:ERROR: '$2' is not a directory or does not have appropriate permissions (rx)"
   echo "usage: payinfo -d directory_with_data_files"
   exit 1
fi

dir="$2"
cat "$dir"/0[1-6][0-3][0-9]04 > $$payroll

cat "$dir"/[DE][0-9][0-9] > $$depts
cat "$dir"/[DE][0-9][0-9][0-9] >> $$depts

empid=$(cat $$payroll | sort -nu | cut -d# -f1) 
totalhrpay=0; totalotpay=0
for emp in $empid; do
   line1=$(grep "^.*:.*:$emp:" $$depts)
   line2=$(grep "^$emp#" $$payroll)
   name=$(echo $line1 | cut -d: -f1)
   if echo "$line1" | cut -d: -f3 >/dev/null; then 
      payrate=$(echo $line1 | cut -d: -f4)
      overtime=$(echo $line1 | cut -d: -f5)
      hours=$(echo $line2 | cut -d# -f2)
      totalhr=0
      for hour in $hours; do
         ((totalhr = totalhr + hour))
      done
      othours=$(echo $line2 | cut -d# -f3)
      totalot=0
      for othour in $othours; do
         ((totalot = totalot + othour))
      done
      hourpay=$(echo "scale=2; $totalhr*$payrate" | bc)
      otpay=$(echo "scale=2; $totalot*$overtime" | bc)
      emppay=$(echo "scale=2; $hourpay+$otpay" | bc) 
      echo "Employee #$emp ($name) earned $"$emppay" during the period"
      ((count=count+1))
((totalhrpay = totalhrpay + hourpay))

#      totalhrpay=$(echo "scale=2; $totalhrpay+$hourpay" | bc)
#      totalotpay=$(echo "scale=2; $totalotpay+$otpay" | bc)
   else
      echo "payinfo:ERROR: Employee $emp is not listed in the Department files" >&2
   fi
done

echo "************ TOTALS ***************"
echo "Employees paid: $count"
echo "Regular pay: $totalhrpay"
echo "Overtime pay: $totalotpay"

===========================================================================

-bash-3.2$ payinfo -d /pub/cs/gboyd/cs160b/asmt04/payroll 
Employee #53 (Stevens,Daran) earned $621.00 during the period
Employee #91 (Wilson,Fred P) earned $1032.00 during the period
Employee #181 (Bennett,Fina) earned $1958.40 during the period
Employee #200 (Doenitz,Erick) earned $45.80 during the period
Employee #333 (Haskett,Phil) earned $370.50 during the period
Employee #342 (Zulo,Giorgio) earned $228.00 during the period
Employee #435 () earned $syntax error on line 1,  during the period
Employee #584 (Pinkerton,Tom) earned $172.00 during the period
Employee #732 (Roberts,Julie) earned $478.00 during the period
Employee #773 (Doe,Annie) earned $272.30 during the period
Employee #784 (Peabody,Connie) earned $970.00 during the period
Employee #833 (Markowitz,Rob) earned $823.50 during the period
Employee #834 (Juliano,Clio) earned $480.00 during the period
Employee #982 (Banshee,Suzy) earned $538.00 during the period
Employee #1012 (Fostner,Alice) earned $178.00 during the period
Employee #1333 (Jones,Grace) earned $900.40 during the period
Employee #1784 (Adams,Gomez) earned $264.00 during the period
Employee #1818 (Cow,Holy) earned $220.00 during the period
Employee #2000 (Sanders,Colonel) earned $672.00 during the period
Employee #2834 (Anzhenila,Fred) earned $525.00 during the period
Employee #3421 (Johnson,JH) earned $319.20 during the period
Employee #3877 (Penske,Jim) earned $28.00 during the period
Employee #5584 (Hoosis,Jacob) earned $506.00 during the period
Employee #6435 (Gonzales,Carlos) earned $594.00 during the period
Employee #7322 (Charles,Master) earned $640.00 during the period
Employee #7731 (Andrews,Jamal) earned $400.00 during the period
Employee #8333 (Doe,John) earned $740.00 during the period
Employee #98221 (Helricht,Abel) earned $145.00 during the period
************ TOTALS ***************
Employees paid: 28
Regular pay: syntax error on line 1, 
Overtime pay: syntax error on line 1, 
-bash-3.2$

Remove all $$ and replace with $. That's definitely causing some problems.

This command:

cut -d# -f3

needs to be:

cut -d\# -f3

Fix that, run again and see where we are.

Hi quirkasaurus,
Thank you for your quick reply. I replace all $$ with $, and I also fix the "cut -d\# -f" line also. But I still can not figure out how to calculate the regular hour pay and the overtime pay. The output said that there is a syntax error on line 1....... :eek:

Is there anything else that I should do? Again, many thanks for looking at my code.

****** Output ******
Employees paid: 28
Regular pay: syntax error on line 1, 
Overtime pay: syntax error on line 1, 
******************



===========================================
-bash-3.2$ cat payinfo
#!/bin/bash
#
#This shell script compiles two types of data files into two temporary files:
#$depts and $payroll 
#It matches the employee IDs from the two files 
#and extracts the regular/overtime worked hours and pay rates
#to calculate each employees' total wages earned 
#from January to June 2004
#It generates an error message and excludes an employee from the calculation 
#if the ID does not exist in both files 
#It calculates the total number of employee paid and the total regular/overtime 
#paid out amount in the end
#
#usage: payinfo -d directory_with_data_files
#
if [ $# -ne 2 ]; then
   echo "payinfo:ERROR: at least teo arguments required"
   echo "usage: payinfo -d directory_with_data_files"
   exit 1
fi

if [ ! -d "$2" -o ! -r "$2" -o ! -x "$2" ]; then
   echo "payinfo:ERROR: '$2' is not a directory or does not have appropriate permissions (rx)"
   echo "usage: payinfo -d directory_with_data_files"
   exit 1
fi

dir="$2"
cat "$dir"/0[1-6][0-3][0-9]04 > $payroll

cat "$dir"/[DE][0-9][0-9] > $depts
cat "$dir"/[DE][0-9][0-9][0-9] >> $depts

empid=$(cat $payroll | sort -nu | cut -d# -f1) 
totalhrpay=0; totalotpay=0
for emp in $empid; do
   line1=$(grep "^.*:.*:$emp:" $depts)
   line2=$(grep "^$emp#" $payroll)
   name=$(echo $line1 | cut -d: -f1)
   if echo "$line1" | cut -d: -f3 >/dev/null; then 
      payrate=$(echo $line1 | cut -d: -f4)
      overtime=$(echo $line1 | cut -d: -f5)
      hours=$(echo $line2 | cut -d\# -f2)
      totalhr=0
      for hour in $hours; do
         ((totalhr = totalhr + hour))
      done
      othours=$(echo $line2 | cut -d\# -f3)
      totalot=0
      for othour in $othours; do
         ((totalot = totalot + othour))
      done
      hourpay=$(echo "scale=2; $totalhr*$payrate" | bc)
      otpay=$(echo "scale=2; $totalot*$overtime" | bc)
      emppay=$(echo "scale=2; $hourpay+$otpay" | bc) 
      echo "Employee #$emp ($name) earned $"$emppay" during the period"
      ((count=count+1))
((totalhrpay = totalhrpay + hourpay))

#      totalhrpay=$(echo "scale=2; $totalhrpay+$hourpay" | bc)
#      totalotpay=$(echo "scale=2; $totalotpay+$otpay" | bc)
   else
      echo "payinfo:ERROR: Employee $emp is not listed in the Department files" >&2
   fi
done

echo "************ TOTALS ***************"
echo "Employees paid: $count"
echo "Regular pay: $totalhrpay"
echo "Overtime pay: $totalotpay"

===========================================================================

-bash-3.2$ payinfo -d /pub/cs/gboyd/cs160b/asmt04/payroll 
Employee #53 (Stevens,Daran) earned $621.00 during the period
Employee #91 (Wilson,Fred P) earned $1032.00 during the period
Employee #181 (Bennett,Fina) earned $1958.40 during the period
Employee #200 (Doenitz,Erick) earned $45.80 during the period
Employee #333 (Haskett,Phil) earned $370.50 during the period
Employee #342 (Zulo,Giorgio) earned $228.00 during the period
Employee #435 () earned $syntax error on line 1,  during the period
Employee #584 (Pinkerton,Tom) earned $172.00 during the period
Employee #732 (Roberts,Julie) earned $478.00 during the period
Employee #773 (Doe,Annie) earned $272.30 during the period
Employee #784 (Peabody,Connie) earned $970.00 during the period
Employee #833 (Markowitz,Rob) earned $823.50 during the period
Employee #834 (Juliano,Clio) earned $480.00 during the period
Employee #982 (Banshee,Suzy) earned $538.00 during the period
Employee #1012 (Fostner,Alice) earned $178.00 during the period
Employee #1333 (Jones,Grace) earned $900.40 during the period
Employee #1784 (Adams,Gomez) earned $264.00 during the period
Employee #1818 (Cow,Holy) earned $220.00 during the period
Employee #2000 (Sanders,Colonel) earned $672.00 during the period
Employee #2834 (Anzhenila,Fred) earned $525.00 during the period
Employee #3421 (Johnson,JH) earned $319.20 during the period
Employee #3877 (Penske,Jim) earned $28.00 during the period
Employee #5584 (Hoosis,Jacob) earned $506.00 during the period
Employee #6435 (Gonzales,Carlos) earned $594.00 during the period
Employee #7322 (Charles,Master) earned $640.00 during the period
Employee #7731 (Andrews,Jamal) earned $400.00 during the period
Employee #8333 (Doe,John) earned $740.00 during the period
Employee #98221 (Helricht,Abel) earned $145.00 during the period
************ TOTALS ***************
Employees paid: 28
Regular pay: syntax error on line 1, 
Overtime pay: syntax error on line 1, 
-bash-3.2$

we need to run it with the command:

bash -xv file_nm 2>&1 | tee log

and examine the log.