I'm trying to read command line arguments through a bash scripts and am having difficulties getting desired results. I want to read in two parameters, one after -var1 and one after var2.
I am passing in these arguments
-var1 hello -var2 world
args=("$@")
x="0"
for (( i=0;i<$#; i++ ))
do
x=$(($x + 1))
echo " $i $x "
if [[ ${args[${i}]} == "-var1" ]] ; then
if [ $# > $x ] ; then
var1="${args[${i+1}]}"
fi
fi
echo $i
if [[ ${args[${i}]} == "-var2" ]] ; then
if [ $# > $x ] ; then
var2="${args[${i+1}]}"
fi
fi
done
It sets both variables, var1
and var2
, equal to hello
, rather than var1="hello"
and var2="world"
. Any thoughts? Thanks in advance