2

我知道“for循环”可以用来遍历所有参数。但是对于我的代码,我更喜欢

i = 1
if [ ${$i} == xx] ; then
 xxx
fi

((iArg++))

if [ ${$i} == xx] ; then
 xxx
fi

显然,${$i} 不起作用。我该如何使它正确?

我曾经使用写 csh

$argv[$i]

现在必须使用 sh shell。感谢帮助

4

1 回答 1

5

由于这个非常困难,参数解析通常使用$1and来完成shift。一个典型的循环可能如下所示:

while [ $# -gt 0 ]; do
    case $1 in
        -a) echo "-a";    shift 1;;   # option with no argument
        -b) echo "-b $2"; shift 2;;   # option which takes argument, use $2
        -c) echo "-c";    shift 1;;
    esac
done

也就是说,间接变量名的方法是 with !,如:

${!i}
于 2013-08-08T15:06:39.267 回答