The following code is executed in subscript.sh
, and subscript.sh
is run inside primaryscript.sh
#### primaryscript.sh
#! /bin/bash
#...
"$bss_path"/subscript.sh "$option"
#...
I am using this code to parse my arguments and their values:
#### subscript.sh
#! /bin/bash
while getopts "hw:l:s:b:" opt; do
case $opt in
w)
x="$OPTARG";;
l)
xx="$OPTARG";;
s)
xxx="$OPTARG";;
b)
xxxx="$OPTARG";;
h)
print_usage
exit 0;;
\?)
echo "Invalid option: -$OPTARG"
exit 1;;
esac
done
The problem appears when I call my script with multiple arguments:
./myscript -l 5.0.3-0 -s 4.0-0 -b 010
getopts
thinks that option l
have 5.0.3-0 -s 4.0-0 -b 010
as argument.
What am I doing wrong?
I tried to play around with the :
and the option, but as I understood I have to put them behind the options taking an argument right?
And getopts
naturally knows that -
is the separator for arguments.
$> echo $BASH_VERSION
$> 3.2.25(1)-release
As Cyrus pointed out in the comment, the problem was how I passed arguments.
./myscript "$options"
The correct way is :
./myscript $options
Since "$options" won't care of the spaces and take the whole string as a single argument.