-1

Good Day, I'm new in bash scripting, requesting for your assistance. I'm trying to loop my sub menu to go back to the main menu in glassfish. I'm using if statements.

I wish for my script to go back to the Main Menu

#!/bin/bash
asadmin=/glassfish/bin/asadmin

echo "This is a console"
echo
echo "1. List Domains"
echo
echo "2. Start Domain"
echo
echo "3. Stop Domain"
echo
echo
echo "Choice:"
read answer
if [ $answer == '1' ]
then
$asadmin list-domains
fi
if [ $answer == '2' ]
then
echo "domain1"
echo "domain2"
read startdomainanswer
$asadmin start-domain $startdomainanswer
fi
if [ $answer == '3' ]
then
echo "domain1"
echo "domain2"
read stopdomainanswer
$asadmin stop-domain $stopdomainanswer
fi
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
johnmavvv
  • 3
  • 3

1 Answers1

0

You currently have no loops, not even of any kind.

Your script runs, waits for you at the choose command, and then drops out at the end. The illusion of a loop is created by the recursive call to asadmin again, which also waits for you at the choose command and then, itself, drops out at the end.

My first suggestion would be to add an "exit" option into the script and then add a while loop to keep going round and round, asking for something do, until you select that "exit" option.

echo "This is a console"
while [ 1 ] 
do
   echo "Choice:"
   read answer
   case answer in 
   1 ) 
      . . . 
   2 ) 
      . . . 
   x ) 
      exit 
      ;;
   esac
done

My second suggestion (and they way I've done it in the past) would be to separate the actions from the "menu" script completely. Read the options from a [menu] file and simply change the name of the file you're reading from to "move" between menus.

For example, using these "menu" files:

[main.menu]
1:List Domains:asadmin list-domains
2:Start Domain:@start
3:Stop Domain:@stop
x:Exit:EXIT

[start.menu]
1:Domain 1:asdamin start-domain Domain1
2:Domain 2:asdamin start-domain Domain2
x:Return:@main

[stop.menu]
1:Domain 1:asdamin stop-domain Domain1
2:Domain 2:asdamin stop-domain Domain2
x:Return:@main

You could create a script something like this:

[asmenu.bash]
$! /bin/bash 

$MENU_FILE="${1:-main}.menu"

while [ 1 ] 
do 
   echo "${MENU_FILE}"
   echo " "

   # Print Menu items - Choice number and description 
   awk -F':' '{print $1, $2}' "${MENU_FILE}"

   echo " " 
   echo "Choice:" 
   read CHOICE 

   # Extract the third element (command or menu name)  
   ACTION=$( awk -F':' '/^'${CHOICE}':/' {print $3}' "${MENU_FILE}" )

   if [ "EXIT" = "${ACTION}" ] ; then 
      exit ' End the menu Script 
   fi 

   if [ "${ACTION}" =~ ^@ ] ; then 
      # Leading at symbol implies a menu name
      MENU_FILE="${ACTION:1}.menu"
   else
      # Otherwise it's a command to execute 
      ${ACTION}
   fi 
done 

Which (if I've written it correctly) should do something like this to, say, start your second Domain:

/* Start of run */ 
main.menu

1    List Domains
2    Start Domain
3    Stop Domain
x    Exit

Choice: 2

start.menu

1    Domain 1
2    Domain 2
x    Return

Choice: 2

/* execution of asadmin start-domain Domain2 */ 

start.menu

1    Domain 1
2    Domain 2
x    Return

Choice: x 

main.menu

1    List Domains
2    Start Domain
3    Stop Domain
x     Exit

Choice: x 
/* end of run */ 
Phill W.
  • 1,479
  • 7
  • 7
  • thank you! I have done it, only problem is that when I exit the main menu, the glassfish also stops. – johnmavvv Oct 15 '19 at 01:51
  • Does asadmin have to stay running? If so, add an ampersand ("&") to the end of the command, to run the process (invoked by your menu script) in background. – Phill W. Oct 15 '19 at 07:06