LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 10-02-2008, 07:13 AM   #16
dannemare
LQ Newbie
 
Registered: Oct 2008
Location: Malmö
Distribution: Debian GNU/Linux
Posts: 2

Rep: Reputation: 0

Quote:
Originally Posted by dannemare View Post
Well, Linus doesn't seem to agree entirely with you.
Read on.

And sorry for dividing my reply into two parts... Forum rules did not allow for me to post external links in my very first post.
 
Old 10-02-2008, 07:07 PM   #17
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,980

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
Bad coders make a mess out of the most structured language elements. Deeply nested loops and if statements can become nicely wound spaghetti, but still spaghetti.

Goto's certainly serve a purpose... if used with care.

Ever tried to write a function which has to be left immediately in multiple conditions, but with the execution of some statements, like closing a file or releasing a mutex? Without goto?

jlinkels
 
Old 12-15-2008, 01:41 AM   #18
dkho
LQ Newbie
 
Registered: Dec 2008
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by dannemare View Post
Well, Linus doesn't seem to agree entirely with you.
Goto is always considered bad style these days, when we have safer constructs like while, for, if-else, etc.

The problem is with the compiler or interpreter that generates code out of these constructs... they tend to generate too much redundant code. While GOTOs are considered fine at the assembly level, they are not encouraged when using higher-level languages...and these include shell scripts!! Shell interpreters are written in a higher-level language anyway...

If you think bash or csh (or other shells) aren't good enough for you, write another one that compiles (or interpretes) while-, or for-loop constructs that generates the least assembly (or binary) code.

This is probably the best way of using GOTOs, by writing a compiler (or assembler, interpreter, etc.) in assembly. There's a super elegant solution of using JMPs for 80x86.

Last edited by dkho; 12-15-2008 at 01:58 AM.
 
Old 10-04-2009, 01:37 PM   #19
mistofeles
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by dannemare View Post
Well, Linus doesn't seem to agree entirely with you.
I have a problem.
I have built a script which goes through many 'if' tests. About twenty of them. Some of them, about 12, exit, if they meet certain conditions.

The problem is that if the script just exits, it leaves the system in bad condition: devices mounted to wrong directories etc.
The script should run about ten lines of code while exiting.

How to do this without GOTO ?

In good old days before purist programmers condemned it, we used GOTO to make this easy in Basic and Fortran.
 
Old 10-04-2009, 01:50 PM   #20
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,208

Rep: Reputation: 147Reputation: 147
goto is good
the linux kernel source has tons for goto in it
 
Old 10-04-2009, 02:01 PM   #21
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,980

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
Tis is one of the cases "goto" is better than if statements... if use with care. But I already said that.

jlinkels
 
Old 10-04-2009, 02:41 PM   #22
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,724

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Use a 'trap' to run a cleanup routine before exiting:

Code:
## User cancelled at a 'sensitive' spot, so scold them!
trap_int() {
  echo
  echo $RED"*** OUCH!!! ***"$NORMAL
  emergency_restore
  exit
}

# emergency_restore
emergency_restore() {
echo "Please don't interrupt during installation!"
# do whatever fixups (unmounts, etc) here:

}

# set a trap like this in your code before running 'sensitive' code:
trap trap_int 2
# The above just traps -INT, but you can specify multiple signals to trap
As far as I know, you can't 'revoke' a trap, so if you only need it at certain places and not in others, you can use another 'dummy' trap which does nothing in order to cancel the one above.
 
Old 10-06-2009, 01:49 AM   #23
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Normally you write a finish() fn and call that as reqd.
In Perl programs I always have 2 subs (equiv to fns):

exit_with_success();
exit_with_failure();

I do the relevant cleanups in there.
 
Old 10-06-2009, 05:52 AM   #24
mistofeles
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 1
Linux bash use function instead of goto

Quote:
Originally Posted by gnashley View Post
Use a 'trap' to run a cleanup routine before exiting:...
Thank you. This 'trap' didn't help this time, but it cave me an idea.
Now I'm using a function 'quit', where I can write all the cleanup I need.
Using one variable to tell, if the calling test has gone err or is the system exiting normally.
The whole script is too large to be copied here, I'll publish it on my site, when it is more mature. Here is small pieces of it to show how:

This script calls the main script:
Code:
#-- Calling script--
MAKER=/home/bin/backup/make_snapshot.sh;

# Call the main script:
$MAKER;
if (( $? )); then {
  $ECHO "make_snapshot.sh script has been run" >>$ERROR_FILE;
  $CAT $ERROR_FILE | $SENDMAIL $RECEIVER;
} fi;
This script makes most of the work:
Code:
#--Piece from the beginning of the script--
#------- VARIABLES -----------
# Set ERR=1 for default:
ERR=1; 
#------- FUNCTIONS -----------
function quit {
 $UMOUNT $MOUNT_DEVICE;
 if (( $? )); then { 
   $ECHO "(1) ERROR: UNmounting $SNAPSHOT_RW" >>$ERROR_FILE;;
   # ERR=1; Not needed, default
 } fi;
 exit $ERR;
}

#------- The MILL ----------
# make sure we're running as root
if (( `$ID -u` != 0 )); then {   
   $ECHO "(2) ERROR: Must be root.  Exiting." >>$ERROR_FILE;;
   # ERR=1; Not needed, default
   quit;
} fi;

