The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
generating line numbers sam4now Shell Programming and Scripting 7 04-28-2008 03:17 PM
reappearing menu list using select forever_49ers Shell Programming and Scripting 9 09-13-2006 01:05 PM
Creating menu list from configuration file nir_s Shell Programming and Scripting 8 01-11-2006 12:45 AM
generating timer Confuse High Level Programming 5 09-16-2003 11:37 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-05-2006
Registered User
 

Join Date: Feb 2004
Posts: 91
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.
Reply With Quote
Forum Sponsor
  #2  
Old 01-05-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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
Reply With Quote
  #3  
Old 01-05-2006
Registered User
 

Join Date: Feb 2004
Posts: 91
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.
Reply With Quote
  #4  
Old 01-05-2006
linuxpenguin's Avatar
Registered User
 

Join Date: May 2002
Location: India
Posts: 295
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.
__________________
War doesnt determine who is right, it determines who is left
Reply With Quote
  #5  
Old 01-05-2006
Registered User
 

Join Date: Feb 2004
Posts: 91
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.
Reply With Quote
  #6  
Old 01-05-2006
Registered User
 

Join Date: Feb 2004
Posts: 91
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
Reply With Quote
  #7  
Old 01-05-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0