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:
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:
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:
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?
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.
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:
${USERSx[...}: Bad substitution
or as such:
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?
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):
#!/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.
...but the most important reason is that programming in CSH is an exercise in masochism. The pituful ad-hoc throws errors that almost never have anything to do with the actual mistake made.