Creating an array out of users: shell script


 
Thread Tools Search this Thread
Operating Systems BSD Creating an array out of users: shell script
# 1  
Old 03-05-2011
Creating an array out of users: shell script

I want to create a shell script for a menu selection consisting of users defined on the machine. To an administrator having the privileges, the selection menu will look as follows:
Code:
Select the user you want to define the variables for:
1) my-username-1
2) my-username-2
etc

Then there would be a read statement, such as the following:
Code:
read $OPTION

of which #OPTION holds the name of the user for whom the task should be performed.
A start would be to use the following:
Code:
cat /etc/passwd | grep home | cut -d : -f 1

How do I turn this into an array or at least something that allows the creation of a selection menu?
Can I reasonably assume that all defined users have a directory in /usr/home?
# 2  
Old 03-06-2011
One option:

Code:
USERS=("" $(awk -F: '{print $1}' /etc/passwd))

echo ${USERS[@]} | awk '{print ++C ") " $1}' RS=" "

printf "Enter a number:  "
read OPTION

echo You chose ${USERS[$OPTION]}

Or you could use select, for example:
Code:
USERS=("" $(awk -F: '{print $1}' /etc/passwd))

PS3="Select a user: "
select USER in ${USERS[@]}; do
 break
done

echo You chose $USER

(you need to modify the awk to remove blank lines, comments and system users)
# 3  
Old 03-06-2011
Thank you very much for your response.
In both cases I am getting the following error message:
Code:
./test-selection-menu.sh: 1: Syntax error: word unexpected (expecting ")")

The following line:
Code:
$(awk -F: '{print $1}' /etc/passwd)

already generates the list of users on its own, so what does the first part of USERS do?
Code:
(""

I looked into selection menus a bit more and I see PS3 a lot in discussions about this. Is PS3 a reserved acronym?
# 4  
Old 03-06-2011
You don't need the "" with the select version, actually.

It is used in the first version because arrays are zero-based (the first element would be index 0). The "" was to use up element 0, so that the usernames would begin at index 1.

For this problem, I would probably use the select approach.

What shell do you use?
# 5  
Old 03-06-2011
Thank you again for your response.
I am using /bin/csh under FreeBSD 8.1 x64.
I have tried many variations of the select script, but I either get an error as such:
Code:
${USERSx[...}: Bad substitution

or as such:
Code:
Command not found

Could this have to do with the shell?
Also, I am assuming no #!/usr/bin/sh or such directive is needed as the first line?
# 6  
Old 03-06-2011
I'd say it has everything to do with the shell. Why are you using that?

You can eliminate the array totally, if using select, but as I don't think csh has a select, that's neither here, nor there!

You should always put "#!/usr/bin/sh or such directive" in the first line. Just preferably not #!/bin/csh!!
# 7  
Old 03-06-2011
I am using the csh, because it is part of the standard install of FreeBSD 8.1 x64. This script happens to be tested on a sandbox machine that is reinstalled every few days.
I currently have the following script ("x" appended to be sure that variables do not interfere with reserved words):
Code:
#!/usr/bin/sh
USERSx=$(awk -F: '{print $1}' /etc/passwd)
PS3="Select a user: "
select USERx in ${USERSx[@]}; do
 break
done
echo You chose $USERx

This leads to a Command not found error, regardless of whether I use eval, `` quotes, extra parentheses etc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Creating a group of users with script

Hi, I have a file with usernames, and the comment section, e.g : Data removed by request of sanchitadutta91, 20 May 2020 I need to add these users into a server. Is it possible to use a script to create the users, together with the comment ? From the commandline to add one user, the... (2 Replies)
Discussion started by: anaigini45
2 Replies

2. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

Shell script for creating multiple users with password

for UserName in `cat users` ; do useradd -d /u02 -s /usr/libexec/openssh/sftp-server -G ftp-users $UserName ; PassWord=$( echo $( tr '' '' <<< ${UserName:0:1} )${UserName:1} ) ; echo "$PassWord@123" | passwd $UserName --stdin ; done can some one explain what the bold text do Please use... (5 Replies)
Discussion started by: James0806
5 Replies

4. Red Hat

Script for Creating more users like 50 in one server

Hi friends could you please share me the script to create more users in one server except newusers, is there any option to change secondary group and how to print passwd and changing the age all these things how can i edit in newsers.. please help me (0 Replies)
Discussion started by: indiankrish
0 Replies

5. Shell Programming and Scripting

Creating new users using a text file as imput (using only shell script and/or awk)

I need somebody who could help with an exercise. You have a text file called users.txt with this info inside: users.txt: user1:1234:/home/homedir1 ; user2:1234:/home/homedir2 ; user3:1234:/home/homedir3 ; user4:1234:/home/homedir4 ; The script should create an user using the... (2 Replies)
Discussion started by: marcosruiz
2 Replies

6. Shell Programming and Scripting

Writing a script on creating and comparing users

Hello Everybody i am a newbie to the bash scripting.please can someone help me out here.i want to write a script on creating 10 users with passwords and comparing the users to the ones in /etc/passwd file.am i gonna use arrays in creating the 10 users or what?and again, how do i compare both... (1 Reply)
Discussion started by: bruno_brunt
1 Replies

7. UNIX for Dummies Questions & Answers

Script for creating users

Hi, I wants to create the users for multiple machine from single main server using the shell script ,can you please help me on this.... (2 Replies)
Discussion started by: Rahulne25
2 Replies

8. Shell Programming and Scripting

Script in bash wchich creating a new users...

Hi, I am a new on this forum but i like :) I need a script in bash which will be crating a new user with folder for websites. For example: I will run this program and he creating a new user(with my name) and folder whcich name like user and if i will localho/~user in browser, she show me files from... (1 Reply)
Discussion started by: puclavv
1 Replies

9. Shell Programming and Scripting

Help with shell script - creating users

echo -e "Enter in a username : \c" read username grep "^$username:" /etc/passwdWhat I'm trying to do is take in a username from my script and I need to be able to check if that username already exists. If it does the script should display a message saying that the user already exists and exit. ... (2 Replies)
Discussion started by: shadowcat
2 Replies

10. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies
Login or Register to Ask a Question