Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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 Shell:coomandline参数上的两个循环_Bash_Shell - Fatal编程技术网

Bash Shell:coomandline参数上的两个循环

Bash Shell:coomandline参数上的两个循环,bash,shell,Bash,Shell,在shell脚本中,我使用shift命令在位置参数上循环。在循环之后,我想重置并在参数上启动另一个循环。可以回到起点吗 while [ $# -gt 0 ]; do case "$1" in "--bla") doing sth shift 2 ;; *) shift 1 ;; esac done 可以将参数保存在临时数组中。然后从中恢复位置参数 args=("

在shell脚本中,我使用shift命令在位置参数上循环。在循环之后,我想重置并在参数上启动另一个循环。可以回到起点吗

while [ $# -gt 0 ]; do
  case "$1" in
    "--bla")
      doing sth
      shift 2
      ;;
    *)
      shift 1
      ;;
  esac
done

可以将参数保存在临时数组中。然后从中恢复位置参数

args=("$@")  # save

while .....
done

set -- "${args[@]}"  # restore
如果需要处理两次参数,请不要使用shift。使用for循环,两次:

for arg in "$@"
do
    …
done

如果需要处理参数选项,请考虑使用GOPT的GNU版本,而不是BASH内置的GETopts,因为它只处理短选项。有关如何执行此操作的详细信息,请参阅