LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH goto (http://www.linuxquestions.org/questions/programming-9/bash-goto-384407/)

dkrysak 11-18-2005 02:26 PM

BASH goto
 
Is there a way to go to a particular line within the flow of a script?


IE I have a case statement, and at the end I want to start the script all over (in a sense going back to my menu).

gnashley 11-18-2005 03:01 PM

Not a good idea to use GOTO in BASH. Try using a while statement instead, or use functions.

urka58 11-19-2005 05:27 PM

You're probably looking for something like this I guess

#!/bin/sh

echo "Question ?"

read x

menu () {
case $x in
a|b)
commands
menu
;;
c|d)
commands
menu
;;
exit|quit)
exit 0
;;
*)
echo "valid responses are a b c d exit quit"
menu
;;
esac
}

menu

Hope this helps
Ciao

tunasashimi 09-09-2006 05:14 AM

UGh
 
Quote:

Originally Posted by gnashley
Not a good idea to use GOTO in BASH. Try using a while statement instead, or use functions.

Go HUG a tree or shag a dolphin or something.

Computers WORK in "GOTO"s. Whiles, procedures and case structures are mental masturbation.

They're all compiled to a bunch of goto's anyway.

Bash doesn't support goto.

Make a loop with a break condition;

bash_suck_level=2001;
while [ bash_suck_level -gt 2000 ]; do
blah
blah
if [ bash crap ]; then
bash_suck_level=1999;
fi
done;

..There is a "break" command, maybe that will work too... anybody?

randyding 09-09-2006 02:34 PM

Quote:

Go HUG a tree or shag a dolphin or something.

Computers WORK in "GOTO"s. Whiles, procedures and case structures are mental masturbation.
YES!
Please may I quote this in my next code review when our lame "goto" police complain again?
Nice!

uselpa 09-09-2006 05:03 PM

Quote:

Originally Posted by tunasashimi
Computers WORK in "GOTO"s. Whiles, procedures and case structures are mental masturbation.

I feel so satisfied... haven't used a single GOTO since 1990 :D

BTW, if you like GOTOs, try using ALTER in COBOL - you'll like it. Read this and enjoy.

tunasashimi 09-11-2006 08:48 PM

Quote:

Originally Posted by randyding
YES!
Please may I quote this in my next code review when our lame "goto" police complain again?
Nice!

LOL, "goto" police hahaha, they all use one-button mice too, and PC's that look like soap.

patsprice 11-16-2006 12:53 PM

Quote:

Originally Posted by tunasashimi
LOL, "goto" police hahaha, they all use one-button mice too, and PC's that look like soap.

I am converting JCL to shell script (for a client) and need to simulate the IBM JCL RESTART command. I learnt to program in COBOL and PL/1 over 20 years ago and agree that using goto to create spaghetti is bad but goto can be a lifesaver too when used judiciously. I absolutety never say never ;) Guns don't kill people, people kill people. Goto does not write spaghetti, people write spaghetti.

I can use goto to place labels in the script and just check the Step number of the RESTART parameter and jump straight to it using the Goto statement and continue from that point. Except the Korn Shell won't let me.

Case will take me there but only to one section and in a 300 line script with 15 steps it is horrible to read and follow.

Since ksh does not support goto (David Korn, what were you thinking?) I am going to kludge a workaround and retrofit all the scripts we did so far. What a pain. Maybe I should just switch to Bourne or bash.

matthewg42 11-16-2006 01:14 PM

tunasashimi considered dangerous.

osvaldomarques 11-16-2006 05:11 PM

Hi gentlemen,

Bash doesn't have goto. I know that for few cases it would be good.

I want to humbly talk what I'm thinking about the posts.

First in the case of the original post, the ideal is a while loop: the example presented by urka is recursive, that means, each menu presentation would be called by the prior. In an infinite while loop, someone could decide to exit using break command.
Code:

while :
do
  clear
  echo -n 'Questions? '
  read x
  switch $x in
    a)
      echo a
      ;;
    b|c)
      echo b or c
      ;;
    q)
      break
      ;;
    *)
      echo Invalid command \"$x\"
      ;;
  esac
done

To patsprice, you could divide your job into functions, each one containing a portion which you need to reprocess on each break point; that is, where you have a label or anything equivalent, you introduce a procedure declaration with a name composed of a fixed part and the number of the breakpoint, as the example above:
Code:

#!/bin/sh

# The file which will save the current position in the job
BREAKPOINT=/mydir/breakpoint
MAXPROCEDURE=3
#
# All the global variables should be declared here
#

