Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i just learned that when you use getopts, the argument after an option is stored in $OPTARG variable.
how about if i have more than 1 arguments? if i'm not mistaken, $OPTARG gets 1 only. how can i get the other arguments?

Correct, only one argument at a time is stored in $OPTARG. getopts needs to be ran within a loop, and each execution of getopts will process just one option:
#!/bin/ksh
optiona=n
optionb=n
optionc=nwhile getopts d:u:abc opt
do
print "opt=$opt OPTARG=$OPTARG"case $opt in
d) database=$OPTARG;;
u) username=$OPTARG;;
a) optiona=y;;
b) optionb=y;;
c) optionc=y;;
*) print "Valid syntax is ..."
exit;;
esacdone
shift $((OPTIND -1))
print "\nProcessing options are:"
print "database=$database"
print "username=$username"
print "option a: $optiona"
print "option b: $optionb"
print "option c: $optionc"
print "files to be processed: $*"exit 0
And following is a sample execution. Note that after shifting the parameters out of the way, this leaves only the filenames on the command line:
myscript -dPROD1 -uJBoothe -ac file1 file2 file3
opt=d OPTARG=PROD1
opt=u OPTARG=JBoothe
opt=a OPTARG=
opt=c OPTARG=Processing options are:
database=PROD1
username=JBoothe
option a: y
option b: n
option c: y
files to be processed: file1 file2 file3

![]() |
comparing time... please ...
|
such .sed file?
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |