|Advanced Bash-Scripting HOWTO: A guide to shell scripting, using Bash|
|Prev||Chapter 3. Tutorial / Reference||Next|
The exit command may be used to terminate a script, just as in a C program. It can also return a value, which is available to the shell.
Every command returns an exit status (sometimes referred to as a return status ). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually may be interpreted as an error code.
Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nn command may be used to deliver an nn exit status to the shell (nn must be a decimal number in the 0 - 255 range).
$? reads the exit status of script or function.
Example 3-1. exit / exit status
#!/bin/bash echo hello echo $? # exit status 0 returned # because command successful. lskdf # bad command echo $? # non-zero exit status returned. echo exit 143 # Will return 143 to shell. # To verify this, type $? after script terminates. # By convention, an 'exit 0' shows success, # while a non-zero exit value indicates an error or anomalous condition. # It is also appropriate for the script to use the exit status # to communicate with other processes, as when in a pipe with other scripts.
Закладки на сайте
Проследить за страницей
Created 1996-2022 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру