need helping writing shell script menu

Discuss Applications
Post Reply
User avatar
cdhgold
administrator
administrator
Posts: 382
Joined: Tue Mar 18, 2003 6:11 pm
Location: Texas

need helping writing shell script menu

Post by cdhgold »

leaarning shell scripts i hve 10 shell scripts that each lauch ssh to log me into a difeferent system i want to write somethign that would run and allow to choose which one i want from some kind of menu .. such as i run one script and it gives me choice of which system to log in to .. anyone have suggestions on where i could go to learn how to do this simple task

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

Assuming you just want a text based menu within the script then the "case" statement is what you'll want along with a "read" statement:

http://tldp.org/LDP/abs/html/testbranch.html#EX29

If you need more specific help I would be glad to code something up for you.

User avatar
cdhgold
administrator
administrator
Posts: 382
Joined: Tue Mar 18, 2003 6:11 pm
Location: Texas

Post by cdhgold »

getting error please help me figure thsi out followed the example

here is my script

Code: Select all

#!/bin/bash
clear
echo "Networker List"
echo "--------------"
echo "1 ADV1.DFW1"
echo "2 ADV2.DFW1"
echo "3 ADV3.DFW1"
echo "4 BAS1.DFW1"
echo "5 BAS2.DFW1"
echo "6 ADV1.IAD1"
echo "7 BAS1.IAD1"
echo "8 IAD1"
echo "9 ENV2.LON"
echo "10 LON"
echo "11 LON2"
echo "12 SAT2"

read "$networker" in
	"1" )
	echo "NO IP"
	;;
	
	"2" )
	xterm -e "ssh chris.goldsmith@*"
	;;

	"3" )
	xterm -e "ssh chris.goldsmith@*"
	;;

	"4" )
	xterm -e "ssh cgoldsmi@*"
	;;

	"5" )
	xterm -e "ssh cgoldsmi@*"
	;;

	"6" )
	xterm -e "ssh cgoldsmi@*"
	;;

	"7" )
	xterm -e "ssh cgoldsmi@*"
	;;

	"8" )
	xterm -e "ssh chris.goldsmith@*"
	;;

	"9" )
	xterm -e "ssh chris.goldsmith@*"
	;;

	"10" )
	xterm -e "ssh cgoldsmi@*"
	;;

	"11" )
	xterm -e "ssh chris.goldsmith@*"
	;;

	"12" )
	xterm -e "ssh cgoldsmi@*"
	;;

esac
exit 0
i have replaced the ip addres with * for security reasons. i did a chmod a+x networker.sh then ran it and got following results

