LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Loops (http://www.linuxquestions.org/questions/programming-9/bash-loops-578535/)

UnitedWeFall 08-20-2007 06:08 PM

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. :)

tjyorkshire 08-20-2007 06:28 PM

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

UnitedWeFall 08-20-2007 06:36 PM

Quote:

Originally Posted by tjyorkshire (Post 2865127)
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')

tjyorkshire 08-20-2007 06:45 PM

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

UnitedWeFall 08-20-2007 06:48 PM

Yeah I don't think that'd work, as the entire case statement needs to run again. Thanks anyway ;)

unSpawn 08-20-2007 06:54 PM

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


syg00 08-20-2007 07:01 PM

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" ;)

UnitedWeFall 08-21-2007 06:18 AM

Thanks for that. I'll give it a go now.

UnitedWeFall 08-21-2007 06:33 AM

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.


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