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
执行:
我想显示“正在升级主机名”,其中将在用户提示期间的第一个while循环中分配主机名。因此,假设hostname_1 = abcdef,hostname_2 = qwerty
然后我要输出为
Upgrading abcdef
Upgrading qwerty
请帮忙!!!!!
壳
while [ $n != 0 ]
do
echo "Upgrading ${hostname_[$n]} "
n=$(( n-1 ))
done
我想显示“正在升级主机名”,其中将在用户提示期间的第一个while循环中分配主机名。因此,假设
hostname_1 =abcdef, hostname_2=qwerty
然后我要输出为
Upgrading abcdef
Upgrading qwerty
请帮忙!!!!!
答案 0 :(得分:0)
变量在子shell的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]}"
done