Using a list menu as a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using a list menu as a variable
# 1  
Old 01-15-2009
Using a list menu as a variable

Hi again

I have the follwing - cat ~/ABCFILE | grep "$SYSTEM" | grep "$USERNAME"

What I'm looking to do is have the variable for $SYSTEM determined by the user making a selection from a numbered list. So, input 1 would be system ABC etc.

I'm very puzzled as to how to go about this?

Any advice at all would be hugely appreciated

Regards
Kip
# 2  
Old 01-15-2009
whoami gives you the username of the person logged on (or sudo'ed into the account). Most shells have the USER environment variable but sudo will usually unset it. whoami works always.

Code:
USERNAME=$(whoami)
# in bash
echo -n "enter a system name: "
# in ksh
echo "enter a system name: \c"

read SYSTEM
grep ~/ABCFILE "$SYSTEM"  | grep "$USERNAME"

Is that what you are asking?
# 3  
Old 01-15-2009
If you have ksh use a select loop. It generates a simple numbered menu out of a list that you must supply. It uses the same syntax as a for loop.

Code:
select myvar [in <list>]
do
  <whatever based on value of $myvar>
done

If you omit 'in <list>' it will default to using "$@" (quoted command line arguments)

This command displays the PS3 prompt for a reply. So set that to something meaningful before running the select command.


so if you did:

Code:
PS3="Select the number of your system: "

select system in ABC DEF GHI
do 
    cat ~/ABCFILE | grep "$system" | grep "$USERNAME"
    break
done

Your user will see a menu and prompt as follows:

1) ABC
2) DEF
3) GHI

Select the number of your system:

You need the 'break' in there in order to exit the select loop in an orderly fashion. Otherwise select will go on forever unless you issues a <Ctrl>D.

I have implied but not said until now that when your user selects a number the corresponding menu item will be placed in the variable 'system'. The actual number will be placed in the built-in variable 'REPLY', in case you want to make use of that. So if the user selects '2' then "$system" = "DEF" and $REPLY = 2.

Lastly, you can build a dynamic list, more or less. I've used select lists based upon files in a directory that look something like this:

Code:
select myfile in $(ls -1 /mydir/*)
do
  cat $myfile  # simplistic, but whatever you need to do to the file goes here
  break
done


Last edited by rwuerth; 01-15-2009 at 01:08 PM.. Reason: learned to use code blocks
# 4  
Old 01-22-2009
Many thanks for all the help, its now working a treat.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Whiptail menu, getting back the variable

Hi all Only learning so if any mistakes, let me know I am trying to create a menu box with Whiptail, taking in the variables from a txt.file called Name.txt which has just 4 names listed for now, one below each other..ie Dave John Mike Mary Bash script is below and calls the txt... (8 Replies)
Discussion started by: olearydc
8 Replies

2. Red Hat

Menu system for terminal like Putty for host /ip list

Is there a way to create a menu in Gnome terminal to have a list of hosts with ip's like in Putty on Windows? (2 Replies)
Discussion started by: jlouki01
2 Replies

3. UNIX for Dummies Questions & Answers

Input A Menu Selection In A Variable

hey all, me again....having a problem with my code, where I essentially am trying to show a menu, have the user select an option (from 1 to 5), then display which selection they picked... #!/bin/bash # A LIST OF THE ERROR MESSAGES AND THE PROPER SYNTAX: error_0="Correct amount of... (1 Reply)
Discussion started by: SoVi3t
1 Replies

4. UNIX for Dummies Questions & Answers

How Do I Create A Multi Line Menu Variable?

I want something that would show up basically like: Menu ----- 1) Option 1 2) Option 2 3) Option 3 Pick one: I tried menu = " Menu \r\n ----- \r\n 1)Option 1 \r\n..............etc etc etc" but that didnt work (just got the whole menu one one line, with the... (2 Replies)
Discussion started by: SoVi3t
2 Replies

5. Shell Programming and Scripting

Variable sub-menu issue

The code im having problems with is highlighted in red, upon selecting option 2 on the main menu (highlighted green) my echo "NETWORK CONNECTIVITY" command seems to get overlooked and the resulting output is "Thank you for using the Operator Administrative Tool." being displayed. Can anyone tell me... (2 Replies)
Discussion started by: warlock129
2 Replies

6. Shell Programming and Scripting

Menu list in Unix csh - command not found

Hello, im taking a class of Unix and i dont really know much about it, im trying to create a list of menu a user would select from and im very lost. Basically it will have 5 options, the user will chose from: 1. list files in the pwd 2. display date and time 3. is the file file or directory 4.... (5 Replies)
Discussion started by: morava
5 Replies

7. Shell Programming and Scripting

reappearing menu list using select

is there a way I can make the menu list reappear when I use select ? ----- menulist="Change_title Remove_tag Change_tag Add_line Quit" select word in $menulist #change_title remove_tag change_tag add_line quit do case $word in # first menu option Change Title ... (9 Replies)
Discussion started by: forever_49ers
9 Replies

8. Shell Programming and Scripting

Creating menu list from configuration file

Hi folks, I have the following function ,which generates menu for installation type: select_install_type() { echo echo ======================================== echo Please select the type of installation: echo ======================================== ... (8 Replies)
Discussion started by: nir_s
8 Replies

9. Shell Programming and Scripting

Generating a list of choices in a menu

Hello all, I have the below script and I'm a little stuck on the best way to continue. Essentially I'm creating a text file (systems.txt) with a list of servers in it by hostname. Then I would like to echo a menu with each hostname and a number to use to pick it from the list. It's somehow... (7 Replies)
Discussion started by: sysera
7 Replies
Login or Register to Ask a Question