# ERR is '1' by default, change it to '0' when everything is OK:
ERR=0; quit;
# That's all, folks !
Some might wonder, why the scripts live in /home/bin/Backup rather than /usr/bin or some 'normal' directory.
This is because I have a habit to have three HD:s in my servers: one for the system, another for the /home and the third, the largest for the /BACKUP. I move /root, /var/www to /home and so it is MUCH easier to update system, make backups and recover broken systems. It is enough to backup /home and /etc.
It is very near I could make the whole system disk RO.
 
Old 10-06-2009, 06:01 AM   #25
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by tunasashimi View Post
Go HUG a tree or shag a dolphin or something.

Computers WORK in "GOTO"s. Whiles, procedures and case structures are mental masturbation.
The next Bill Gates has just introduced himself.

Go To Statement Considered Harmful
-- Edsger W. Dijkstra
 
Old 12-20-2010, 05:54 AM   #26
Xmister
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
Let's say you have a function which initialized a few pointers, but those are only needed in that function.
You have to return from the function in let's say 20 if statement.
You have 3 choices:
- Free the pointers in every statement before return. This is BAD. If you add a new pointer, you have to write 20 new line!
- Write a function that will be called by this function. Still not too good. Our code will be fulled with "func1_quit", "func2_quit", etc unneeded functions, which just makes it more unreadable and robust. (You are trying to understand a function, every time you see the ..._quit called, you have to find it in the code, then go back to our function)
- Write a goto that will jump to the end of the function where all the needed destruction statements are there+you will have only 1 return. This way the code will be the MOST readable of every cases. That's why the linux kernel developers are using this method.

The teachers are saying "Don't use goto" just to learn the procedural thinking well, because goto is just extends this practise, and doesn't replace it!
The evolution of a programmer in terms of goto looks like:
-Using goto a lot of places (where he shouldn't)->BAD
-He learnt the goto is bad, won't use it anymore->Better, but still far from professional
-He realizes that goto can be good too, and using it where it makes the code more readable and easily maintenable->It's a good start..
 
Old 12-21-2010, 06:40 AM   #27
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
Quote:
Originally Posted by patsprice View Post
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.
Yeah, me too. What about using REXX or ooREXX for your client then - AFAIR there exist labels and the SIGNAL instruction. There are free implementations of it for Linux.
 
Old 12-22-2010, 08:30 AM   #28
patsprice
LQ Newbie
 
Registered: Nov 2006
Location: Stevensville MD
Distribution: Red Hat, Ubuntu, Debian
Posts: 4

Rep: Reputation: 0
Like I said in one of the posts above, I worked out a way to have restart steps. I did this in 2006/2007 and it all worked sweetly, like a charm, I even simulated GDGs is Korn shell with the ability to roll back and do batch reruns. I have already forgotten most of the details and moved on to newer adventures. Let me know if you have questions and I can try to help.
 
Old 03-20-2013, 06:35 PM   #29
miriam-e
LQ Newbie
 
Registered: Nov 2011
Location: QLD, Australia
Distribution: Puppy Linux
Posts: 10

Rep: Reputation: Disabled
It is weird to see people still repeat the myth that GOTOs are bad. It all comes from a letter written by Edgar Dijkstra to the ACM way back in 1968. It was titled "Goto Considered Harmful". I think most people simply read the title and didn't bother reading the whole two pages.

He was an early proponent of structured programming, and while he disliked the excessive use of GOTOs he actually felt they have a rightful place in programming. Unfortunately the tendency of people to reduce complex, balanced messages into quick, memorable, one-line summaries meant that many came to wrongly dismiss GOTO as bad practise. This terrible error in judgement has meant that generations of programmers must write complex, impenetrable code to replace a single, simple GOTO when it would have been the most sensible solution.

I've never understood people's propensity for elevating random statements by "important" people into the status of religious doctrine, and then for others to repeat that doctrine without question. It is as if some master carpenter came to the personal belief that hammers and nails are bad and that they should be replaced by screws and glue. But the fact is that hammers and nails are the appropriate tool for certain jobs. Likewise the GOTO is the appropriate tool in a rich toolbox of programming techniques. We are made poorer by its denial.

Bash scripts could often be made much simpler, faster, and easier to read by judicious use of GOTO. It is a great pity that it was omitted.

Additional:
I just found an annotated version of Dijkstra's article that enables people to wade through his awful writing style. You will be able to truthfully say you're one of the few who actually read the damned thing. It is here:
http://david.tribble.com/text/goto.html

Last edited by miriam-e; 03-20-2013 at 08:29 PM.
 
Old 03-20-2013, 09:13 PM   #30
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 505
Blog Entries: 2

Rep: Reputation: 67
The bad image of GOTOs was probably reinforced by the early BASICs which used line numbers and lacked WHILE-WEND or REPEAT-UNTIL statements. As a result, users of these languages had to make clumsy representations of those statements by using GOTO statements instead (ever tried programming the Commodore 64?)

Nicholas Wirth probably had the right idea. You can use GOTOs if you wish but you have to do extra work first by setting up and declaring labels before you can use them.
 
  


Reply

Tags
bash, mac


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 05:19 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