我试图将脚本从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 (或破折号)下运行时会生成“语法错误:糟糕的替换”。这应该起作用吗?如果没有,有没有使用间接引用的替代方法?
发布于 2009-06-18 19:05:03
你可以试试eval
cmd() {
arg=$1
shift
eval "echo \$$arg"
}https://stackoverflow.com/questions/1014522
复制相似问题