ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
"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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.