Using a Command Language as a High Level Programming Language
My name is Gwendolyn James, and I'm writing these newsletters to help your growth, while supporting my DevOps and Infrastructure teaching practice. Feel your brain switching off at work? Let's switch it back on. Come hang out in Slack and let's talk code!
I'll be making a small change to my newsletter format. I've been writing one large email every 8ish weeks; I'm going to start writing 2-3 smaller emails about the same paper, and send them out a bit more frequently.
This is part 1 of a writeup of "Using A Command Language as a High Level Programming Language", by John Mashey.
What's the paper's background?
John Mashey was writing in 1976, as part of the Programmer's Workbench1 team. The paper is about how his team was inventing in the new space of Command Languages2, guided by the value it was delivering to the teams at AT&T.
His team added constructs like if/then/else/endif, goto, assignment of environment variables and more, as command line binaries (not built into the shell). I'll write about how that works, because it's illuminating and changes your perception of Unix and shell.
How did goto Work?
This isn't goto like in the C programming language:
Using "goto label" causes the following actions:
- A seek is performed to move the read/write pointer to the beginning of the command file.
- The file is scanned from the beginning, searching for ": name" on a line, either alone or followed by a blank or a tab.
- The read/write pointer is adjusted (via the seek) to point at the line following the labeled line.
Thus, the only effect of goto is the adjustment of the shell's file read/write pointer to cause the shell to resume interpreting commands starting at the line following the labeled line.
from the paper
Here's a section of a Ken Thompson script that features goto. For a deeper look at the script, John Arundel and I discussed it on YouTube recently:
if ! -r l.s -o ! -r c.c goto bad
as l.s
....[many lines redacted for clarity]
exit
: bad
echo l.s or c.c not found
The implementation of this goto leans on the Unix process model. The shell executes goto, and so it's a child process of the shell. Child processes inherit exactly their parents' file descriptors, including Standard Input.
It's easy to skip past what a revelation this should be for your own shell programming. The child's file descriptors are the parent's file descriptors. Manipulating one impacts the other.
goto then starts iterating over Standard Input. This moves the shell's own seek pointer forwards. It moves the seek pointer to 0, 0, 0 at the beginning of the file, then forwards until it finds a comment line with a matching label.
The implementation is old C, not ANSI C, but is still readable at goto.c. If you want to walk through it with a friendly person, Slack and let's talk code!
The idea of swapping control of shared resources back and forth between shell and binaries makes the shell a Swiss Army chainsaw. You can use the right language for a job, and pass control back and forth between different best-in-class tools.
It makes one sympathetic, really, to Larry Wall's invention of Perl.
What's In Part Two
The next part will be about a question Mashey raises: why are string-related tools so much more popular than arithmetical tools in shell?
P.S. Come join my Slack! It's a chill place for DevOps and Software engineers to learn from each other.
Links
in the mistake there is the improvement because it is in that exact moment your brain is somehow looking for an escape room, how to get out from the problem and then it comes to the invention.
Be Comfortable Being Uncomfortable - developers are seeking meaning right now. This video interview with an Italian artist is about being bold enough to pursue professional vision and understanding, when no good outcome is guaranteed.
Historical Shell Scripts Read Through Get to grips with the script that calls goto in this email! The incomparable John Arundel and I walk through a couple of shell scripts from the 1970s, written by Dennis Ritchie and Ken Thompson.
[1] Linux has many flavours, like Ubuntu or Fedora or Slackware. Unix had them too; one was Programmers' Workbench. ↩
[2] Your shell is a command language interpreter; the commands and basic programming constructs are the Command Language. ↩