【发布时间】:2014-03-04 11:28:08
【问题描述】:
几天前已经问过与此相关的问题here
但这一次条件不同,有以下使用getopts的bash脚本
#!/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
上面的脚本对除-A 选项之外的所有选项都正常工作。它有什么问题让你从下面的脚本执行步骤中知道
$ ./sample.bash -A 3 OFF
Set OFF state on 3 bulb
这是预期的输出,但是当我给出多个选项时,它的行为就会出错
$ ./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
在这里,我希望OFF 而不是-r,显然它会给出这个输出,因为这次不是$3,而是$7,但我的问题是我如何通知脚本它现在是$7 而不是$3 .
和
$ ./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
这次在-A 之后,所有选项都被丢弃,再次-A 而不是OFF
如何在-A 选项的任何序列中正确访问-A 选项之后的两个参数?
还有任何人对问题有疑问,请告诉我,坦率地说,它的任何解决方案都意味着非常简单或困难,但目前我不知道。
【问题讨论】: