Hello,
Decided to edit the whole post as nobody was replying, and it was pretty darn big as it was. So I have to write this script for my assignment, and I am new to scripting. The problem is we have to handle command line args, process an operations file which adds supermarket items to a database file (by database I don't mean sql, it's simply a regular file), and send standard output. So far I have dealt with the cmdline args. But I am stuck on how I go about processing the file.
basically the input looks like this.
displayall
DEleTe:MARgarINe - canola:mEADowlea
findbrand:own brand
findbrand:Country LIFE
findproduct:Milk - chocolate
findit:bread
aDd:margarine - original:devondale:5
ADd:margarine - original:devondale:5
This will be converted to uppercase as I have already done using tr.
then I need to use grep or something to check the first word delimited by ':' or newline, eg. displayall is one command that can be used to show the whole database, add to add the rest of the line to database, delete to delete a line. etc
I'm not sure how to go about this, I'm guessing I could use a for loop to read each line and process line by line something like:
for i in $opfile
do
# grep the line somehow
case $i in
ADD) add [ rest of line ]
;;
DELETE) delete [ rest of line ]
;;
DISPLAYALL) displayall
;;
ETC)
;;
*) echo "operation unkown"
;;
esac
done
so naturally I would have functions for add, delete, displayall, findproduct, findbrand.
Q1. what's the best way to go about this?
Q2. is it possible to call functions from other files, or should I separate my functions by creating separate scripts and just call on those scripts?
Reason I ask #2 is having a big wall of code makes debugging hard, for someone new to scripting like me.
Thanks.
---------- Post updated 11-25-09 at 12:07 PM ---------- Previous update was 11-24-09 at 10:12 PM ----------
Thought maybe:
#eg. line is ADD:BREAD - WHITE:TIPTOP:4
for i in $input
do
grep -o /"ADD"/"DELETE"/"DISPLAYALL"/"FINDPRODUCT"/"FINDBRAND"/ $i | tempvar
case $tempvar in
ADD) grep -ov /"ADD"/"DELETE"/"DISPLAYALL"/"FINDPRODUCT"/"FINDBRAND"/ $i | add
;;
*) echo "unknown switch"
;;
esac
done
or something... would this work?
in that grep matches ADD, in current line and sends output to tempvar.
then case in tempvar is add, so then we grep the line again inverting match the rest of line being BREAD - WHITE:TIPTOP:4 and pipe to add function