Skip to content
Latchkey

awk exit: Control the Exit Code for CI Gates

awk exit ends the program immediately and an optional code becomes the process exit status, which CI uses to pass or fail a step.

To make awk fail a job on a parsed condition, set a flag and exit with a non-zero code. The exit status is what the runner checks.

What it does

exit stops reading input and jumps to END (running it once), then the process exits. exit N sets the exit status to N. A program that never calls exit returns 0 unless an error occurred. Setting a flag during processing and calling exit in END is the gate idiom.

Common usage

Terminal
# fail the step if any coverage row is below 80
awk '$2 < 80 {bad=1} END {exit bad}' coverage.txt
# exit early on the first match (and succeed)
awk '/READY/ {found=1; exit} END {exit !found}' status.log
# explicit non-zero code
awk 'END {exit 2}' file.txt; echo "status: $?"

Forms

StatementWhat it does
exitStop now, run END, exit with current status (0 unless set)
exit NExit with status N
exit badExit with the value of a flag variable (0 or 1)
exit !foundInvert a found-flag into a pass/fail code
$? (shell)Reads awk exit status after the command

In CI

A gate reads as awk 'cond {bad=1} END{exit bad}'; the runner fails the step when awk exits non-zero. Note that exit still runs the END block, so a summary you print in END is emitted even on the failing path, which is useful for a final message.

Common errors in CI

In a pipeline like producer | awk '...', the shell reports the exit code of the last command (awk) only with the default behavior; set -o pipefail if an earlier stage can fail. An exit inside the main block still runs END, so guard END logic that should not run twice. A bare exit with no code that follows an error keeps the error status, which may be non-zero unexpectedly.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →