[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9. Commands

The macro run is used to run a program:
run ls -l ../lib/*.o
The macro exec is like run, but it does an execve without a prior fork.

An implicit run is added before an undeclared identifier that matches an executable in PATH:
ls -l ../lib/*.o
Note that the run is inserted at macro-expansion time, not at run-time. Hence the program (here ls) must be in the PATH both at compile-time and run-time. (Otherwise, the compiler would not know whether to compile -l as subtraction or to quote it.)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.1 Standard input and output

If we think of running a program as a kind of function call, it is natural to think of a pipeline as function composition: The output of one function is used directly as an input of another. It is reasonable to view standard input and output abstractly as strings.

A program has two sets of inputs: The command line arguments (passed in argv), and standard input. Q allows infix functions, with a varying number of right parameters. We will interpret the left argument (if any) as standard input to the program, and the right arguments will be passed as argv. The left argument will be evaluated, while the right arguments will be quoted.

We can pass a string to a program:
Q1> "Hello World!\n" run /usr/bin/tr a-zA-Z A-Za-z

The stdin argument need not be a string, but can be any printable object:
Q2> 250 for 10
250 251 252 253 254 255 256 257 258 259
Q3> (250 for 10) tr 0-9 1-90
361 362 363 364 365 366 367 368 369 360

Input can be a pathname object:
Q4> ./Makefile wc
     199     848    6895
In that case, no pipe or fork is needed: The file is just opened for input, and the file descriptor is passed to the program. This is standard input redirection.

Similarly, the output of a command can be saved as a string:
Q1> :S=(ls -ld ../test)
Q2> sprintf "(%#s)" S
("drwxrwxr-x  2 bothner       512 Jul 25 17:41 ../test\n")
(The format `%#s' means to print the argument in a form that can be read back in, with appropriate escapes.)

Finally, the composition of two programs is a pipeline:
Q3> ls -ld ../test) tr a-zA-Z A-Za-z
DRWXRWXR-X  2 BOTHNER       512 jUL 25 17:41 ../TEST
Notice the unbalanced parenthesis; this is a convenience that is allowed for interactive input only.

Generalized output redirection has the form:
>filename expression
This evaluates expression, but prints the result on filename. The expression can be any expression, not just ones that run programs: For example:
Q4> >foo "2+4=$(2+4)\n"
Q5> cat foo

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Per Bothner on December, 4 2001 using texi2html