Generating a list of choices in a menu


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generating a list of choices in a menu
# 1  
Old 01-05-2006
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 associating the number with the hostname that is confusing me.

Here is my code:

#!/bin/bash
clear
COUNT=0
SYSTEMS=`cat systems.txt`

while [ "$CHOICE" != "q" ]
do

for SYSTEM in $SYSTEMS

do
COUNT=`expr $COUNT + 1`

echo "$COUNT. $SYSTEM"
done

echo ""
echo "Please choose."
echo ""
read CHOICE

COUNT=0
clear

done




What I have so far example:

1. host1
2. host2
3. host3

Please choose.

3


What I'd like example:

1. host1
2. host2
3. host3

Please choose.

3

You picked host3!



The hostnames are not always host* of course, so I can't use that as a convention for the script to know which server I mean.

Any help would be awesome. Thanks guys. Smilie
# 2  
Old 01-05-2006
assuming your '/tmp/hosts.txt' contains:
Code:
host1
host2
host3
host4

here's one way of creating a 'menue':
Code:
#!/bin/ksh

hosts='/tmp/hosts.txt'
PS3="Pick one of the above: "

select i in $(< ${hosts})
do
      [ $i ] && print "you picked->[${i}]" || print -u2 'invalid selection'
done

# 3  
Old 01-05-2006
Quote:
Originally Posted by vgersh99
assuming your '/tmp/hosts.txt' contains:
Code:
host1
host2
host3
host4

here's one way of creating a 'menue':
Code:
#!/bin/ksh

hosts='/tmp/hosts.txt'
PS3="Pick one of the above: "

select i in $(< ${hosts})
do
      [ $i ] && print "you picked->[${i}]" || print -u2 'invalid selection'
done

That's awesome vgersh, thank you very much. Smilie
# 4  
Old 01-05-2006
that is cool, I tried this and noticed, i couldnt exit, so did some research on man ksh and then came up with this

select i in $(< hosts.txt)
do
if [ "$REPLY" = "q" ]
then
break
fi
[ $i ] && print "you picked->[${i}]" || print -u2 'invalid selection'
done


so the content that is typed is stored in REPLY.
# 5  
Old 01-05-2006
Quote:
Originally Posted by linuxpenguin
that is cool, I tried this and noticed, i couldnt exit, so did some research on man ksh and then came up with this

select i in $(< hosts.txt)
do
if [ "$REPLY" = "q" ]
then
break
fi
[ $i ] && print "you picked->[${i}]" || print -u2 'invalid selection'
done


so the content that is typed is stored in REPLY.
This is also awesome. Smilie
# 6  
Old 01-05-2006
Here is what I have now, which is working well. I'm using ssh as a test of the menu. Once I log into the server I choose, and then logout, it doesn't redraw the menu choices again for the next choice. Is there a way I can make it redraw the choices after that first loop completes?

#!/bin/ksh
clear
HOSTS='systems.txt'
PS3="Please choose a system: "

select i in $(< ${HOSTS})
do
if [ "$REPLY" = "q" ]
then
break
fi
[ $i ] && print "" || print -u2 'Invalid Choice'
clear
ssh -X ${i}
done
# 7  
Old 01-05-2006
Code:
#!/bin/ksh

hosts='/tmp/hosts.txt'
PS3="Pick one of the above: "

select i in $(< ${hosts})
do
      [ $i ] && print "you picked->[${i}]" || print -u2 'invalid selection'
      clear
      ssh -X ${i}
      nl -s') ' "${hosts}"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. Programming

Listing Option Menu Choices from Text File

Hello, I am starting up a tool and one of the initial steps is to select a site/location which is being read from a text file. Here is the text file contents: site1 site2 site3 Here is the code: #!/usr/bin/python from Tkinter import * (3 Replies)
Discussion started by: tattoostreet
3 Replies

3. Shell Programming and Scripting

Need help in create menu with 3 sub menu using the case command

hi all i am a newbie to this is there any examples on creating a main menu with 3 sub menu main menu -> option a , b and c a menu -> option 1 ,2 and 3 b menu -> option 1 ,2 c menu -> option 1 ,2 i am getting headache as my code kept getting unexpected EOF ---------- Post... (0 Replies)
Discussion started by: chercm
0 Replies

4. 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

5. Shell Programming and Scripting

generating the variable list for WHILE READ statement at runtime

Hi, I am reading the contents of a file in variables as - cat ${var_file_name} | while read COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 The problem is ... my file can have any number of columns - 5, 10, 11 .... So i want a dynamic variable list as - cat ${var_file_name} |... (8 Replies)
Discussion started by: gopalss
8 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. UNIX for Dummies Questions & Answers

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... (3 Replies)
Discussion started by: Great Uncle Kip
3 Replies

8. 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

9. 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
Login or Register to Ask a Question