读取多个输入并存储在数组中,然后在shell脚本中打印该数组中的值

时间:2015-04-30 12:10:41

标签: linux bash shell

读取多个输入然后将其存储在一个或多个变量中,然后在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

1 个答案:

答案 0 :(得分:1)

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