CS010 Assignment 2

Shell Scripts

Due: January 27, 5 PM

This assignment should be turned in for evaluation. To turn in the assignment, you will use the turnin command (you will submit two files):

turnin -c 010 receipt.sh
turnin -c 010 total.c

It is ok to turn in the same file more than once. This will overwrite the earlier version. You may want to do this if you discover a mistake or make an improvement after turning in your assignment.

Your job is to write a script to process receipts.  The directory /home/faculty/freund/share/cs010/assignment2 contains several starter files, including small.rct and large.rct.  The receipt files have the following form:

#
#This is a small data file
#
#
1/1/03      Turkey sandwich     $5
1/2/03      Gas                 $12
1/2/03      Groceries           $33

You will write a script that outputs the following:
  1. the total amount spent on each receipt.
  2. a list of all the items purchased on all receipts, sorted by price.
  3. the number of different item names appearing in the receipts
Here is sample output for the two receipts in that directory:

-> receipt.sh
large.txt ...
total:
617

small.txt ...
total:
50

All Items:
Coffee              $5
Turkey sandwich     $5
Lasagna             $8
Gas                 $12
Pizza               $12
Pizza               $12
Pizza               $13
Groceries           $14
Phone Bill          $17
Groceries           $33
Sneakers            $77
Plane Ticket        $459

Total Number of Unique Items:
      11


Several points to note:

  1. The script should ignore any lines containing a #.
  2. The width of the columns, position of the dollar sign, etc. are all fixed- they will not vary.

Getting started


You will need to use a number of Unix programs we have already examined: grep, sort, uniq, cut, echo, maybe others (rm, cat might be useful), as well as >, >>, and |.  For some of these programs, you will have to use options we did not cover in  class (for example, grep has an option to print all lines NOT containing a specific string).  The man pages cover all of the features you may need.  
In order to complete the script, I suggest the following steps:
  1. Start with the receipt.sh file provided- it has file permissions set up so you can execute it.  (If you do not use this file, run the command "chmod u+x name", where name is the name of the file you are using.  More on this next week).
  2. Write a script to extract the cost column from a receipt.
  3. Write a C program called total.c that reads numbers and prints out their total.  One tidbit: the scanf function returns the number of matched input.  For example, scanf("%d", &num) returns 1 if a number is successfully read, and either 0 or -1 if an error occurs or there is no more input.
  4. Combine parts 1 and 2 into a script that prints out the total for one receipt.
  5. Change the script to perform the first three steps on all files matching *.rct in the directory.
  6. Add code to collect all items and prices from all receipts into one file, and output the requested information  in the expected format.
  7. Submit both receipt.sh and total.c.