Linux Shell Bash with special-meaning exit codes

Source: Internet
Author: User

Linux Shell Bash with special meaning exit code purpose description

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. */

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 */
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

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

Example four in the script, delete temporary files when you exit bash code
    1. 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

Source: http://codingstandards.iteye.com/blog/836625

Table D-1. "reserved" Exit codes

the value of the exit code meaning Example Notes
1 Generic error Let "var1 = 1/0" All kinds of errors may use this exit code, such as "except 0 error"
2 Shell built-in command usage error (described on bash documentation) Rarely seen, usually the exit code is 1
126 Command call cannot be performed The permission for a program or command is not executable
127 "Command not Found" Guess it's $path wrong, or it's a misspelling.
128 Exit parameter is wrong Exit 3.14159 Exit can only be an integer parameter with a range of 0-255 (see footnote)
128+n fatal error of Signal "n" $ppid of the Kill-9 script $? Return 137 (+ 9)
130 Use Control-c to end the script Control-c is a fatal error of Signal 2, (130 = 128 + 2, see above)
255* Out-of-range exit status Exit -1 The exit command can only accept integers with a range of 0-255 as arguments

From the table above, we learned that exit codes 1-2, 126-165, and 255 [1] have special meanings, so you should avoid using the user-specified exit parameters. If the script uses Exit 127 as the exit statement, it may cause confusion in troubleshooting (How To Is this caused by "command not Found" or by user-defined?). However, many scripts use Exit 1 as the common return error value. Because the exit code 1 can indicate too many errors, but in doing so, for debugging, also can not help the role.

In fact, there have been a systematic classification of exit status values (see /usr/include/sysexits.h), but this file is for C + + programmers. In fact, shell scripts also need such a similar standard. So the author calls for limiting the use of user-defined exit codes, especially in the range 64-113 (and 0, for success), so that it can be consistent with the C + + standard. This way we have 50 available exit codes and are very easy to troubleshoot.

The user-defined exit codes in all the examples in this book conform to this standard, except for those examples that go beyond the standard range, such as example 9-2.

Use $ on the command line only at the bash or SH prompt when the shell script exits ? Results that are consistent with the table above. In some cases, running C-shell or tcsh may give different values.

Note the exit value out of range may produce unexpected exit codes. If the exit value is greater than255, then the exit code will take 256 modulo. For example, Exit 3809 will be 225 (3809 = 256 = 225).
Source: http://www.cnblogs.com/osroot/archive/2010/01/11/1643947.html

The __function__ constant records the name of the current function in C + +. Sometimes it is useful to include this information in the log output. In bash, too, there is a constant funcname, but the difference is that it is an array, not a string, where the first element of the array is the name of the current function.

Maybe it's a little hard to understand why funcname an array? Take a look at the example below and you'll see.

#!/bin/bashfunction Test_func () {  echo  "current $FUNCNAME, \ $FUNCNAME = (${funcname[@]})"  "current $FUNCNAME, \ $FUNCNAME = (${funcname[@]})" }function Another_func () Span class= "PLN" >{ echo  "current $FUNCNAME, \ $FUNCNAME = = ($ {funcname[@]}) " }echo  "out of function, \ $FUNCNAME = (${ funcname[@]}) "test_funcecho " out of function, \ $FUNCNAME = (${funcname[@]}) "  

The result after execution is:

Out of function, $FUNCNAME = () current Test_func, $FUNCNAME = = (test_func main) Current Another_func, $FUNCNAME => ; (Another_func Test_func Main) Current Test_func, $FUNCNAME = (test_func main) out of function, $FUNCNAME = = ()

So, to be more precise, funcname is an array, but it is maintained in bash as a stack-like form.

Another useful constant similar to funcname is Bash_source, which is also an array, but its first element is the name of the current script.
This is useful when source, because in the script that is source, $ A is the name of the parent script, not the script name of the source. and Bash_source
can be useful.

# If the script is sourced by another scriptIf[-"$BASH _source"-"$BASH _source"!=  "$"]then do_somethingElse# Otherwise, run directly in the shell do_otherfi
                              

The only thing that's unfortunate is that this will cause the script to lose some portability, because not all shells support these constants.


Source: Http://kodango.com/get-function-name-in-bash

Linux Shell Bash with special-meaning exit codes

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.