Bash Shell:coomandline参数上的两个循环
在shell脚本中,我使用shift命令在位置参数上循环。在循环之后,我想重置并在参数上启动另一个循环。可以回到起点吗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=("
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,因为它只处理短选项。有关如何执行此操作的详细信息,请参阅