Original link: http://codingstandards.iteye.com/blog/836625 (reproduced please specify the source)
Description of Use
The exit command exits the current shell and can terminate the current script execution in a shell script.
Common parameters
Format: Exit n
Exit. Set the exit code to N. (cause the Shell to exit with a status of N.)
Format: Exit
Exit. The exit code is unchanged, which is the exit code for the last command. (If n is omitted, the exit status is the last command executed. )
Format: $?
The exit code for the previous command.
Format: Trap "Commands" EXIT
Executes the command specified by commands when exiting. (A trap on EXIT is executed before the shell terminates.)
Exit code (Exit status, or exit code) Convention:
0 indicates success (zero-success)
Non 0 indicates failure (non-zero-failure)
2 indicates improper usage (incorrect usage)
127 indicates that the command was not found. (Command not Found)
126 means not executable (not an executable)
>=128 Signal Generation
Man 3 exit wrote the C standard specifies-constants, exit_success and Exit_failure, that May is passed to exit () to Indica Te successful or unsuccessful termination, respectively.
The following excerpt from/usr/include/stdlib.h
C code
- #define EXIT_FAILURE 1/* failing EXIT status. */
- #define EXIT_SUCCESS 0/* Successful EXIT status. */
#define Exit_failure 1/ * failing EXIT status. */#define EXIT_SUCCESS 0/ * Successful EXIT status. */
BSD tries to standardize the exit code.
Man 3 exit wrote BSD have attempted to standardize exit codes; See the file <sysexits.h>.
The following excerpt from/usr/include/sysexits.h
C code
- #define EX_OK 0/* Successful termination */
- #define EX__BASE */* BASE value for error messages */
- #define EX_USAGE/* Command line USAGE error */
- #define EX_DATAERR/* Data format Error */
- #define EX_NOINPUT */Cannot open input */
- #define EX_NOUSER */Addressee Unknown */
- #define EX_NOHOST */* Host name unknown */
- #define EX_UNAVAILABLE/* Service Unavailable */
- #define EX_SOFTWARE */Internal software error */
- #define EX_OSERR/* system error (e.g., can ' t fork) */
- #define EX_OSFILE */critical OS file missing */
- #define EX_CANTCREAT./* can ' t create (user) output file */
- #define EX_IOERR */input/output Error */
- #define EX_TEMPFAIL */temp failure; User is invited to retry */
- #define EX_PROTOCOL/* Remote error in PROTOCOL */
- #define EX_NOPERM */Permission denied */
- #define EX_CONFIG/* Configuration Error */
- #define EX__MAX/* Maximum listed value */
#define EX_OK 0 */Successful termination */#define EX__BASE */BASE value for error MESSAG ES */#define EX_USAGE/* Command line USAGE error */#define EX_DATAERR/* Data format Error */ #define EX_NOINPUT/Cannot open input */#define EX_NOUSER/* Addressee unknown */#define Ex_n Ohost/* Host name unknown */#define EX_UNAVAILABLE/* Service Unavailable */#define Ex_software */* Internal software error */#define EX_OSERR//system error (e.g., can ' t fork) */#define EX_OSF ILE */Critical OS file missing */#define EX_CANTCREAT//Can ' t create (user) output file */#defi NE ex_ioerr/* input/output error */#define EX_TEMPFAIL//Temp failure; User is invited to retry */#define EX_PROTOCOL/* Remote error in PROTOCOL */#define EX_NOPERM 77/ * Permission denied * * #define Ex_config/* Configuration Error */#define EX__MAX//maximum listed value */
Use example one to exit the current shell
[Email protected] ~]# [[email protected] ~]# exit logout
Example two in the script, go to the directory where the script is located, or exit the bash code
- CD $ (dirname $0) | | Exit 1
CD $ (dirname $) | | Exit 1
Example three in the script, determine the number of parameters, do not match the way you print, Exit Bash code
- If [ "$#"-ne "2"]; Then
- echo "Usage: $ <area>
- Exit 2
- Fi
If ["$#"-ne "2"]; Then echo "Usage: $ <area>
Example four in the script, delete temporary files when you exit bash code
- Trap "Rm-f tmpfile; echo Bye. " EXIT
Trap "Rm-f tmpfile; echo Bye. " EXIT
Example five check the exit code of the previous Command bash code
- ./mycommand.sh
- Excode=$?
- If [ "$EXCODE" = = "0"]; Then
- echo "O.K"
- Fi
./mycommand.shexcode=$?if ["$EXCODE" = = "0"]; Then echo "O.k" fi
Problem thinking related information"1" 91linux Linux Exit command "2" winding streets [concept]exit n "3" Linux University Bash Shell exit Status Tutorial with practical Examples
Exit of the Linux command-exits the current shell return value status