Exit of Linux command-exit current Shell "return value status"

Source: Internet
Author: User

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
    1. #define EXIT_FAILURE 1/* failing EXIT status. */
    2. #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
  1. #define EX_OK 0/* Successful termination */
  2. #define EX__BASE */* BASE value for error messages */
  3. #define EX_USAGE/* Command line USAGE error */
  4. #define EX_DATAERR/* Data format Error */
  5. #define EX_NOINPUT */Cannot open input */
  6. #define EX_NOUSER */Addressee Unknown */
  7. #define EX_NOHOST */* Host name unknown */
  8. #define EX_UNAVAILABLE/* Service Unavailable */
  9. #define EX_SOFTWARE */Internal software error */
  10. #define EX_OSERR/* system error (e.g., can ' t fork) */
  11. #define EX_OSFILE */critical OS file missing */
  12. #define EX_CANTCREAT./* can ' t create (user) output file */
  13. #define EX_IOERR */input/output Error */
  14. #define EX_TEMPFAIL */temp failure; User is invited to retry */
  15. #define EX_PROTOCOL/* Remote error in PROTOCOL */
  16. #define EX_NOPERM */Permission denied */
  17. #define EX_CONFIG/* Configuration Error */
  18. #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
    1. 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
    1. If [ "$#"-ne "2"]; Then
    2. echo "Usage: $ <area>
    3. Exit 2
    4. Fi
If ["$#"-ne "2"]; Then    echo "Usage: $ <area> 

Example four in the script, delete temporary files when you exit bash code
    1. 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
    1. ./mycommand.sh
    2. Excode=$?
    3. If [ "$EXCODE" = = "0"]; Then
    4. echo "O.K"
    5. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.