【发布时间】:2009-06-18 18:52:48
【问题描述】:
我正在尝试将脚本从 BASH 移植到 ASH (Almquist SHell),但遇到了间接引用问题。下面的函数
cmd() {
# first argument is the index to print (ie label)
arg=$1
# ditch the first argument
shift
# print the label (via indirect reference)
echo "${!arg}"
}
应该产生以下输出
cmd 1 one two three
one
cmd 2 one two three
two
cmd 3 one two three
three
这在 BASH 下按预期工作,但在 ASH(或 DASH)下运行时会生成“语法错误:替换错误”。这应该工作吗?如果没有,是否有替代使用间接引用的方法?
【问题讨论】: