LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-30-2012, 06:51 PM   #1
eamesj
Member
 
Registered: May 2006
Posts: 51

Rep: Reputation: 1
creating submenus in bash


Hi all,

Trying to build a series of options in a bash script and im fine with 1 menu but i want to put a sub menu in one of the options...

#!/bin/bash
menu ()
{
clear
echo "-------------------------------"
echo " M A I N - M E N U"
echo " 1) option "
echo " 2) option "
echo " 3) option "
echo " 4) option "
echo "-------------------------------"
while
do
read action
case $action in
"1")
do something
;;
"2")
do something else
;;
"3")
submenu ()
{
clear
echo "-------------------------------"
echo "SUB -MENU"
echo "a) option"
echo "b) option"
echo "-------------------------------"
while :
do
read subaction
case $subaction in
"a")
do something ;;
"b")
do something else ;;
esac
done
}
submenu;
;;
"4")
echo "Bye!"
exit 0
;;
*)
echo "Error: Invalid option..."
read -p "Press [Enter] key to continue..." readEnterKey
;;
esac
done
}
menu;


but bash is complaining that there's a syntax error, can someone tell me how can i create in submenus in bash ?

thanks,
 
Old 12-30-2012, 07:34 PM   #2
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
solved

Solved it

Hadnt added a ;; what i wrote is actually correct. Can see why its called bash...

solution:

3)
submenu ()
{
clear
echo "-------------------------------"
echo "SUB -MENU"
echo "a) option"
echo "b) option"
echo "-------------------------------"
while :
do
read subaction
case $subaction in
"a")
do something ;;
"b")
do something else ;;
esac
done
}
submenu;
;;
4)
 
Old 12-30-2012, 11:28 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


I recommend not putting the submenu function definition inside the main menu function. Functions are generally intended to be defined at the top of the script, so that they will be sitting in the environment ready to use when you need them.

It also lessens the clutter, helping readability.


Edit:
Also, your first loop has an error:

Code:
while		#should be "while :"
do
read action
By the way, many experienced scripters feel that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block. Be sure to indent all sub-commands properly, too.

Code:
while true; do
	read action
	...
done
I also personally prefer using the true keyword to ":" in this kind of loop.

Last edited by David the H.; 12-30-2012 at 11:35 PM. Reason: as stated
 
1 members found this post helpful.
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating SubMenus in Gnome RHE6 chris_carr Linux - Newbie 3 01-06-2012 08:31 AM
[SOLVED] Adding and moving submenus in kmenuedit (kde4) josephj Linux - Desktop 3 12-05-2010 05:03 AM
Unreadable submenus in Firefox 2.0.0.13 raypen Linux - Software 0 04-02-2008 01:17 PM
Pointer click does not select submenus in X....Mathematica and Matlab xmeson Linux - Software 1 07-29-2006 07:01 PM
How do I move KDE submenus to the Mandrake menu? enigma Z Linux - Software 0 10-09-2003 12:45 PM


All times are GMT -5. The time now is 03:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration