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 08-20-2007, 06:08 PM   #1
UnitedWeFall
LQ Newbie
 
Registered: Aug 2007
Location: New Zealand
Distribution: Ubuntu
Posts: 5

Rep: Reputation: 0
Bash Loops


Hey guys, i've been searching all over the internet to try and figure this one out, and I stumbled across this forum. It seemed like a good place to get some help

I'm new to Linux and Bash, and i'm trying to write my first proper script at the moment. However i'm stuck trying to create a working loop for a case statement.

It goes something like this:

Code:
echo "Selection 1"
echo "Selection 2"
echo "Selection 3"

echo -n "Enter selection"
read x

case $x in
1) #Selection 1 code here
;;
2) #Selection 2 code here
;;
3) #Selection 3 code here
;;
*) #Default code here
esac
Basically i've been trying to write a loop that lets me go back to the start of the program so the user can input another selection once their first selection has been displayed.

I tried using something like:

Code:
loop=0
while [$loop = 0 ]
do
....code....
done
But that just loops the program endlessly.

Is there any simple way to achieve what I'm wanting to do?

Any help is much appreciated.
 
Old 08-20-2007, 06:28 PM   #2
tjyorkshire
Member
 
Registered: Jun 2007
Location: UK
Distribution: openSUSE 10.2
Posts: 138

Rep: Reputation: 15
use functions, for example:

Code:
function <name> {
}
and put the contents of the function in the brackets. Then you can call up the function at any time by using the function name. Its probably the easiest way of reusing code

Hope this helps

Last edited by tjyorkshire; 08-20-2007 at 06:30 PM.
 
Old 08-20-2007, 06:36 PM   #3
UnitedWeFall
LQ Newbie
 
Registered: Aug 2007
Location: New Zealand
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tjyorkshire View Post
use functions, for example:

Code:
function <name> {
}
and put the contents of the function in the brackets. Then you can call up the function at any time by using the function name. Its probably the easiest way of reusing code

Hope this helps
You're saying I can define the entire case statement as a function then call it back when I need to?

I just tried doing that but I got an unexpected end of file error on the line where I closed the function bracket (after 'esac')
 
Old 08-20-2007, 06:45 PM   #4
tjyorkshire
Member
 
Registered: Jun 2007
Location: UK
Distribution: openSUSE 10.2
Posts: 138

Rep: Reputation: 15
Actually i was originally thinking of putting the menu bit in a function like this:
Code:
function mainmenu { 
echo "Selection 1"
echo "Selection 2"
echo "Selection 3"

echo -n "Enter selection"
read x
}
But thinking about it now, its not really want you wanted or needed for the current script. Sorry about that
 
Old 08-20-2007, 06:48 PM   #5
UnitedWeFall
LQ Newbie
 
Registered: Aug 2007
Location: New Zealand
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Yeah I don't think that'd work, as the entire case statement needs to run again. Thanks anyway
 
Old 08-20-2007, 06:54 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,118
Blog Entries: 54

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
Basically i've been trying to write a loop that lets me go back to the start of the program so the user can input another selection once their first selection has been displayed.
That's a case for "select" me thinks:
Code:
codeblock_nil() { echo "doSomething"; }
codeblock_one() { echo "doSomethingElse"; }
codeblock_two() { echo "doSomethingCompletelyDifferent"; }

select name in quit "Selection 1" "Selection 2" "Selection 3"; do
 case "$name" in
  quit) break
       ;; 
  "Selection 1") codeblock_nil
       ;;
  *2) codeblock_one
       ;;
   *) codeblock_two
       ;;
 esac
done

Last edited by unSpawn; 08-20-2007 at 06:57 PM. Reason: REM Added some functions so you dont have to wedge everything in the case statement.
 
Old 08-20-2007, 07:01 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,203

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
while [ $x != "quit" ]
do
.
.
.
done

Have a look on tldp.org for the abs (says advanced, but isn't really)

Edit: man, I gotta learn to type faster ...
As usual, always more than one answer - at least we both agree on "quit"

Last edited by syg00; 08-20-2007 at 07:04 PM.
 
Old 08-21-2007, 06:18 AM   #8
UnitedWeFall
LQ Newbie
 
Registered: Aug 2007
Location: New Zealand
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for that. I'll give it a go now.
 
Old 08-21-2007, 06:33 AM   #9
UnitedWeFall
LQ Newbie
 
Registered: Aug 2007
Location: New Zealand
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Hmm nope can't get any of it to work. I just end up getting "unexpected end of file" errors :S

Might have to spend a bit of time looking at my code.
 
  


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, LS, For loops, and whitespaces in directories jhrbek Programming 27 09-22-2010 05:17 AM
Bash For Loops gives syntax error meadensi Linux - Newbie 2 02-23-2005 10:30 AM
bash scripting and loops phoeniks Programming 5 01-24-2005 04:00 PM
bash, loops and spaces in filenames shy Programming 5 11-08-2004 07:43 AM
bash - while + until loops grouping? trees Linux - General 2 02-19-2004 02:29 PM


All times are GMT -5. The time now is 01:22 PM.

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