读取多个输入,然后将其存储在一个或多个变量中,然后在shell脚本中打印该数组/变量的值

read -p " Enter no. of users : " no_user
            if [ $no_user -gt 1 ]; then
                for ((i=1;; i++)); do
                    read -p "Enter Mob no ($i) : " $[mob$i]
                    if [ $i == $no_user ]; then break; fi
                done
            else
                read -p "Enter Mob no ($i) : " mob$i
            fi
                if [ $no_user > 1 ]; then
                    for ((i=1;; i++)); do
                        echo "Mobile no $i = " $[mob$i]
                            if [ $i == $no_user ]; then break; fi
                    done
                fi

最佳答案

在第5行和第9行将$[mob$i]mob$i更改为mob[$i]。并在第13行将$[mob$i]更改为${mob[$i]}
完整代码:

read -p " Enter no. of users : " no_user
if [ $no_user -gt 1 ]; then
for ((i=1;; i++)); do
read -p "Enter Mob no ($i) : " mob[$i]
if [ $i == $no_user ]; then break; fi
done
else
read -p "Enter Mob no ($i) : " mob[$i]
fi
if [ $no_user > 1 ]; then
for ((i=1;; i++)); do
echo "Mobile no $i = " ${mob[$i]}
if [ $i == $no_user ]; then break; fi
done
fi

关于linux - 读取多个输入并存储在数组中,然后在Shell脚本中打印该数组中的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29967444/

10-13 03:20