我正在研究“Beginning Linux Programming 4th ed”一书,第 2 章是关于 shell 编程的。第 53 页上的示例给我留下了深刻的印象,并尝试开发一个脚本来显示更多内容。这是我的代码:
enter code here
#!/bin/bash
var1=10
var2=20
var3=30
var4=40
for i in 1 2 3 4 # This works as intended!
do
x=var$i
y=$(($x))
echo $x = $y # But we can avoid declaring extra parameters x and y, see next line
printf " %s \n" "var$i = $(($x))"
done
for j in 1 2 3 4 #This has problems!
do
psword=PS$j
#eval psval='$'PS$i # Produces the same output as the next line
eval psval='$'$psword
echo '$'$psword = $psval
#echo "\$$psword = $psval" #The same as previous line
#echo $(eval '$'PS${i}) #Futile attempts
#echo PS$i = $(($PS${i}))
#echo PS$i = $(($PS{i}))
done
#I can not make it work as I want : the output I expect is
#PS1 = \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
#PS2 = >
#PS3 =
#PS4 = +
如何获得预期的输出?当我按原样运行它时,我只会得到
PS1 =
PS2 =
PS3 =
PS4 = +
PS1 和 PS2 发生了什么?为什么我没有得到与我得到的相同的价值
echo $PS1
echo $PS2
echo $PS3
echo $PS4
因为那是我想要得到的。