the getopts doesnt seem to be working in the subshell of the ksh. when I echo $@ and $* from the subshell it shows nothing. even when I am capturing the parameters from the outer shell and passing while invoking the file then I am still not getting it properly.
Remove the # at the start of all of the lines in your while loop. The # and everything following it on a line is treated as a comment; not code to be executed.
PS You also need to change:
sqltype ="$opt"
to:
sqltype="$opt"
(no space before the equals sign).
PPS You'll also want to change the first line of outerscript from:
$o=$@
to:
o="$@"
(no dollar sign in front of o and double quotes around $@ even though the latter might not make any different with the arguments you're passing in this example).
Thanks guy's. I just figured the issue. the code was within a function hence it was not capturing the values. anyway, I am getting another issue now that its not capturing second flag values. do you know why?
Its probably to do with the arguments but its been very long since I have worked on shell scripts so I am still recalling a lot of stuff.
You can remove the shift command, it's not required.
$ cat myScript
while getopts f:v:s opt; do
case "$opt" in
f) echo f has $OPTARG;;
v) echo v has $OPTARG;;
s) echo s has nothing;;
esac
done
$ ./myScript -f f_opt -s -v v_opt
f has f_opt
s has nothing
v has v_opt
You can only save one of v's arguments into "vname". Is that what you mean?
Unless you process it within the loop, you'll need to store more values, for which you can use an array:
$ cat myScript
while getopts f:v:s opt; do
case "$opt" in
f) echo f has $OPTARG;;
v) v_args[${#v_args[@]}]=$OPTARG;;
s) echo s has nothing;;
esac
done
echo ${v_args[0]}
echo ${v_args[1]}
$ ./myScript -v abc -v 123
abc
123