1

This is how the script gets started over the terminal:

echo 'username1 username2 username3' | ./security_check.sh

This is the part of the program which should read the stdin and put it into an array:

while read -r line_input
do
    i_users[i]=$line_input
    i+=1
done

IFS is not set to something different in this script.

After the input is saved in an array it should be able to get printed like this:

for i in ${i_users[@]}
do
    echo "$i"
done

This script takes the whole input through stdin and puts it just in i_users[0]. How can I fix this? The array i_users was declared with declare -a i_users at the beginning of the script

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
Ch4nger
  • 11
  • 2

1 Answers1

2

Consider:

  • while read -r line_input is going to read data one line at a time
  • the script has one line of input (username1 username2 username3)
  • so the array ends up with a single entry (index=0 / value=username1 username2 username3)

To break the single line of input into separate array entries you have a few options, one being the read -a suggested by @choroba:

$ cat security_check.sh
#!/bin/bash

read -ra i_users
typeset -p i_users

Running OP's example script call:

$ echo 'username1 username2 username3' | ./security_check.sh
declare -a i_users=([0]="username1" [1]="username2" [2]="username3")

NOTE: This works for a single line of input (as shown in OP's sample script call).

markp-fuso
  • 28,790
  • 4
  • 16
  • 36