Networker List
--------------
1 ADV1.DFW1
2 ADV2.DFW1
3 ADV3.DFW1
4 BAS1.DFW1
5 BAS2.DFW1
6 ADV1.IAD1
7 BAS1.IAD1
8 IAD1
9 ENV2.LON
10 LON
11 LON2
12 SAT2
2
./networker.sh: line 18: read: `': not a valid identifier
./networker.sh: line 19: syntax error near unexpected token `)'
./networker.sh: line 19: ` "1" )'

what is wrong?[/code]

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

The first thing I see is your read statement is wrong as hinted by the error. Your read should look like this:

Code: Select all

read networker
and right under that line you should have your "case" statement:

Code: Select all

case "$networker" in
It's also usually best to put in a default option:

Code: Select all

   *)
   echo "You did not press a valid option!"
   ;;
So your program would look like this:

Code: Select all

#!/bin/bash

clear

echo "Networker List"
echo "--------------"
echo "1 ADV1.DFW1"
echo "2 ADV2.DFW1"
echo "3 ADV3.DFW1"
echo "4 BAS1.DFW1"
echo "5 BAS2.DFW1"
echo "6 ADV1.IAD1"
echo "7 BAS1.IAD1"
echo "8 IAD1"
echo "9 ENV2.LON"
echo "10 LON"
echo "11 LON2"
echo "12 SAT2"

read networker

case "$networker" in

   "1" )
   echo "NO IP"
   ;;

   "2" )
   xterm -e "ssh chris.goldsmith@*"
   ;;

   "3" )
   xterm -e "ssh chris.goldsmith@*"
   ;;

   "4" )
   xterm -e "ssh cgoldsmi@*"
   ;;

   "5" )
   xterm -e "ssh cgoldsmi@*"
   ;;

   "6" )
   xterm -e "ssh cgoldsmi@*"
   ;;

   "7" )
   xterm -e "ssh cgoldsmi@*"
   ;;

   "8" )
   xterm -e "ssh chris.goldsmith@*"
   ;;

   "9" )
   xterm -e "ssh chris.goldsmith@*"
   ;;

   "10" )
   xterm -e "ssh cgoldsmi@*"
   ;;

   "11" )
   xterm -e "ssh chris.goldsmith@*"
   ;;

   "12" )
   xterm -e "ssh cgoldsmi@*"
   ;;

   *)
   echo "You did not press a valid option!"
   ;;

esac

exit 0
Next you might want to create a loop so that if an invalid option is pressed it presents the menu again without having to run the program again. If you do this you might want to put exit statements under each "xterm" line so the program exits after a valid option is executed. Or maybe you don't want it to exit, maybe you would rather background the xterm process and have the menu redraw so another option can be pressed. Then you can start as many xterm/ssh sessions as you want. You would probably want to add an "exit" option to the menu if you did this. It would be cleaner than breaking out of the script with a ^C.

User avatar
cdhgold
administrator
administrator
Posts: 382
Joined: Tue Mar 18, 2003 6:11 pm
Location: Texas

Post by cdhgold »

void you the man thanks .. that works great now .. as it is when i make a choice it does what i want it brings up the right ssh session .. what i would like to do now is 1) after making a choice have it go back to the option so i can choose an additional session without rerunning the script and 2) when i leave a session be back at menu where i can either close it or choose another session ...

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

You mean something like this?

Code: Select all

#!/bin/bash

quit=0

while [ "$quit" != "1" ]
do

   clear

   echo "Networker List"
   echo "--------------"
   echo "0 Q)uit"
   echo "1 ADV1.DFW1"
   echo "2 ADV2.DFW1"
   echo "3 ADV3.DFW1"
   echo "4 BAS1.DFW1"
   echo "5 BAS2.DFW1"
   echo "6 ADV1.IAD1"
   echo "7 BAS1.IAD1"
   echo "8 IAD1"
   echo "9 ENV2.LON"
   echo "10 LON"
   echo "11 LON2"
   echo "12 SAT2"
   echo ""
   echo -n "Enter Option> "

   read networker

   case "$networker" in

      [0qQ] )
      echo "Have a nice day!"
      quit=1
      ;;

      "1" )
      echo "NO IP"
      ;;

      "2" )
      xterm -e "ssh chris.goldsmith@*" &
      ;;

      "3" )
      xterm -e "ssh chris.goldsmith@*" &
      ;;

      "4" )
      xterm -e "ssh cgoldsmi@*" &
      ;;

      "5" )
      xterm -e "ssh cgoldsmi@*" &
      ;;

      "6" )
      xterm -e "ssh cgoldsmi@*" &
      ;;

      "7" )
      xterm -e "ssh cgoldsmi@*" &
      ;;

      "8" )
      xterm -e "ssh chris.goldsmith@*" &
      ;;

      "9" )
      xterm -e "ssh chris.goldsmith@*" &
      ;;

      "10" )
      xterm -e "ssh cgoldsmi@*" &
      ;;

      "11" )
      xterm -e "ssh chris.goldsmith@*" &
      ;;

      "12" )
      xterm -e "ssh cgoldsmi@*" &
      ;;

      *)
      echo "You did not press a valid option!"
      sleep 2
      ;;

   esac

done

exit 0

Post Reply