save_break_point()
{
  echo $BP >$BREAKPOINT
}

procedure_1()
{
  # First part of the job
  echo procedure 1
}

procedure_2()
{
  # Second part of the job
  echo procedure 2
}

procedure_3()
{
  # Third part of the job
  echo procedure 3
}

#
# Initial state
[ -e $BREAKPOINT ] && BP=`cat $BREAKPOINT | sed 's/^\([0-9]*\).*$/\1/'`
[ "$BP" = "" ] && BP=1

# Process each function based on the breakpoint

while [ $BP -le $MAXPROCEDURE ]
do
  procedure_$BP
  save_break_point
  BP=`expr $BP + 1`
done
[ -e $BREAKPOINT ] && rm -f $BREAKPOINT

Here, procedure_1 is the first part of the job, where there isn't restart, procedure_2 must be executed if procedure_1 was done and the control logic is at the end, working as an outer loop.

tunasashimi 11-16-2006 05:26 PM

Osval, that is just !@)#( Beautiful. You're a genius! That while solution must be the best...

But nice work on the goto. "echo 3 >$BREAKPOINT". Hahaha. HAHHAHAHAHA. MU HA HA. Now thats creative!

(I'd love to count the amount of redundant code bash churns out on that....like a 4x4 Stuck in mud, spinning, the CPU pushing!)

Hurray, 2 months later and we have an elegant solution. USE WHILE. *Sigh* Bash. Yes, bash your head against the table.

:tisk:

Now I'm on to wondering about performance benchmarks comparing different scripting languages....

I know if I saved a "BP" variable to a file in GWBASIC 20 years ago, it would go terribly slow. Sure, bash knows how to cash..sorry Cache. :D But, Hmm.. :o

Anybody knows of a good site that pits Bash, Perl, Zerl, Wurl, Curl :scratch: I'm polluting this thread, aren't I.

What I'm really wondering is... Say I run a regular BASH script that reads and writes a lot to and from files, on an 8086, from a floppy. How slow would it be, and what's a better alternative for an interpreted / scripted language?

:cry:

osor 11-16-2006 08:54 PM

Just thought I might quote one of the co-creators of UNIX:

"If you want to go somewhere, goto is the best way to get there."
--Ken Thompson

patsprice 11-17-2006 07:57 AM

Quote:

Originally Posted by osvaldomarques
Hi gentlemen,

Bash doesn't have goto. I know that for few cases it would be good.

I want to humbly talk what I'm thinking about the posts.

First in the case of the original post, the ideal is a while loop: the example presented by urka is recursive, that means, each menu presentation would be called by the prior. In an infinite while loop, someone could decide to exit using break command.

[Code removed for brevity]

Here, procedure_1 is the first part of the job, where there isn't restart, procedure_2 must be executed if procedure_1 was done and the control logic is at the end, working as an outer loop.

I was over-hasty when I said I should use goto in any of the modern shells, they seem to have exercised their control by removing user choice.

Of all the responses posted I tend to agree with tunasashimi most.

Since the developed code is already working and tested I did not want to mess with the logic by adding complexity such as conditional breaks. The steps are sequential ascending, so in the end I added a MAXSTEPS=nn, Wrapped each section in a function like this (where nn is a sequence):
Stepnn()
{
Code
}
and (pseudocode*) set up a loop at the end to initialize STARTSTEP = 1 or the RESTART parameter (if there is one), then loop i++ from STARTSTEP to MAXSTEPS calling the functions with a variable e.g. STEP${i}

I have to say it would have been MUCH easier to check for RESTART paramater and goto LABEL. And much more elegant and required much less debugging, since I have some junior scriptkiddies working on the project.

Thanks, Osor. And Ken Thompson is 100% right.

siya 04-05-2008 05:01 PM

Quote:

"If you want to go somewhere, goto is the best way to get there."
--Ken Thompson
Sorry for digging up an old thread.

With keen respect towards ken Thompson,

A few decades ago, if you wanted to go somewhere a cart was supposed to be the easiest and fastest way.. :-D

Goto is no doubt easy-to-use and addictive, but successful programmers (in the modern era) seem to be skipping it themselves and also advising others to.

dannemare 10-02-2008 08:11 AM

Quote:

Originally Posted by siya (Post 3111948)
Goto is no doubt easy-to-use and addictive, but successful programmers (in the modern era) seem to be skipping it themselves and also advising others to.

Well, Linus doesn't seem to agree entirely with you.


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