Already asked question related this few days ago here
But this time the condition different,Having following bash script using getopts
#!/bin/bash
ipaddr=""
sysip=""
msgType=""
sshTimeout=""
bulbIndex=""
bulbstate=""
while getopts ":ht:d:A:r:s:m:" OPTION
do
case $OPTION in
h)
usage $LINENO
;;
t)
let "t_count += 1"
ipaddr="${OPTARG}"
echo -e $ipaddr
;;
d)
let "d_count += 1"
echo "Not supported"
exit 0
;;
A)
let "A_count += 1"
bulbIndex="${OPTARG}" # After -A option firsr argument is bulb index and second is state off/on
bulbstate=$3
printf "Set %s state on %s bulb\n" $bulbstate $bulbIndex
;;
r)
let "r_count += 1"
sysip="${OPTARG}"
echo -e $sysip
;;
m)
let "m_count += 1" #message type 1:text 2:number 3:Text&number
msgType="${OPTARG}"
echo -e $msgType
;;
s)
let "s_count += 1"
sshTimeout="${OPTARG}"
echo -e $sshTimeout
;;
?)
echo -e "wrong command sysntax"
exit 0
;;
esac
done
Above script working fine for all options except -A
option.What is wrong with it let you know from below script execution steps
$ ./sample.bash -A 3 OFF
Set OFF state on 3 bulb
This is expected output but when i give multiple option then it behave wrong like
$ ./sample.bash -t 192.168.0.1 -r 192.169.0.33 -A 3 OFF
192.168.0.1
192.169.0.33
Set -r state on 3 bulb
Here i expect OFF
instead -r
and obviously it gives this output because this time it not $3
but it $7
but my problem is how i inform to script it's now $7
not $3
.
And
$ ./sample.bash -t 192.168.0.1 -A 3 OFF -r 192.169.0.33 -m 1
192.168.0.1
Set -A state on 3 bulb
this time after -A
all options are discarded and again -A
instead OFF
How can i correctly access both arguments after -A
option in any sequence of -A
options?
Also any one have query regarding question let me know and frankly speaking whatever solution of it means very simple or hard but currently i don't know.