我正在尝试getopts
在脚本cygwin
中使用。bash
以下是代码:
#!/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
安装?我该如何检查?