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
/home/seachild/winsor
$ echo $?
Ø
$ cd /home3
/home3: bad directory
$ echo $?
1
$

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
root:x:Ø:1:ØØØØ-Admin(ØØØØ):/:/sbin/sh
oak% echo $status
Ø
oak% grep anthonly /etc/passwd
oak% echo $status
1
oak% grep root /etc/password
grep: can't open /etc/password
oak% echo $status
2
oak%

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
5
$ expr $i - 1
3
$ expr $i \* 2
8
$ expr $i / 2
2
$ expr $i % 2
Ø
$ j=2
$ expr $i + $j
6
$

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
8
oak% @ total++
oak% echo $total
9
oak% @ total += 4
oak% echo $total
13
oak% @ newtotal = ($total > 5)
oak% echo $newtotal
1
oak%.
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