Jump to: |  | OMake Home • Guide Home • Guide (single-page) • Contents (short) • Contents (long)
Index: |  | All • Variables • Functions • Objects • Targets • Options

# Chapter 11 Shell commands

- Simple commands
- Globbing
- Background jobs
- File redirection
- Pipelines
- Conditional execution
- Grouping
- What is a shell command?
- Basic builtin functions
- Job control builtin functions
- Command history

Shell commands (commands to be executed by the operating system) can be freely mixed with other code.

NOTE: the syntax and shell usage is identical on all platforms, including Win32. To avoid portability problems on Win32, it is recommended that you avoid the use of the native shell interpreter `cmd`.

        LIB = $(dir lib)
        println(The contents of the $(LIB) directory is:)
        ls $(LIB)

## 11.1 Simple commands

The syntax of shell commands is similar to the syntax used by the Unix shell `bash`. In general, a command is a pipeline. A basic command is part of a pipeline. It is specified with the name of an executable and some arguments. Here are some examples.

        ls
        ls -AF .
        echo Hello world

The command is found using the current search path in the variable `PATH[]`, which should define an array of directories containing executables.

A command may also be prefixed by environment variable definitions.

        # Prints "Hello world"
        env X="Hello world" Y=2 printenv X
        # Pass the include path to the Visual C++
        env include="c:\Program Files\Microsoft SDK\include" cl foo.cpp

## 11.2 Globbing

Commands may contain wildcard patterns. A pattern specifies a set of files through a limited kind of regular expression. Patterns are expanded before the function is executed.

       # List all files with a .c suffix
       ls *.c

       # List all files with a single character prefix, and .c suffix
       ls ?.c

       # Rename the file hello.ml to foo.ml
       mv {hello,foo}.ml

A comprehensive description of OMake glob patterns is given in Section 10.4.

## 11.3 Background jobs

The command may also be placed in the background by placing an ampersand after the command. Control returns to the shell without waiting for the job to complete. The job continues to run in the background.

        gcc -o hugeprogram *.c &

## 11.4 File redirection

Input and output can be redirected to files by using the `<`, `>`, and `>&` directives after the command.

        # Write to the "foo" file
        echo Hello world > foo

        # Redirect input from the foo file
        cat < foo

        # Redirect standard output and errors to the foo file
        gcc -o boo *.c >& foo

## 11.5 Pipelines

Pipelines are sequences of commands, where the output from each command is sent to the next. Pipelines are defined with the `|` and `|&` syntax. With `|` the output is redirected, but errors are not. With `|&` both output and errors are redirected.

       # Send the output of the ls command to the printer
       ls *.c | lpr

       # Send output and errors to jyh as email
       gcc -o hugefile *.c |& mail jyh

## 11.6 Conditional execution

Commands may also be composed though conditional evaluation using the `||` and `&&` syntax. Every command has an integer exit code, which may be zero or some other integer. A command is said to succeed if its exit code is zero. The expression `command1 && command2` executes `command2` only if `command1` succeeds. The expression `command1 || command2` executes `command2` only if `command1` fails.

       # Display the x/y file if possible
       cd x && cat y

       # Run foo.exe, or print an error message
       (test -x foo.exe && foo.exe) || echo "foo.exe is not executable"

## 11.7 Grouping

Parenthesis are used for grouping in a pipeline or conditional command. In the following expression, the `test` function is used to test whether the `foo.exe` file is executable. If it is, the `foo.exe` file is executed. If the file is not executable (or if the `foo.exe` command fails), the message `"foo.exe is not executable"` is printed.

       # Run foo.exe, or print an error message
       (test -x foo.exe && foo.exe) || echo "foo.exe is not executable"

## 11.8 What is a shell command?

Syntactially, shell commands are any line that is not one of the following:

- A variable definition of the form `VAR=string`
- A function call `f(...)` or method call `o.f(...)`
- A rule definition containing a colon `string: ...`
- A special command, including the following:
  - `if ...`
  - `switch ...`
  - `match ...`
  - `section ...`
  - `return ...`

Commands may also be builtin (aliases). See the documentation for the `Shell` object for more information.

## 11.9 Basic builtin functions

### 11.9.1 echo

The `echo` function prints a string.

    $(echo <args>)
    echo <args>

### 11.9.2 cd

The `cd` function changes the current directory.

        cd(dir)
           dir : Dir

The `cd` function also supports a 2-argument form:

        $(cd dir, e)
           dir : Dir
           e : expression

In the two-argument form, expression `e` is evaluated in the directory `dir`. The current directory is not changed otherwise.

The behavior of the `cd` function can be changed with the `CDPATH` variable, which specifies a search path for directories. This is normally useful only in the `osh` command interpreter.

        CDPATH : Dir Sequence

For example, the following will change directory to the first directory `./foo`, `~/dir1/foo`, `~/dir2/foo`.

        CDPATH[] =
           .
           $(HOME)/dir1
           $(HOME)/dir2
        cd foo

## 11.10 Job control builtin functions

### 11.10.1 jobs

The `jobs` function prints a list of jobs.

`jobs`

### 11.10.2 bg

The `bg` function places a job in the background.

`bg <pid...>`

### 11.10.3 fg

The `fg` function brings a job to the foreground.

`fg <pid...>`

### 11.10.4 stop

The `stop` function suspends a job.

`stop <pid...>`

### 11.10.5 wait

The `wait` function waits for a job to finish. If no process identifiers are given, the shell waits for all jobs to complete.

`wait <pid...>`

### 11.10.6 kill

The `kill` function signals a job.

`kill [signal] <pid...>`

## 11.11 Command history

### 11.11.1 history

        $(history-index) : Int
        $(history) : String Sequence
        history-file : File
        history-length : Int

The history variables manage the command-line history in `osh`. They have no effect in `omake`.

The `history-index` variable is the current index into the command-line history. The `history` variable is the current command-line history.

The `history-file` variable can be redefined if you want the command-line history to be saved. The default value is `~/.omake/osh_history`.

The `history-length` variable can be redefined to specify the maximum number of lines in the history that you want saved. The default value is `100`.

Jump to: |  | OMake Home • Guide Home • Guide (single-page) • Contents (short) • Contents (long)
Index: |  | All • Variables • Functions • Objects • Targets • Options