LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 09-15-2007, 03:43 AM   #1
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Rep: Reputation: 32
Unhappy helping me in a bash script that perform a "select" menus Task


hello all .... ,


i want to make a bash script that controls the action of the logs and some services by using menus and i want it to be something like that

starts with that form :

1) mysql
2) system
3) apache
4) quit
Please Select a Choice Or For The Menu :

when the user enter a choice he will redirect to another menu for example if the user type 1 which is for mysql service this menu will appear

1) mysql-log
2) mysql-start
3) mysql-stop
4) mysql-restart


and every choice of that perform a certain function like mysql start , restart , stop and so on

i write this script

Code:
 #!/bin/bash

#set the PS third  variable

PS3='Please Select a Choice Or  For The Menu : '

list="mysql system-log apache-log quit"

select i in $list

do

if [ $i = "mysql" ]

then

list2="mysql-log mysql-start mysql-stop mysql-restart"

select j in $list2


do 


if [ $j = "mysql-log" ]

then 

               watch tail /var/log/mysqld.log


elif [ $j = "mysql-start"   ]
then 
	service mysqld start

elif [ $j = "mysql-start"   ]

then 
	service mysqld start
 
elif [ $j = "mysql-stop"   ]
then
	service mysqld stop 
elif [ $j = "mysql-restart"   ]
then 
	service mysqld restart 
elif [ $i= "system-log" ]
then 	
	watch tail /var/log/messages

elif [ $i= "apache-log" ]
then
        watch tail /var/log/httpd/error-log


elif [ $i = "quit" ]


then
       exit;

fi

done

fi

done


but i got errors >> 1-i cannot choose another option except the choice 1 which is mysql service if i choose another service nothing happened !!!!

Code:
  
Please Select a Choice Or  For The Menu : 2
Please Select a Choice Or  For The Menu :

2-if i choose the first item 1.)mysql it works and all the items listed in the mysql item work well but at the same time i cannot get back to the other option so how i can add a back option


and sorry for the long thread

Thanks For ALL
 
Old 09-15-2007, 07:19 AM   #2
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
If you indent your code you'll find it much easier to see where things are going wrong.

Code:
#!/bin/bash

#set the PS third  variable
PS3='Please Select a Choice from the Menu : '
list="mysql system-log apache-log quit"

while true; do
    select i in $list; do
        if [ $i == "mysql" ]; then
            list2="mysql-log mysql-start mysql-stop mysql-restart"
            select j in $list2; do
                if [ $j == "mysql-log" ]; then
                    watch tail /var/log/mysqld.log
                    break 2
                elif [ $j == "mysql-start" ]; then
                    service mysqld start
                    break 2
                elif [ $j == "mysql-stop" ]; then
                    service mysqld stop
                    break 2
                elif [ $j == "mysql-restart" ]; then
                    service mysqld restart
                    break 2
                fi
            done
        elif [ $i == "system-log" ]; then
            watch tail /var/log/messages
            break 1
        elif [ $i == "apache-log" ]; then
            watch tail /var/log/httpd/error-log
            break 1
        elif [ $i == "quit" ]; then
            exit 0;
        fi
    done
done
 
Old 09-15-2007, 08:00 AM   #3
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
thanks for you help it finally works

what is the break 2 and break 1 means / ? i know that break "only" alter the execution of the loop what about break 1 and 2 ? ...

but how i can make that ? > -if i choose the first item 1.)mysql it works and all the items listed in the mysql item work well but at the same time i cannot get back to the other option so how i can add a back option

how can i make a back option to return to the main menu ? ..

Last edited by adam_blackice; 09-15-2007 at 08:54 AM.
 
Old 09-15-2007, 10:16 AM   #4
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
Your script has three loops nested inside each other:

Code:
level 1 'while true; do ... done'
level 2                 'select i in $list; do ... done'
level 3                                        'select j in $list2; do ... done'
level 1 displays the main menu.
If you are in level 3 and you want to get back to the main menu you need to break out of 2 loops by using 'break 2'.
If you are in level 2 and you want to get back to the main menu you need to break out of 1 loop by using 'break 1'.
('break' means exactly the same as 'break 1')

I'm sorry, but I don't understand the rest of your question.
 
Old 09-15-2007, 11:27 AM   #5
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
thanks for your great support ,, really thanks

all i meant that i want to add to every menu another "back" option so that i can out from any menu to the higher level menu

u mean that will done by using break so if i want to get out of the if condition 2 to the one i will use break 1 and so on ?


thanks for your help
 
Old 09-15-2007, 01:09 PM   #6
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
The problem is that the menu on level 3 is only accessed through an if/then condition test, so you can't redisplay its menu without going back to the main menu and selecting 'mysql' again.

One solution is to add yet another loop
So now your script has four loops nested inside each other:

Code:
level 1 'while true; do ... done'
level 2             'select i in $list; do ... done'
level 3                         'while true; do ... done'
level 4                                     'select j in $list2; do ... done'
Code:
#!/bin/bash

#set the PS third  variable
PS3='Please Select a Choice from the Menu : '
list="mysql system-log apache-log quit"

while true; do
    select i in $list; do
        if [ $i == "mysql" ]; then
            list2="mysql-log mysql-start mysql-stop mysql-restart main-menu"
            while true; do
                select j in $list2; do
                    if [ $j == "mysql-log" ]; then
                        watch tail /var/log/mysqld.log
                        break 1
                    elif [ $j == "mysql-start" ]; then
                        service mysqld start
                        break 1
                    elif [ $j == "mysql-stop" ]; then
                        service mysqld stop
                        break 1
                    elif [ $j == "mysql-restart" ]; then
                        service mysqld restart
                        break 1
                    elif [ $j == "main-menu" ]; then
                        break 3
                    fi
                done
            done
        elif [ $i == "system-log" ]; then
            watch tail /var/log/messages
            break 1
        elif [ $i == "apache-log" ]; then
            watch tail /var/log/httpd/error-log
            break 1
        elif [ $i == "quit" ]; then
            exit 0;
        fi
    done
done
 
  


Reply


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 10:35 AM
Socket Programming making use of "select()" and "writefds" johncsl82 Programming 10 11-13-2011 12:27 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
searching "TIC TAC TOE" bash script LV-chronos Linux - Newbie 5 05-29-2005 02:20 PM
normal user want to perform "init 6" by using " sudo acbenny Linux - General 3 08-08-2004 07:41 AM


All times are GMT -5. The time now is 12:30 AM.

Main Menu
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