Loop through usernames 1 by 1 and select

Hello All,

A quick description of what I would like to do;

1) Find all the current registered usernames and loop through them.
2) Display each username 1 by 1 and Ask the user if they would like to store the username in another array (y or n)
3) Display the list of chosen usernames outside the original loop

This is what I have so far;

#!/bin/sh
 
for username in  $(cut -d":" -f 1 /etc/passwd) *** This line works***
do
echo "Add $username to the list ? (y/n)"
        read answer
        if [[ $answer = y ]]; then
                storeSelectedUser = ${username[@]}
        fi
done
 
for selUse in ${storeSelectedUser[@]}
do
        print $selUse
done

The above is able to find all the registered users and display them one by one. The problem I am having is storing the chosen items in an array. I've searched the site and found similar problems but cant seem to get it right. Appologies if this has been asked before, it seems like it would be a common problem. Can anyone help?

Regards

Please use code tags for code and data samples, thank you

remove space from the below statement.

storeSelectedUser=${username[@]}

Thanks for your reply, that worked! I'll need to brush up on my syntax!!

How did it work? "storeSelectedUser" is not even an array in your script. Try the following:

#!/bin/sh
i=0 
for username in  `cut -d":" -f1 /etc/passwd`
do
    echo -e "$username - add $username to the list ? (y/n): \c "
    read answer
    if [ "$answer" == "y" ]
    then
        storeSelectedUser[$i]=$username
        i=$(($i + 1))
    fi
done
 
for selUse in ${storeSelectedUser[@]}
do
    echo $selUse
done

Ah, see what you mean. It would only store the last item selected rather than all of them. Thanks.

---------- Post updated at 11:25 AM ---------- Previous update was at 11:12 AM ----------

Forgot to add. The only change I made to your code was that I used;

for username in $(cut -d":" -f 1 /etc/passwd)

instead of;

for username in `cut -d":" -f1 /etc/passwd`

This works perfectly for me. Thanks