1

I'm trying to take the input from the user into an array but Shell is accepting user input separated by spaced. Is there any way to accept the user input given separately in each line. My code below:

#!/bin/bash
echo "enter the servers names..."
read -a array
for i in "${array[@]}"
do
echo $i
done
exit 0

Input:

hello world

I want to the input to be taken as below (in two different lines):

hello
world

Kindly help. Thanks.

3
  • Set IFS=$'\n' before your read to only break on newlines. (you can save the old value, e.g. (oldIFS=IFS before changing it, the restore the original IFS=oldIFS after the loop -- not necessary here because you just exit) Commented May 1, 2017 at 6:52
  • you mean this way - IFS=$'\n' read -a array ? Commented May 1, 2017 at 7:03
  • It sounds like you want to preprocess the input. Try tr -s ' ' '\n' | read -a ..., and be aware that this will make it so the variables are only available within the subshell created by the pipe. This will also take care of blank lines for you (unless that blank line is at the start of the input). Commented May 1, 2017 at 17:44

3 Answers 3

1

You can specify the delimiter of read to stop reading, rather than newline. (read man page)

-d delim continue until the first character of DELIM is read, rather than newline

For example:
read -d':' -a array

If you want there is no delimiter to input, you can use a loop to read into the elements of the array, then check whether the input is null string or not.

i=0
read "array[$i]"
while [ "${array[$i]}" ];
do
    let "i = i+1"
    read "array[$i]"
done

So the input will be

hello
world
# > there is one more empty line
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I don't have any delimiters in my input. They are just one word and should be given in each line
this code is perfectly working fine. But that empty line is troubling as it is taking an input
So far, i don't know how to deal with the one more empty line... Or you can input an EOF(ctrl-d).
1

According to help read:

Reads a single line from the standard input, or from file descriptor FD...

This loop would do instead:

echo "enter the servers names..."
i=0
until false; do
  read server
  [ -z "$server" ] && break
  let i=i+1
  array[$i]="$server"
done

for i in "${array[@]}"; do
  echo $i
done
exit 0

The loop will exit on an empty line or EOF (ctrl-D).

Example session terminated by empty line:

@server:/tmp$ ./test.sh
enter the servers names...
se1
se2
se3

se1
se2
se3

Example session terminated by EOF on the empty line after se2:

@server:/tmp$ ./test.sh
enter the servers names...
se1
se2
se1
se2

Please note that I check for an empty string while reading the names; but it is possible to check for empty strings (or whatever else) in any loop, for example while printing them or doing other computations.

Comments

-1
#!/bin/bash
echo "enter the servers names..."
read -a array -d '\n'
for i in "${array[@]}"
do
echo $i
done
exit 0

or

#!/bin/bash
echo "enter the servers names..."
readarray array
for i in "${array[@]}"
do
echo $i
done
exit 0

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.