Bash 在cygwin上使用getopts时出错
我试图在Bash 在cygwin上使用getopts时出错,bash,getopts,Bash,Getopts,我试图在bash脚本中使用cygwin上的getopts。代码如下: #!/bin/bash # Sample program to deal with getopts. echo "Number of input arguments = $#"; i=0; while [ ${i} -lt 10 ]; do i=$[${i}+1]; echo ${i}; done while getopts ":a:b:c:e:" opt; do case ${opt} in a)
bash
脚本中使用cygwin
上的getopts
。代码如下:
#!/bin/bash
# Sample program to deal with getopts.
echo "Number of input arguments = $#";
i=0;
while [ ${i} -lt 10 ];
do
i=$[${i}+1];
echo ${i};
done
while getopts ":a:b:c:e:" opt;
do
case ${opt} in
a)
echo "-a was triggered with argument: ${OPTARG}";
;;
b)
echo "-b was triggered with argument: ${OPTARG}"
;;
c)
echo "-c was triggered with argument: $[OPTARG}"
;;
e)
echo "-e was triggered with argument: ${OPTARG}"
;;
?)
echo "Invalid argument: ${OPTARG}"
;;
esac
done
当我运行上述代码时,我得到以下错误:
./getOpts_sample.bash: line 37: syntax error near unexpected token `done'
./getOpts_sample.bash: line 37: `done'
我无法理解这个错误背后的原因。为什么
getopts
循环不工作,而第一个循环工作?是因为我的系统没有安装getopts
?我该如何检查它?这不是cygwin特有的;第26行出现语法错误:
echo "-c was triggered with argument: $[OPTARG}"
用{
替换[
,它就会工作
注意第11行:echo${i}
错误,请使用echo${!i}
打印第i个参数
第10行的注意:语法$[]
现在已过时;您可以使用(())
,如下所示:
((i++))
或者更好,将第8-12行替换为:
for ((i=0; i<10; i++)); do echo ${!i}; done
for((i=0;这是一个很好的答案,我希望我能多投一票!谢谢!我想我需要睡一觉..我看不清代码了。