Basic

Exit codes as a written set

A number that means finished and a number that means refused are two numbers. The set is written, and a written set is why a script can branch on a run.

What you will see

refuse number outside set. A wrong number is a bug, not a meaning.

About 1 min of reading

Try It

Branch on a written set

1

Write the set

printf '0 kept\n2 refused\n9 broke\n' > /tmp/e.txt; wc -l < /tmp/e.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3. Three numbers is the set, which is small.
2

Answer refused

printf 'refuse\n' | awk 'END{exit 2}'; echo $?

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2. The number is from the set, which is what a script branches on.
3

Refuse an outside number

printf '7\n' | grep -c -f -x <(cut -f1 -d' ' /tmp/e.txt) /dev/stdin | awk '{print ($1==0) ? "refuse number outside set" : "ok"}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
refuse number outside set. A wrong number is a bug, not a meaning.

Data Flow

A run to a number from a set
1 The set is written with meanings
↓
2 The run decides kept, refused or broke
↓
3 The number is written as the exit
↓
4 A number outside the set refuses
Why this works
Why zero is the only kept number

Zero is the number a shell treats as success. One kept number is what makes every other number a claim.

The failure at this step

A run that answers zero and changed nothing. The set is the answer, and the answer is the number to use.

Examples

The set is small

Kept, refused, broke. Three numbers, and three numbers is a set a reader memorises.

The set is in the help

The help prints the numbers with their meanings. Printed meanings are what a script writer reads.

A new number is refused

A number outside the set is not answered. Not answered is what makes a wrong number a bug rather than a meaning.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Two streams, one meaning each
Previous Lesson
Two streams, one meaning each
Next Lesson
Help as the tool answering about itself
Help as the tool answering about itself →