LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-01-2007, 08:56 PM   #1
mcdrr
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Rep: Reputation: 0
BASH Need help figuring out how to loop SELECT statement


Hello everyone,

I was hoping that someone could shed some light/point me in the right direction with a script that I am working on. For starters I am new to scripting so the code may not the best way to accomplish what I am trying to do but like I mentioned I am in the process of learning how to script.

here is a copy of the script. What I am trying to do is if there is a way to loop/reprint the SELECT menu after an option is selected for example if I select option 3 I get the results which works fine, but then I want the script to automatically reprint the option list over and over until the QUIT option is selected. For now the way that I am doing it is by having it echo in the script manually.

If I do "elif [ $answer = "Menu" ]
then
echo $CHOICE


I get the choices to print out but in this format
System Secure Cron Menu Quit


not like this.
1) System
2) Secure
3) Cron
4) Menu
5) Quit
Please select one of the options above for Menu press 4:



Is there anyway to accomplish this? Thanks for all the help in advance. Please let me know if you guys have any questions.



COPY OF SCRIPT
-----------------------------------------------------------------------------------
#!/bin/bash
# Date:
# Author:
# Purpose:


PS3='Please select one of the options above for Menu press 4:'
CHOICE="System Secure Cron Menu Quit"
select answer in $CHOICE
do
if [ $answer = "System" ]
then
tail /var/log/messages
elif [ $answer = "Secure" ]
then
tail /var/log/secure
elif [ $answer = "Cron" ]
then
tail /var/log/cron
elif [ $answer = "Menu" ]
then
echo "1) System"
echo "2) Secure"
echo "3) Cront"
echo "4) Menu"
echo "5) Quit"
elif [ $answer = "Quit" ]
then
exit
fi
done
----------------------------------------------------------------------------------
 
Old 05-07-2007, 06:50 AM   #2
Oupa
LQ Newbie
 
Registered: Apr 2007
Location: Welkom, South Africa
Distribution: RHL + Mandriva
Posts: 15

Rep: Reputation: 0
Include the following lines with your script:

loop=0
while [ $loop = 0 ]
do
YOUR SCRIPT CODING COMES HERE
.......................
...................
done
exit 0
----------------------------

Modify your quit option to break the loop:
elif [ $answer = "Quit" ]
then
loop=1
break
fi
-----------------------------
More readable would be to use the case statement.
The coding would be something as follows:

#!/bin/bash
showmenu()
{
clear
cat << !menu.txt

*** Menu of Options ***

1 System
2 Secure
. ...
5 Quit

!menu.txt
}
loop=1
while [ $loop -eq 1 ]
do
showmenu
printf "\n Enter choice: "
read opt
case $opt in
1) tail /var/log/messages
;;
2) tail /var/log/secure
;;
.) ...
;;
5) loop=0
break
;;
*) printf " Invalid Choice\n"
;;
esac
printf "\n Press Enter to return to Menu"
read junk
done
exit 0
----------------------------
Brief explanation:
The showmenu funtion is read but not executed.
The while loop executes showmenu
showmenu uses the cat command to display the menu section.
Control returns to the loop at the:
printf "\n Enter choice: "
The choice is read and depending on what number was choosen the 'case' statement executes the relevant coding. The case statement ends with esac.
The While loop continues to execute until choice 5 is made.
---------------------------------------------
 
Old 05-08-2007, 04:47 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
yes, use the case statement in the select, something like:
Code:
case $choice in

       this) do_this
                
                ;;
       that) do_that
                ;;

        *wildcard* ) etc
                  ;;
        *)  # default
            do_thing
            ;;
esac
you can use break to get out of loops, and even qualify the nesting
depth.

I personally just use CTRL-C to get out of select cos I am lazy.
 
  


Reply

Tags
bash, loop, while


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Does CGI/PERL have a Select Case Statement? craig467 Programming 2 02-16-2007 04:33 PM
select statement not work in php with OCIEXEcute() suchi_sood Programming 9 04-12-2006 06:22 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
Bash select statement and spaces meshcurrent Linux - General 2 04-09-2003 09:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 AM.

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
Open Source Consulting | Domain Registration