0

i have some problem in reading the input and store it in array in shell script. please help

declare -a array_name
echo "How many groups you want to enter?"
read group_count
echo "enter $group_count groups: "
for(( c = 0 ; c <= $group_count ; c++))
do
  read abc_elements
  while read abc_elements
  do
   array_name[$c] = "$abc_elements"
  done
done
echo -e "${array_name[@]}"

Thank you.

1
  • Please provide a bit of info yourself. What's abc_elements and what's array_name. Why are you trying to read abc_elements twice? What do your loops do and what are their terminating conditions. Finally, you didn't even say what is going wrong, what error messages you get, and how did you try to solve the issues. Commented Oct 30, 2013 at 10:16

1 Answer 1

2

You have added while loop here due to which code is always going into true condition. It's not needed here. And Use c < $group_count condition because array index starts from 0.

Use following code :

declare -a array_name
echo "How many groups you want to enter?"
read group_count
echo "enter $group_count groups: "
echo $group_count
for(( c = 0 ; c < $group_count ; c++))
do
  read abc_elements
#  while read abc_elements
#  do
  array_name[$c]="$abc_elements"
#  done
done
echo -e "${array_name[@]}"

It'll return you the array as expected.

Sign up to request clarification or add additional context in comments.

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.