On this page:
4.1 Parsing
4.2 Running
6.9.0.6

4 Sh Lang

Sh lang is the mini language that is used inside any curly braces {}. This page goes into the details of how it works.

4.1 Parsing

We will use the following example:

{ls | grep rkt | sort -r > sorted.txt}

First since Racket uses pairs of |’s to quote names, we replace | with ’pipe before Racket gets a chance to parse it. Then we get a list with the following elements:

(ls ’pipe grep rkt ’pipe sort -replace > sorted.txt)

Then we split the list into a list of lists separated by ’pipes (we’ll call them run-lists), collecting any redirects into their own list. For each run-list, if the first element is an executable in the current path then we escape the list to:

(run (symbol->string ’the-first-element) (escape-args ’rest-of-the-list))

where escape-args wraps values with (!!local-or-string x), so the interpreter will check to see if x is a Racket definition or form, or otherwise treat it as a string.

So the grep run-list would look like:

(run "grep" (!!local-or-string rkt))

Once thats done for each run-list we collect them all into one list, add any redirects we found, and wrap it with a pipe call which is responsible for launching subprocesses and setting up file streams:

(pipe ’( (run "ls") (run "grep" (!!local-or-string rkt)) (run "sort" (!!local-or-string -r))) ’(!!local-or-string sorted.txt))

4.2 Running

When interpreting sh-lang, the interpreter looks for any (!!local-or-string x) pairs. Depending on x, one of the following will happen: