Linux command exit - exit the current shell

from:
http://blog.163.com/bobile45@126/blog/static/96061992201311712658570/

Purpose Description
The exit command is used to exit the current shell, and the execution of the current script can be terminated in the shell script.

Common parameters
Format : exit n
to exit. Set the exit code to n. (Cause the shell to exit with a status of n.)

Format: exit
to exit. The exit code remains unchanged, which is the exit code of the last command. (If n is omitted, the exit status is that of the last command executed. )

Format: $?
The exit code of the last command.

Format: trap "commands" EXIT
executes the command specified by commands when it exits. (A trap on EXIT is executed before the shell terminates.)

The convention of exit code (exit status, or exit code):
0 means success (Zero - Success)
non-0 means failure (Non-Zero - Failure)
2 means improper usage ( Incorrect Usage)
127 indicates that the command is not found (Command Not Found)
126 indicates that it is not executable (Not an executable)
>=128 signal generation

man 3 exit wrote
The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, that may be passed to exit() to indicate
successful or unsuccessful termination, respectively.

The following is excerpted from /usr/include/stdlib.h
C code Favorite code
#define EXIT_FAILURE 1 /* Failing exit status. */ 
#define EXIT_SUCCESS 0 /* Successful exit status. */ 

BSD attempts to standardize exit codes.
man 3 exit wrote that
BSD has attempted to standardize exit codes; see the file <sysexits.h>.


The following is excerpted from /usr/include/sysexits.h
C code Collection code
#define EX_OK 0 /* successful termination */ 
 
#define EX__BASE 64 /* base value for error messages */ 
 
#define EX_USAGE        64      /* command line usage error */ 
#define EX_DATAERR      65      /* data format error */ 
#define EX_NOINPUT      66      /* cannot open input */ 
#define EX_NOUSER       67      /* addressee unknown */ 
#define EX_NOHOST       68      /* host name unknown */ 
#define EX_UNAVAILABLE  69      /* service unavailable */ 
#define EX_SOFTWARE     70      /* internal software error */ 
#define EX_OSERR        71      /* system error (e.g., can't fork) */ 
#define EX_OSFILE       72      /* critical OS file missing */ 
#define EX_CANTCREAT    73      /* can't create (user) output file */ 
#define EX_IOERR        74      /* input/output error */ 
#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ 
#define EX_PROTOCOL 76 /* remote error in protocol */ 
#define EX_NOPERM 77 /* permission denied */ 
#define EX_CONFIG 78 /* configuration error */ 
 
#define EX__MAX 78 /* maximum listed value */ 

Example
Example 1 Exit the current shell
[root@new55 ~]#
[root@new55 ~]# exit
logout

Example 2 In the script, enter the directory where the script is located, otherwise exit the
Bash code Favorite code
cd $(dirname $0) || exit 1Example 

3 In the script, judge the number of parameters, print the usage method if they do not match, and exit the
Bash code Favorite code
if [ "$#" -ne "2" ]; then 
    echo "usage: $0 <area> <hours>" 
    exit 2 
fi 

example four in the script, delete temporary files on exit
Bash code Favorite code
trap "rm -f tmpfile; echo Bye." EXIT 

example 5 Check the exit code of the previous command
Bash code Favorite
code./mycommand.sh 
EXCODE=$? 
if [ "$EXCODE" == "0" ] ; then 
    echo "OK" 
fi 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326371824&siteId=291194637