shell gurus,
can you help me with this *stupid*(sorry been at it for too long) problem. I'm a complete shell script newbie :)
~school assignment as usual~
I need to write a shell script that can take flags and arguments.
what the script does is it does "text analysis", aka count how many words are in the file specified. but it needs to respond to options as well, like when specified :
-u it only counts unique words,
-i for turning all alpha chars to lowercase before processing,
-c to use all non-alpha chars as word delimiters,
-m, etc...
the prof is forcing the usage of the "getopts" script to handle cmdline arguments.
how the heck do I handle all the flag variations and make my script interpret all of them?
example:
testfile:
can't do this argh@#$@bad
help help
# unique words
$ wordcount -u testfile
5
# non-alpha word delimiters
$ wordcount -c testfile
8
# unique and non-alpha
$ wordcount -uc testfile
7
How the heck do I structure my script? Right now I'm using this hack to do non-alpha, but I'm sure it's not the right approach:
tr '0123456789' ' ' < test1 | tr '`~!@#$%^&*()_+-=' ' ' | wc | awk '{print $2}'
so, each filter does a bit of the problem and pipes it along for further processing until I get the result.
But *how* do I detect from getopts which flags have been specified together?
this is my getopts thingy:
[code]
while getopts culmf: option
do
case $option in
c) callscript1;;
u) callscript2;;
l) callscript3;;
m) m=$optionarg;;
echo "bad flag";;
esac
done
[/code]
how can I get that to handle the case where multiple flags are specified, and what should I do?(sorry eyes too tired and bleary to phrase a more objective question)
thanks for any help!