Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with script structuring

Status
Not open for further replies.

ssteves

Programmer
Mar 29, 2002
2
US
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:

Code:
while getopts culmf: option
  do
    case $option in
    c) callscript1;;
    u) callscript2;;
    l) callscript3;;
    m) m=$optionarg;;
        echo &quot;bad flag&quot;;;
    esac
  done

thanks for your help/suggestions!
 
Hi Steve,

Which shell are you using and on which version of UNIX? Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
hi mike,

I'm using solaris 2.7 and switch between borne shell and bash. any hints? :)

thanks
steve
 
This example is from the getopt man page on HPUX 11.11, very similar to your code.
[tt]
set -- `getopt abo: $*`

if [ $? -ne 0 ]; then
echo $USAGE
exit 2
fi

while [ $# -gt 0 ]; do
case $1 in
-a | -b)
FLAG=$1
shift
;;
-o)
OARG=$2
shift 2
;;
--)
shift
break
;;
esac
done
[/tt]

The man page states that the following command strings will be interpreted correctly using the above code.
[tt]
cmd -aoarg file file
cmd -a -o arg file file
cmd -oarg -a file file
cmd -a -oarg -- file file
[/tt]
Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
ssteves:

Over the years, I've used getopts, but mainly for portability issues, I've abandomed it. I like to be able to do things like this:

script.ss F1=60

but maybe I'm hamfisted and it comes out

script.ss F1 =60 or script.ss F1= 60

I've developed a method for adding a little intelligence to processing command-line arguments, and I'm including a code snippet. The idea is processing the arguments by &quot;shifting&quot; thru the list.

Is it for everybody? No. I've received criticism that it's inefficient, and has too many awk calls. Perhaps, but I'm not worried about efficiency processing c-l arguments, but accuracy.

For what it's worth .....


# keyword=value demo
# handles examples as F1=60, F1 =60, F1= 60
while [ &quot;$#&quot; -gt 0 ]
do
case $1 in
F1) # spaces after keyword
shift; equ=$1
# spaces after equal sign
if [ &quot;$equ&quot; = &quot;=&quot; ]
then
shift
f1arg=$1
else # no equal sign, but maybe =value
ret_val=`echo &quot;$1&quot; | awk '{ if (beg=index($0,&quot;=&quot;) )
print 1
else
print 0 }'`
if [ ret_val -gt 0 ]
then # get rid of the equal signs
f1arg=`echo &quot;$1&quot;|tr -d &quot;=&quot; `
else
echo &quot;Illegal command-line option &quot;
exit 1;
fi
fi
shift
;;

F1*) # no spaces after key word
# string may look like KEYWORD= value
# if last character is an equal, next field is parameter
ret_val=`echo &quot;$1&quot; | awk '{ if (beg=index($0,&quot;=&quot;) )
{
len=length($0)
if (beg == len)
print 1
else
print 0
}
else print 0 }'`
if [ $ret_val -gt 0 ]
then # next field is the days to keep
shift; f1arg=$1
else # field is probably KEYWORD=value
ret_val=`echo &quot;$1&quot; | awk '{ if (beg=index($0,&quot;=&quot;) )
{ beg++ # bump beginning
len=length($0)
num=substr($0, beg, len)
print num # print string after equal sign
}
else print '\0' }'`
if ! [ -z &quot;$ret_val&quot; ]
then
f1arg=$ret_val
else
# found parameter, but no equal sign
echo &quot;Illegal command-line option &quot;
exit 1
fi

fi
shift
;;

*) echo &quot;Command-line option not defined&quot;
exit 1
;;
esac
done
if ! [ -z &quot;$f1arg&quot; ]
then # echo if set
echo $f1arg
fi

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top