Classic Shell Scripting - Arnold Robbins [72]
* * *
Tip
The ++ and -- operators are optional: conforming implementations do not have to support them. bash and ksh93 do support them.
The standard allows an implementation to support additional operators. All versions of ksh93 support the C comma operator, and recent versions support exponentiation with **. bash also supports both of these.
The standard only describes arithmetic using constant values. When parameter evaluation, such as $i, is done first, the arithmetic evaluator only sees constant values. In practice, all shells that support $((...)) allow you to provide a variable name without prefixing it with $.
* * *
According to POSIX, arithmetic is done using C signed long integers. ksh93 supports floating-point arithmetic, but you should not rely on that for portable programs.
Exit Statuses
Every command—be it built-in, shell function, or external—when it exits, returns a small integer value to the program that invoked it. This is known as the program's exit status. There are a number of ways to use a program's exit status when programming with the shell.
Exit Status Values
By convention, an exit status of 0 indicates "success"; i.e., that the program ran and didn't encounter any problems. Any other exit status indicates failure.[1] (We'll show you shortly how to use the exit status.) The built-in variable ? (accessed as $?) contains the exit value of the last program that the shell ran.
For example, when you type ls, the shell finds and runs the ls program. When ls finishes, the shell recovers ls's exit status. Here's an example:
$ ls -l /dev/null
ls on an existing file
crw-rw-rw- 1 root root 1, 3 Aug 30 2001 /dev/null ls's output
$ echo $?
Show exit status
0 Exit status was successful
$ ls foo
Now ls a nonexistent file
ls: foo: No such file or directory ls's error message
$ echo $?
Show exit status
1 Exit status indicates failure
The POSIX standard defines the exit statuses and their meanings, as shown in Table 6-5.
Table 6-5. POSIX exit statuses
Value
Meaning
0
Command exited successfully.
> 0
Failure during redirection or word expansion (tilde, variable, command, and arithmetic expansions, as well as word splitting).
1-125
Command exited unsuccessfully. The meanings of particular exit values are defined by each individual command.
126
Command found, but file was not executable.
127
Command not found.
> 128
Command died due to receiving a signal.
Curiously, POSIX leaves exit status 128 unspecified, apart from requiring that it represent some sort of failure. Only the low-order eight bits are returned to the parent process, so an exit status greater than 255 is replaced by the remainder of that value divided by 256.
Your shell script can pass an exit value back to its caller, using the exit command. Simply pass a number to it as the first argument. The script will exit immediately, and the caller receives that number as your script's exit value:
exit 42 Return the answer to the ultimate question
* * *
exit
Usage
exit [ exit-value ]
Purpose
To return an exit status from a shell script to the script's caller.
Major options
None.
Behavior
The default exit status used if none is supplied is the exit status of the last command executed. If that is what you want, it is best to do this explicitly in the shell script:
exit $?
* * *
if-elif-else-fi
The most obvious way to use a program's exit status is with the if statement. The general syntax is:
if pipeline
[ pipeline ... ]
then
statements-if-true-1
[ elif pipeline
[ pipeline ... ]
then
statements-if-true-2