在POSIX sh中替换bash数组和C样式的循环 [英] Replacing bash arrays and C-style for loops in POSIX sh
本文介绍了在POSIX sh中替换bash数组和C样式的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将以下bash代码转换为纯shell脚本(sh)语言,以便它应由其他脚本语言模式(即破折号脚本)运行.
I want to convert following bash code in pure shell script (sh) language so that it should run by other script language mode i.e dash script.
arguments=("$@")
for (( i=0; i<$#; i++ )); do
case "${arguments[$i]}" in
-foo)
let "i = i + 1"
echo "${arguments[$i]}"
;;
*)
break
esac
done
上面的代码可以在bash模式下很好地运行,但是会在破折号模式下出现错误.
above code finely run in bash mode but through an error on dash mode.
2: ./orig.sh: Syntax error: "(" unexpected
现在,如果我更改第2行以摆脱错误,如下行
Now If I change line 2 to get rid the error as following line
arguments="$@"
但是现在我又遇到了关于循环的错误
But now I got another error about loop
3: ./orig.sh: Syntax error: Bad for loop variable
推荐答案
POSIX sh不支持数组或C样式的循环.
POSIX sh doesn't support arrays or C-style for loops.
i=0
while [ $(( ( i += 1 ) <= 10 )) -ne 0 ]; do
eval "val=\$$i"
case "$val" in
-foo)
i=$(( i + 1 ))
eval "val=\$$i"
echo "$val"
;;
*)
break
esac
done
这篇关于在POSIX sh中替换bash数组和C样式的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文