Previous Table of Contents Next

Exit Status

When a command or shell function terminates, it returns an exit status to the invoking shell. The exit status is a numeric value that indicates whether the program ran successfully and whether certain events occurred in the command.

Every command that runs has an exit status, which is set by the programmer when writing the command. Usually, an exit status of 0 means that the program executed successfully. Any non-zero value (usually 1 or -1) means that the program failed. Programmers may not always follow this convention. Check the manual page for a given command to determine its exit status. For example, the grep command returns one of three exit status values: 0 means that the search pattern was found, 1 means that the pattern could not be found, and 2 means that the grep command could not open or find the file(s) to be searched.

For the Bourne shell, the $? variable holds exit status for the last command executed in the same way that the C shell status variable does. Usually, 0 indicates success and 1 indicates failure.

$ pwd
$ echo $?
$ cd /home3
/home3: bad directory
$ echo $?

The C shell variable status is automatically set by the shell to the exit status of the last command executed. You can use the echo command to display the exit status at the prompt.

oak% grep root /etc/passwd
oak% echo $status
oak% grep anthonly /etc/passwd
oak% echo $status
oak% grep root /etc/password
grep: can't open /etc/password
oak% echo $status

When writing shell scripts, you can add an exit 0 to the end of the script to indicate successful completion. Exiting with any other value shows that something went wrong.

Mathematical Operations

You can do mathematical operations only on integers (whole numbers) for all three shells. If you want to do more complicated arithmetic, you can use the awk command.

To perform mathematical operations in the Bourne shell, you can use the expr command. Here is the syntax:

expr arguments

You must separate the mathematical operators (shown in Table 16-15) and the operand with white space.

Table 16-15 Mathematical Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder

The multiplication, division, and remainder operators have higher precedence than do the addition or subtraction operators. Use parentheses for grouping.

The following example uses the expr command:

$ i=4
$ expr $i + 1
$ expr $i - 1
$ expr $i \* 2
$ expr $i / 2
$ expr $i % 2
$ j=2
$ expr $i + $j

The Korn shell has let and built-in $(( )) expressions for doing arithmetic.

The C shell @ command is similar to the Bourne and Korn shell expr command. The @ command evaluates an expression mathematically and then assigns the resulting value to a shell variable, as shown in Table 16-16.

The expressions can be mathematical or logical. Mathematical expressions typically use the operators shown in Table 16-16. Logical expressions typically use one of the following operators, and yield a 1 (true) or 0 (false) value:

> < >= <= == !=

The following example shows C shell numeric values:

oak% @ total = 5 + 3
oak% echo $total
oak% @ total++
oak% echo $total
oak% @ total += 4
oak% echo $total
oak% @ newtotal = ($total > 5)
oak% echo $newtotal
Table 16-16 C Shell Mathematical Operators

Syntax Description
@variable=(expression) Sets value of variable equal to the expression.
@variable+=(expression) Performs addition.
@variable-=(expression) Performs subtraction.
@variable*=(expression) Performs multiplication.
@variable/=(expression) Peforms division.
@ variable ++ Adds 1.
@ variable -- Subtracts 1.

Previous Table of Contents Next