我正在尝试在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
?我该如何检查呢?
发布于 2013-03-17 01:49:28
这不是特定于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
https://stackoverflow.com/questions/15452505
复制相似问题