This reminds me of the Thompson shell goto, which is an external(!) program that messes with its parent's file descriptors looking for `: label`.<p>See manpage at <a href="https://etsh.nl/man/_goto.1.html" rel="nofollow noreferrer">https://etsh.nl/man/_goto.1.html</a> and source code at <a href="https://github.com/eunuchs/tsh/blob/master/goto.c">https://github.com/eunuchs/tsh/blob/master/goto.c</a>.<p>See, history can give you a more inert syntax. And maybe a new way of thinking about how to make this thing... I would love to have a more robust version to do C-style goto cleanups.
Since he mentions this being for a work thing, how I've handled a similar situation (long running shell thingies) is:<p>Break up the script into steps, and place them in order in a dot-d style folder, like 1.foo.sh, 2.bar.sh, 3.baz.sh. Have a separate _context.sh which supplies the upfront variables or whatever are needed for each step. Ensure that each step can run on its own with just the context sourced. Then have a run.sh in the folder above that sources the context and the steps in order.<p>Now, that said, typically these kinds of things are dataflow or even buildsystem-adjacent, so I've also experimented with doing them in CMake (intermediate products managed with add_custom_target) or as Nix derivations (intermediate products are input-addressed in the Nix store), but I'd love to be aware of other tools that are better for expressing this kind of thing.
He writes "prepare to cringe" and he is not wrong. As far as I understand, this technique implements GOTO by reading the source code (again) into memory, filtering out everything before the target label and evaluating the remaining source code. I think this doesn't preserve state, variables etc. so not really a GOTO. But interesting technique.<p>edited for clarity
It's a funny trick, but you could probably also use setjmp and longjmp with ctypes.sh :-)<p><a href="https://github.com/taviso/ctypes.sh">https://github.com/taviso/ctypes.sh</a>
Instead of:<p><pre><code> sed -n "/$label:/{:a;n;p;ba};"
</code></pre>
I think it's more idiomatic to do:<p><pre><code> sed -n "/$label:/,$ p"
</code></pre>
Or even:<p><pre><code> sed "0,/$label:/,$ d"
</code></pre>
Which deletes the label itself, so you don't need the subsequent `grep -v ':$'`, but then you also aren't allowed to put any statements on the same line as the label.
Honestly, I <i>love</i> this.<p>Precisely because there's too much gatekeeping in programming and learning etc. Give people sharp knives and let them break things
BAT files writing other BAT files was often the only way to get scripting workflows done on primordial Windows versions, but chaos typically ensued if you attempted to rewrite the file you were presently running, as it appears that cmd didn't do anything fancy like read the whole file into memory before processing it (presumably due to the draconian memory limits people laboured under back then)
> It runs sed on itself to strip out any parts of the script that shouldn’t run, and then evals it all.<p>How I have done this is:<p>1. Put all the steps in functions.<p>2. Have a main function that calls all the other functions in order.<p>3. If given an argument, the main function skips all the functions up to that one.
If you're going to do that, you might as well go the whole hog and implement INTERCAL's "come from"[1] for maximal evil.<p>[1] <a href="https://en.wikipedia.org/wiki/COMEFROM" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/COMEFROM</a>
the author mentions in a note that bash was complaining and that they might put the labels in comments to dodge the issue. They might also be able to change the label format to `: label`. `:` just returns true regardless of what parameters you pass it, so it could still look "labelish" without having to use an actual comment.
That's one of the few places where it would be appropriate to store the current execution point somewhere in /var/cache or /var/lock and write the script so that it would look there at launch and dispatch accordingly.
"GOTO is considered harmful". This kind of thinking just makes me want to run off screaming into a paper bag. Language developers are, in many ways, the modern aristocrats telling us what we can't or should not do. I still miss assembly language.
I had mostly done some short stuff in bash that does not need much flow controls and loops etc; most programming I will use C instead, which does have goto and is sometimes useful even though usually the other flow controls are better. My own programming language designs do have a goto command because it is sometimes useful; the Free Hero Mesh programming language has both goto and gosub, in addition to the structured flow controls (if/then, begin/while/repeat, etc).<p>The way is done in that article doesn't seem best way to do; modifying the shell itself to seek the shell script file for it seem to be better in my opinion, or perhaps using the "enable" command to add your own built-in "goto" command (although I don't know if the API supports that). Another message on here mentions an external program messing with the file descriptors, but does bash use a consistent file descriptor number for the shell script file with which this will work?
That's neat but doesn't `case` support fallthrough? So I expect you could just put your script in one big `case` statement and skip to the branch you need.
And I did cringe, and then I thought it looked kinda fun. It would literally never have occurred to me in a million years to try to start a shell script half way though - so trapped am I in the paradigm of the familiar.<p>As for the script that takes several days and often breaks half way through... sounds like what Makefiles are for to me.
after many years, I learned that usually shell scripts are only good for the most basic of uses. here is a similar program in Go that doesn't require any hacks:<p><pre><code> package main
import "os"
func main() {
var x int
switch len(os.Args) {
case 1: goto start
default:
switch os.Args[1] {
case "foo": goto foo
case "mid": goto mid
}
}
start:
x = 100
goto foo
mid:
x = 101
println("This is not printed!")
foo:
if x == 0 { x = 10 }
println("x is", x)
}</code></pre>