Loop through usernames 1 by 1 and select


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through usernames 1 by 1 and select
# 1  
Old 11-29-2011
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;
Code:
#!/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

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-29-2011 at 06:23 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-29-2011
remove space from the below statement.
Code:
storeSelectedUser=${username[@]}

# 3  
Old 11-29-2011
Thanks for your reply, that worked! I'll need to brush up on my syntax!!
# 4  
Old 11-29-2011
How did it work? "storeSelectedUser" is not even an array in your script. Try the following:
Code:
#!/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

# 5  
Old 11-29-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing passing and executing select statement in loop

Hi, i want to do the following: Grep the following kind of strings for the 15digit ID which is stored in filename1: "14:06:51.396 INFO BMCREMEDYSD INPUT-ACTION Failed to retrieve Remedy Incident Modification record: 000000000039047 org.apache.axis2.AxisFault: Read timed out - complete... (9 Replies)
Discussion started by: Khushbu
9 Replies

2. Shell Programming and Scripting

Select command going to infinite loop after running the script

cd /opt/et/WAS/apps/8.0 find . -name "HostIntegration.properties" -o -name "HostSocket.properties" -o -name "environment.properties" 2> /dev/null | awk -F '' '{print $4}'|awk '!x++' | cat>/home/cbadmin/file1.txt cd /home/cbadmin/ PS3='Please enter a number from list of applications==>:' select... (3 Replies)
Discussion started by: bhas85
3 Replies

3. Shell Programming and Scripting

how to test input variable is a string in a select loop

Okay -- I hope I ask this correctly. I'm working on my little shell script to write vendor names and aliases to files from user input. If a user choose to add to a file, he can do that as well. I'm using a select loop for this function to list all the possible files the user can choose from.... (7 Replies)
Discussion started by: Straitsfan
7 Replies

4. Shell Programming and Scripting

Compare file names and select correct elements to include in "for each loop"

Hi everyone, I`ll try to be most clear I can explaining my help request. I have 2 folders Folder A-->This folder receives files through FTP constantly Folder B-->The files from Folder A are unzipped and then processed in Folder B Sometimes Folder A doesn`t contain all... (2 Replies)
Discussion started by: cgkmal
2 Replies

5. Shell Programming and Scripting

How to execute a no of SELECT COUNT(*) statements using a loop

HI Unix Gurus, I have a number of SELECT count(*) statements in an input file and I want to execute it using a shell script but one by one using loop in script.... How can I do this..... (7 Replies)
Discussion started by: ustechie
7 Replies

6. UNIX for Advanced & Expert Users

Usernames in Sudoers have #

We have users that have a # in their username. Sudo is working on some servers and not others. I have narrowed it down to the # in their username. Any suggestions or ideas why it is working on 1 server but not another. Server not working is - Solaris 10 patch level 138888-01 Server working is ... (1 Reply)
Discussion started by: Gibby13
1 Replies

7. UNIX for Advanced & Expert Users

Usernames across Applications

Hello everyone, I was just wondering if there was a way to make it so that usernames could be used across applications. For instance, I have a server that has a bunch of users. I want these users to have ways of accessing my database as well as a variety of other programs, but I don't want them... (2 Replies)
Discussion started by: gonzofish
2 Replies

8. Shell Programming and Scripting

extracting usernames with at least 4 characters

Hi, i want to use grep to extract users with at least 4 characters in their username, i've tried who | grep \{4,\} but thats not working!!!!!! Thanks (4 Replies)
Discussion started by: c19h28O2
4 Replies

9. UNIX for Advanced & Expert Users

Usernames and processes most used

I need a command that returns the usernames that have ran the 10 processes that have taken the most time to execute on a machine. I also need a command that returns only those directories that have read/execute permissions for all the users. (4 Replies)
Discussion started by: anw68ster
4 Replies
Login or Register to Ask a Question