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. :slight_smile:

assuming your '/tmp/hosts.txt' contains:

host1
host2
host3
host4

here's one way of creating a 'menue':

#!/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. :slight_smile:

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. :slight_smile:

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

#!/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

Freaky, I haven't run into this one before yet. Thanks again Vgersh. All kinds of fun toys. :slight_smile: