SriniV SriniV - 6 months ago 49
Bash Question

Double Quoted variables failing the script

I am having trouble while using quoted variables to call a script within a script.

#!/bin/sh

while getopts c:a: optionalargs
do
case $optionalargs in
c)copt=$OPTARG;;
a)aopt=$OPTARG;;
*)echo "Invalid arg";;
esac
done

if [ ! -z "$copt" ]
then
export CHAR_SET=$copt
fi

if [ ! -z "$aopt" ]
then
export ADDITIONAL_FLAGS=$aopt
fi

shift $((OPTIND -1))

/var/bin/script1.sh "${CHAR_SET}" "${ADDITIONAL_FLAGS}"


Results:

when using
/var/bin/script1.sh "${CHAR_SET}" "${ADDITIONAL_FLAGS}"
with quotes

./a.sh -a additional -c character
too many arguments . Use -h for help


when using
/var/bin/script1.sh ${CHAR_SET} ${ADDITIONAL_FLAGS}
without quotes

./a.sh -a additional -c character
script runs fine


Question:

Why the double quotes failing my script? Shouldn't it prevent globbing?

Answer Source

The problem was identified in the script that is being called internally. It processes the double-quoted arguments and replaces with single quotes. So, this is really not an issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download