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:
- the total amount spent on each receipt.
- a list of all the items purchased on all receipts, sorted by price.
- 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:
- The script should ignore any lines containing a #.
- 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:
- 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).
- Write a script to extract the cost column from a receipt.
- 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.
- Combine parts 1 and 2 into a script that prints out the total for
one receipt.
- Change the script to perform the first three steps
on all files matching *.rct in the directory.
- Add code to collect all items and prices from all receipts into one
file, and output the requested information in the expected format.
- Submit both receipt.sh and total.c.