read -p "Enter the new Kernel Destination: " kernel_new
n=1
while [ "$word" != "no" ]
do
read -p "Enter the Hostname: " hostname_$n
read -p "Enter the SID: " sid_$n
read -p "Enter the destination of Kernel: " kernel_old_$n
read -p "Want to add new entry please give yes or else give no: " word
n=$(( n+1 ))
done
echo "Kernel Upgrade is starting now"
while [ $n != 0 ]
do
echo "Upgrading ${hostname_[$n]} "
n=$(( n-1 ))
done执行:
我想要显示"Upgrading hostname“,在用户提示期间,将在我的第一个while循环中分配hostname。假设hostname_1 =abcdef,hostname_2=qwerty
然后我希望输出结果为
Upgrading abcdef
Upgrading qwerty请救命!
壳
while [ $n != 0 ]
do
echo "Upgrading ${hostname_[$n]} "
n=$(( n-1 ))
done我想要显示"Upgrading hostname“,在用户提示期间,将在我的第一个while循环中分配hostname。所以假设
hostname_1 =abcdef, hostname_2=qwerty然后我希望输出结果为
Upgrading abcdef
Upgrading qwerty请救命!
发布于 2019-07-13 15:06:50
这些变量在子while中的while循环中捕获,在外部不可用。但是您可以尝试使用关联数组。下面是一个简单的例子,说明它是如何工作的:
#!/bin/bash
# declare an associative array
declare -A dict
n=0
while true; do
read -r -p "Hostname: " dict['hostname_'$n]
read -r -p "retry? (Yn): " answer
[[ "$answer" == "n" ]] && break
(( n++ ))
done
# loop over all entries in reverse order
for i in $(seq $n -1 0); do
printf "%s\n" "${dict['hostname_'$i]}"
donehttps://stackoverflow.com/questions/57013824
复制相似问题