Optargs????


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optargs????
# 8  
Old 01-23-2008
Consider the following loop.
This allows three command line options , one of which accepts an argument(b),
because "b" is followed by a ":" in the getopts arguments.
When processing option b , $OPTARG holds the argument to this option.

while getopts "ab:c" opt; do
case $opt in
a ) process option -a ;;
b ) process option -b
$OPTARG is the option's argument ;;
c ) process option -c ;;

esac
done
# 9  
Old 01-23-2008
The following while statement loops through all the options and sets them to the corresponding variable. getopts returns true while there are options to be processed. The argument string, here hw:e:u:m:d, specifies which options the script accepts. If the user specifies an option which is not in this string, it sets $optionName to ?. If the option is succeeded by a colon, the value immediately following the option is placed in the variable $OPTARG.

while getopts "hw:e:u:m:d" optionName; do
case "$optionName" in
h) printHelpAndExit 0;;
d) debug="0";;
w) whatTowatch="$OPTARG";;
e) email="$OPTARG";;
u) startAtUid="$OPTARG";;
m) maxCpuUsage="$OPTARG";;
[?]) printErrorHelpAndExit "$badOptionHelp";;
esac
done
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

optargs processing

Hello i'm writing some analyzing script and i'm giving to my program these parameters, for example: ./procinfo -r tmpfile sh -c "cat tmpfile" where -r is proccessed in getopts and it takes an argument by OPTARG.. The thing is I need to save this part to some variable: sh -c "cat tmpfile"... (4 Replies)
Discussion started by: midin
4 Replies
Login or Register to Ask a Question