2

Read multiple inputs then store that in an array or variables, then print the values from that array/variables in shell script

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
3
  • 1
    You forgot the question. Commented Apr 30, 2015 at 12:12
  • 2
    I think the answer is 42. Commented Apr 30, 2015 at 12:47
  • 1
    Using a loop that just reads users until a sentinel/blank line is read instead of asking for a count is probably a friendlier model. Commented Apr 30, 2015 at 13:04

2 Answers 2

2

Change $[mob$i] and mob$i to mob[$i] at line 5 and 9. And also change $[mob$i] to ${mob[$i]} at line 13

Complete code:

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
Sign up to request clarification or add additional context in comments.

Comments

1

Using mob$i instead of $[mob$i] on line 4 will fix your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.