我在尝试为循环中的变量赋值并尝试使用korn shell在循环外打印时遇到问题。我想在我的脚本的后面部分使用该变量。所以我试图通过打印动态变量的值进行测试。我刚从阵列中分配给它。
#!/usr/bin/ksh
clear
BINPATH=/usr/bin
SVR_LIST=servers_list
set -A SERVERS `cat $SVR_LIST`
typeset -i i=0
Sn=${#SERVERS[@]}
#echo "Number of servers in an array are .................." $Sn
while [ $i -lt ${#SERVERS[@]} ] ; do
#print ${SERVERS[$i]}
typeset -l s${i}=${SERVERS[$i]}
#eval echo "Value of Variable is" \${s$i}
#s=\${s$i}
(( i=i+1 ))
done
s=\${s$i}
eval echo "value of s is " $s
s=eval \${s0}
APPSERVER1=$s
echo $APPSERVER1
s=eval \${s1}
APPSERVER2=$s
echo $APPSERVER2
I am getting following error.
value of s is
./variableTest.sh[21]: ${s0}:not found
${s4}
./variableTest.sh[24]: ${s1}:not found
${s4}
答案 0 :(得分:1)
这是工作代码......
#!/bin/ksh
clear
#BINPATH=/usr/bin
SVR_LIST=test
set -A SERVERS `cat $SVR_LIST`
typeset -i i=0
Sn=${#SERVERS[@]}
#echo "Number of servers in an array are .................." $Sn
while [ $i -lt ${#SERVERS[@]} ] ; do
#print ${SERVERS[$i]}
typeset -l s${i}=${SERVERS[$i]}
eval echo "Value of Variable is" \${s$i}
#s=\${s$i}
(( i=i+1 ))
done
#NOTE THIS LINE i value is i+1 here..
#so if you had last variable as s10=abc you are using s11 outside the loop in follwoing two lines..now its s10
let i=i-1
s=\${s$i}
eval echo "value of s is " $s
#CHANGES HERE
s=$s0
APPSERVER1=$s
echo $APPSERVER1
s=$s1
APPSERVER2=$s
echo $APPSERVER2
...输出
Value of Variable is "credit":
Value of Variable is "wic":
Value of Variable is "wiccash":
Value of Variable is "sfmnp":
Value of Variable is "snap":
Value of Variable is "baked goods":
Value of Variable is "baked goods":
"credit":
"wic":