Getopts in the subshell of ksh

Hi,

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.

the below code is in the inner script

while getopts ":f:v:s" opt; do
#        case $opt in
#           "-f") shift;
#                sqltype ="$opt"
#                 echo "OPTION IS $opt"
#       ((sqltype != "c"|"h")) && { printf "%b\n" "${USAGE}";  exit 1; }
#       ;;
#   "-v") vname="$2"
#       ;;
#   "-s") dbstring="$2"
#        ;;
#   *) echo printf "%b\n" "${USAGE}"
#       exit 1
#       ;;
#
#       esac
echo $opt
shift
done

invoking from the outer script like this

$o=$@
. ./innerscript $o

Invoking the outer script like

./outerscript -f parameter

I am using ksh here. can some one please help me with this? in the above code its not getting in the while loop itself.

Many thanks!!

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).

I understand that. but my point is its not even getting into the while loop and not displaying the echo statement.

Please see the PPS in my earlier note...

Also, why are the arguments (:slight_smile: before the options (f, v, s)?

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.

while getopts "f:v:s" opt; do
echo $opt
        case $opt in
           "f") shift;
                 typeset -l sqltype=$OPTARG
       [[ $sqltype != "c" ]] &&  [[ $sqltype != "h" ]] && { printf "%b\n" "${USAGE}";  exit 1; }
       ;;
   "v") vname="$OPTARG"
       ;;
   "s") dbstring="$OPTARG"
        ;;
   *) echo printf "%b\n" "${USAGE}"
       exit 1
       ;;

        esac
done

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

I did. but still its not reading the second v flag.

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

Notice also that "s" has no arguments (f:v:s).

1 Like

Thanks Scot for all your help :slight_smile: