then it should execute a set of statements and again ask like this
what do you want to do
1.add 2.subtract 3.exit
when i choose exit it should goto my previous menu asking again (please tell which option to chose
mango 2. tango 3. rango )
i have did coding to an extent like this
#!/usr/bin/ksh
#
echo -e -n "choose names as listed in the below format 1. mango 2. tango 3. rango 4.exit"
read answer
echo "$answer"
while true
do
case $answer in
mango) DT=$(date)
echo "******************* $DT ***********************"
echo -e "Specify what options 1.add 2.subtract 3.exit"
read input
#
if [ "add" = "$input" ]
then
echo "add"
elif [ "exit" = "$input" ]
then
exit 0;;
#iam getting error at the place exit 0;;
elif [ "subtract" = "$input" ]
echo "sub"
else
echo "!! must specify add or subtract"
fi
#
tango) echo "tango"
break;;
#
rango) echo "rango"
break;;
exit) exit 0;
break;;
#
*) echo "please enter correct value" read ;;
break;;
# --- VBE --- adding missing done...--- #
done
As you can see first it will ask me to choose from 6 options
mango 2. tango 3. rango 4.exit
. If for example if i chose 1. mango
then it will ask
1.add 2.subtract 3.exit
now if i chose exit . it should go back to main menu (THIS IS WHAT IAM NOT ABLE TO DO) and ask
mango 2. tango 3. rango 4.exit
if i give exit here then only script should exit.
Please help how to do this one in the existing scrit. if not possible then modify and do in whicvere way u wish to do to to get the final result. I have just put an skeleton overview of the script. If anyone have any better approach on this then please advice as to how to arrive to this solution.
The ;; are in the wrong place, they should be after the fi which terminates your if statement. In a case statement the ;; terminates each separate condition.
The last two lines are a bit iffy. Probably best to just output an error message because the script is going to ask the question again on the next iteration of the "while true" loop (if you move the question to within the loop). Also, lose the break;; line because it is totally out of context.
Add the "esac" to complement the original "case".
Add the "done" to complement the original "do".
*) echo "please enter correct value" ;;
esac
done
The initial question to populate $answer should probably be inside the do-done loop or the users will neve get the chance to change the value.
Consider using the ksh's built-in "select" construct for constructing menus. This shows a nested example that does what you are looking for:
#!/usr/bin/ksh
# Set select construct prompts for each menu level.
typeset -r MAINPROMPT="Select a main option: "
typeset -r MANGOPROMPT="Select mango option: "
# Loop main menu until user exits explicitly.
while :
do
print "\nMenu Title Goes Here\n"
PS3=$MAINPROMPT # PS3 is the prompt for the select construct.
select option in mango tango rango exit
do
case $REPLY in # REPLY is set by the select construct, and is the number of the selection.
1) # mango
# Loop mango menu until user exits explicitly.
while :
do
print "\nmango sub-menu title\n"
PS3=$MANGOPROMPT
select option1 in add substract exit
do
case $REPLY in
1) # add
print "\nYou picked [add]"
break # Breaks out of the select, back to the mango loop.
;;
2) # subtract
print "\nYou picked [subtract]"
break # Breaks out of the select, back to the mango loop.
;;
3) # exit
break 2 # Breaks out 2 levels, the select loop plus the mango while loop, back to the main loop.
;;
*) # always allow for the unexpected
print "\nUnknown mango operation [${REPLY}]"
break
;;
esac
done
done
break
;;
2) # tango
;& # Fall through to #3
3) #rango
print "\nYou picked $option"
break
;;
4) # exit
break 2 # Break out 2 levels, out of the select and the main loop.
;;
*) # Always code for the unexpected.
print "\nUnknown option [${REPLY}]"
break
;;
esac
done
done
exit 0
New, improved version that clears the screen before displaying the menu:
#!/usr/dt/bin/dtksh
# set select construct prompts for each menu level.
typeset -r MAINPROMPT="Select a main option: "
typeset -r MANGOPROMPT="Select mango option: "
# Loop main menu until user exits explicitly.
while :
do
clear
print "\nMenu Title Goes Here\n"
PS3=$MAINPROMPT # PS3 is the prompt for the select construct.
select option in mango tango rango exit
do
case $REPLY in # REPLY is set by the select construct,
# and is the number of the selection.
1) # mango
# Loop mango menu until user exits explicitly.
while :
do
clear
print "\nmango sub-menu title\n"
PS3=$MANGOPROMPT
select option1 in add substract exit
do
case $REPLY in
1) # add
print "\nYou picked [add]"
break # Breaks out of the select, back to the mango loop.
;;
2) # subtract
print "\nYou picked [subtract]"
break # Breaks out of the select, back to the mango loop.
;;
3) # exit
break 2 # Breaks out 2 levels, the select loop plus
# the mango while loop, back to the main loop.
;;
*) # always allow for the unexpected
print "\nUnknown mango operation [${REPLY}]"
break
;;
esac
done
sleep 2
done
break
;;
2) # tango
;& # Fall through to #3
3) #rango
print "\nYou picked $option"
break
;;
4) # exit
break 2 # Break out 2 levels, out of the select and the main loop.
;;
*) # Always code for the unexpected.
print "\nUnknown option [${REPLY}]"
break
;;
esac
done
sleep 2
done
exit 0