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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help - Bug: A script to compile two types of data files into two temporary files
# 1  
Old 12-17-2009
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.

Code:
***************************
-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$


Last edited by Scott; 12-17-2009 at 06:17 PM.. Reason: Please use code tags
# 2  
Old 12-17-2009
Remove all $$ and replace with $. That's definitely causing some problems.

This command:
Code:
cut -d# -f3

needs to be:
Code:
cut -d\# -f3

Fix that, run again and see where we are.
# 3  
Old 12-17-2009
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....... Smilie

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

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$


Last edited by Scott; 12-17-2009 at 06:18 PM.. Reason: Please use code tags
# 4  
Old 12-21-2009
we need to run it with the command:

Code:
bash -xv file_nm 2>&1 | tee log

and examine the log.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help with exit, grep, temporary files, awk

1. The problem statement, all variables and given/known data: I do not understand how/why the following code is used. Please do not simply refer me to the man pages since I have already reviewed them extensively. Thank you. exit 2 , exit 3, exit 0 I understand the basics of why the exit... (5 Replies)
Discussion started by: BartleDoo
5 Replies

2. AIX

Hidden temporary files in AIX

Hi, Some porocess is creating hidden temporary files in /tmp directory. And they are not getting deleted. System is going out of disk space after some days. The temp files are getting created like .<user name><pid>. I have checked the application code, but didnt get any clue. Does these files... (4 Replies)
Discussion started by: viswath.sen
4 Replies

3. UNIX for Advanced & Expert Users

Which time should be used for deleting temporary files?

I want to create a folder for users to put their temporary files and a folder for users to put their permanent files. For the temporary folder, I need to implement a deletion policy. I would like to know normally which time, ctime, mtime or atime, should be used to implement such deletion policy. (1 Reply)
Discussion started by: marctc
1 Replies

4. Shell Programming and Scripting

bash script to compile multiple .c files with some options

I'm trying to write a bash script and call it "compile" such that running it allows me to compile multiple files with the options "-help," "-backup," and "-clean". I've got the code for the options written, i just can't figure out how to read the input string and then translate that into option... (5 Replies)
Discussion started by: travis.batzer
5 Replies

5. Shell Programming and Scripting

Rsync temporary files

Hi, I am looking to use rsync in a very specific way, and even though I have trawled the rsync man pages I have not succeeded in seeing a way of doing the following: The temporary files created by rsync should not be created in the destination directory. (I have used --temp-dir option to... (0 Replies)
Discussion started by: LostInTheWoods
0 Replies

6. Shell Programming and Scripting

Writing files without temporary files

Hey Guys, I was wondering if someone would give me a hand with an issue I'm having, let me explain the situation: I have a file that is constantly being written to and read from with updated lines: # cat activity.file activity1 activity2 activity3 activity4 activity5 This file... (2 Replies)
Discussion started by: bashshadow1979
2 Replies

7. Shell Programming and Scripting

Temporary files and rm

Hello everyone, I am creating a temporary file in my ksh by using something file filetemp=filetemp.$$ Later on in my script I write to the file $filetemp by 'cat'ing to it. Then in the script I am doing a 'less' on the file to view it. At the end of the script I issue a rm $filetemp 2>... (4 Replies)
Discussion started by: gio001
4 Replies

8. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies
Login or Register to Ask a Question