getopts

I have a script that facillitates NDM (Connect::\Direct) transfer to remote hosts. This script uses getopts to parse through the parameters passed to it and to set appropriate variables based upon what was passed in.

Kickoff="mv $PATH/$FILE1 $PATH/$FILE2"

ndm_shell.ksh -p $Node -s $Source -d $Destination -k $Kickoff

My issue arrises with the Kickoff variable. When I pass this to my ndm_shell getopts sets the value of the host script variable to "mv "

My question: Is there a way to pass a single variable that resolves to multiple arguments to getopts so that my internal variable contains the entire string?

Have you tried quotes?

ndm_shell.ksh -p $Node -s $Source -d $Destination -k "$Kickoff"

instead of
ndm_shell.ksh -p $Node -s $Source -d $Destination -k $Kickoff

interesting behavior
if I pass the variable in quotes, -k"${KICKOFF}" then getopts will asign the variable inside of my case statement as follows:

while getopts p:n:s:d:l:b:f:c:k:u:w:g:h:i: opt
do
case $opt in
k)
KICKOFF_PROG=$OPTARG
;;
esac
done

echo "Kickoff Program variable is set to $KICKOFF_PROG"

The echo above produces the following result:
Kickoff Program variable is set to ${KICKOFF}

but....
when I actually use the variable $KICKOFF_PROG it will resolve to the string I passed....interesting behavior.

Why doesnt the echo statement expand the variable?

The shell expanded variables once during the processing of the echo statement. It does not then loop to see if any new variable references were created.

If you want to expand a variable inside a variable, you need to use "eval echo".

As for your original question, you have multiple variables inside variables and it's not obvious to me when you want them expanded. One intrepretation would suggest that:
Kickoff="\"mv $PATH/$FILE1 $PATH/${FILE2}\""
might do the trick.

1 Like