Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exit with code, stdout & stderr

Overview

exit-with is a minimalistic command-line utility that terminates immediately after starting, while controlling the exit code, standard output (stdout) and standard error (stderr) messages, all in a single invocation.

Usage

exit-with [arg]... 

All arguments are optional. Each argument can be either a literal value, or a file name. If file name is provided, then the contents of the file is used as the argument value.

Note

Because of the specific usage of the exit-with utility, the only outputs from this application are the exit code and stdout/stderr messages. There are no additional options such as displaying version information, displaying help, or handling invalid arguments with error messages.

How it works

The behavior of this application is defined by the following decision table:

123
i32intruetruefalse
arg1in= 0!= 0-
codeout0arg10
stdoutoutarg2arg3…arg1
stderroutarg3…arg2arg2…

i32 and arg1 are inputs. code, stdout and stderr are outputs. i32 is a function that returns true when arg1 is a 32-bit signed integer.

There are three rules defined in columns numbered from 1 to 3.

RULE 1

when

  • the first argument arg1 provided to exit-with is a 32-bit signed integer

and

  • the first argument arg1 is equal to zero

then

  • the returned exit code will be 0,
  • the second argument arg2 will be printed to stdout,
  • all arguments starting fromarg3... will be printed to stderr.

RULE 2

when

  • the first argument arg1 provided to exit-with is a 32-bit signed integer

and

  • the first argument arg1 is not equal to zero

then

  • the returned exit code will be the value of arg1,
  • all arguments starting fromarg3... will be printed to stdout (sic!),
  • the second argument arg2 will be printed to stderr (sic!).

RULE 3

when

  • the first argument arg1 provided to exit-with is not a 32-bit signed integer

then

  • the returned exit code will be 0,
  • the first argument arg1 will be printed to stdout,
  • all arguments starting fromarg2... will be printed to stderr.

Tip

The destination where the messages are printed out depends on the value of code.

Examples

argumentsexit codestdoutstderr
0
00
hello0hello
0 hello0hello
0 hello world0helloworld
0 "" world0world
11
1 hello1hello
1 hello world1worldhello
1 "" world1world