Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在cygwin上使用getopts时出错_Bash_Getopts - Fatal编程技术网

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;这是一个很好的答案,我希望我能多投一票!谢谢!我想我需要睡一觉..我看不清代码了。