shell gurus/gurusses,
I'm really in need of help debugging/structuring my script program. I'm a complete shell script newbie
btw if anyone knows a better place to ask this question please do tell.
I am writing a shell script that can take flags and arguments.
what the script does is it counts how many words are in the file specified. but it needs to respond to flag options as well, like when specified :
-u it only counts unique words,
-i ignore case,
-c to use all non-alpha chars as word delimiters,
-m specify +/-number for wordlength
I'm using the "getopts" script to handle cmdline arguments.
how can I structure my script to handle the flags when they are specified together?
example:
testfile:
can't do this argh#!^$badscript
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 can I structure my script for the 3rd case? 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 solves a bit of the problem and pipes it along for further processing until I get the final result.
But *how* do I detect from getopts which flags have been specified together?
here is how I'm using getopts so far:
thanks for your help/suggestions!
I'm really in need of help debugging/structuring my script program. I'm a complete shell script newbie
btw if anyone knows a better place to ask this question please do tell.
I am writing a shell script that can take flags and arguments.
what the script does is it counts how many words are in the file specified. but it needs to respond to flag options as well, like when specified :
-u it only counts unique words,
-i ignore case,
-c to use all non-alpha chars as word delimiters,
-m specify +/-number for wordlength
I'm using the "getopts" script to handle cmdline arguments.
how can I structure my script to handle the flags when they are specified together?
example:
testfile:
can't do this argh#!^$badscript
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 can I structure my script for the 3rd case? 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 solves a bit of the problem and pipes it along for further processing until I get the final result.
But *how* do I detect from getopts which flags have been specified together?
here is how I'm using getopts so far:
Code:
while getopts culmf: option
do
case $option in
c) callscript1;;
u) callscript2;;
l) callscript3;;
m) m=$optionarg;;
echo "bad flag";;
esac
done
thanks for your help/suggestions!