LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 11-18-2005, 01:26 PM   #1
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Rep: Reputation: 15
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).
 
Old 11-18-2005, 02:01 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Not a good idea to use GOTO in BASH. Try using a while statement instead, or use functions.
 
Old 11-19-2005, 04:27 PM   #3
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 15
Posts: 546

Rep: Reputation: 43
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
 
Old 09-09-2006, 04:14 AM   #4
tunasashimi
Member
 
Registered: Jun 2005
Posts: 82

Rep: Reputation: 15
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?

Last edited by tunasashimi; 09-11-2006 at 07:49 PM.
 
Old 09-09-2006, 01:34 PM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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!
 
Old 09-09-2006, 04:03 PM   #6
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
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

BTW, if you like GOTOs, try using ALTER in COBOL - you'll like it. Read this and enjoy.
 
Old 09-11-2006, 07:48 PM   #7
tunasashimi
Member
 
Registered: Jun 2005
Posts: 82

Rep: Reputation: 15
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.
 
Old 11-16-2006, 11:53 AM   #8
patsprice
LQ Newbie
 
Registered: Nov 2006
Location: Stevensville MD
Distribution: Red Hat, Ubuntu, Debian
Posts: 4

Rep: Reputation: 0
Angry

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.
 
Old 11-16-2006, 12:14 PM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
tunasashimi considered dangerous.
 
Old 11-16-2006, 04:11 PM   #10
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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.

Last edited by osvaldomarques; 11-16-2006 at 04:13 PM.
 
Old 11-16-2006, 04:26 PM   #11
tunasashimi
Member
 
Registered: Jun 2005
Posts: 82

Rep: Reputation: 15
Wink

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.



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. But, Hmm..

Anybody knows of a good site that pits Bash, Perl, Zerl, Wurl, Curl 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?


Last edited by tunasashimi; 11-16-2006 at 04:31 PM.
 
Old 11-16-2006, 07:54 PM   #12
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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
 
Old 11-17-2006, 06:57 AM   #13
patsprice
LQ Newbie
 
Registered: Nov 2006
Location: Stevensville MD
Distribution: Red Hat, Ubuntu, Debian
Posts: 4

Rep: Reputation: 0
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.
 
Old 04-05-2008, 04:01 PM   #14
siya
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Rep: Reputation: 0
Wink

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.
 
Old 10-02-2008, 07:11 AM   #15
dannemare
LQ Newbie
 
Registered: Oct 2008
Location: Malmö
Distribution: Debian GNU/Linux
Posts: 2

Rep: Reputation: 0
Wink

Quote:
Originally Posted by siya View Post
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.
 
  


Reply

Tags
bash, mac



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
goto/label command for scripting in bash shell terry.trent Linux - Software 3 07-09-2010 10:15 AM
can you use goto in bash script sabliny Programming 3 10-07-2005 05:54 PM
goto usage bru Programming 5 03-09-2004 03:44 AM
Goto command? batfoot Linux - General 4 08-26-2003 07:17 PM
GOTO function in bash? sobchak Programming 1 07-22-2002 05:10 AM

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

All times are GMT -5. The time now is 04:12 PM.

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