execline: the if command
Keywords: execline command if

execline
Software
skarnet.org

# The `if` program

`if` performs conditional execution.

## Interface

In an execlineb script:

         if [ -X ] [ -n ] [ -t | -x exitcode ] { prog1... } prog2...

- `if` reads prog1... in a block. It forks and executes it, then waits for it to complete.
- If prog1 crashes, `if` prints an error message then exits 128 plus the number of the signal that killed prog1.
- If prog1 exits a non-zero status, `if` exits 1.
- Else `if` execs into prog2.

## Options

- `-X` : treat a crash of prog1 as a non-zero ("false") exit.
- `-n` : negate the test (exit on true, exec into prog2 on false)
- `-x` exitcode : exit exitcode instead of 1 if the test fails.
- `-t` : exit 0 instead of 1 if the test fails. This is equivalent to `-x 0`.

## Notes

- `if` will exit if prog1... exits false. To use it in an execline script that must run prog3... no matter the result of the test, use a `foreground` wrapper:

       foreground { if { prog1... } prog2... } prog3...

  (in execlineb syntax)
- `if prog1... "" prog2...` is equivalent to `sh -c 'prog1... && exec prog2...'`.