[英]Bash scripting - nested variables
我目前遇到一個棘手的問題,目前還沒有找到任何解決方案。
我寫了這樣的腳本:
#!/bin/sh
x=1
while [ "$x" -le $# ]; do
echo "$x"'. Argument is: ' "\$$x"
x="$(( $x + 1 ))"
done
我建議外殼程序在擴展變量以訪問位置x上的參數之后將對表達式“ \\ $$ x”求值,但輸出為:
1. Argument is: $1
請幫忙。 提前謝謝。
這是解決方法
$ cat a.sh
#!/bin/sh
x=1
while [ "$x" -le $# ]; do
echo "$x"'. Argument is: ' "${!x}" # If you need indirect expansion, use ${!var} is easier way.
x="$(( $x + 1 ))"
done
測試結果
$ sh a.sh a b c
1. Argument is: a
2. Argument is: b
3. Argument is: c
此代碼應工作:
#!/bin/sh
x=0
args=($@)
while [ "$x" -lt $# ]; do
echo "$x"'. Argument is: ' "${args[${x}]}"
x="$(( $x + 1 ))"
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.