Exit and exit Status Codes

Source: Internet
Author: User
The exit and exit status codes exit are used to end a script, just like in C. it also returns a value, which is passed to the parent process of the script. The parent process uses this value for further processing. each command returns an exit status code (sometimes called return... syntaxHighlighter. all ();

Exit and exit Status Codes


Exit is used to end a script, just like in C. it also returns a value, which is passed to the parent process of the script. The parent process uses this value for further processing.

Each command returns an exit status code (sometimes called return status ). A successful command returns 0, but an unsuccessful command returns a non-zero value. A non-zero value is usually interpreted as an error code. A good UNIX Command, program, and tool returns 0 as the exit code to indicate success, although occasionally there are exceptions.

Similarly, functions and scripts in the script also return the exit status code. the final command executed in the script or script function determines the exit status code. in the script, the exit nnn command will pass the nnn exit code to shell (nnn must be a decimal number and the range must be 0-255 ).


When the script ends with an exit command without parameters, the exit status code of the script is determined by the last command executed in the script (that is, the command before exit ).

1 #! /Bin/bash
2
3 COMMAND_1
4
5...
6
7 # The exit status code is determined by the final command.
8 COMMAND_LAST
9
10 exit


Exit command and exit $? Without Parameters? The effect is the same, even the end of the script does not write exit, it is the same as the effect of the first two.

1 #! /Bin/bash
2
3 COMMAND_1
4
5...
6
7 # The exit status code is determined by the final command.
8 COMMAND_LAST
9
10 exit $?


1 #! /Bin/bash
2
3 COMMAND1
4
5...
6
7 # The exit status code is determined by the final command.
8 COMMAND_LAST

 


$? Saves the exit status code of the last executed command. When the function returns, $? Save the exit status code of the most executed command in the function. This is how bash processes the function "Return Value". When a script exits, $? Saves the exit status code of the script, which is the exit status code of the Last Command executed in the script. in general, 0 indicates successful, and an integer ranging from 1 to 255 indicates an error.


--------------------------------------------------------------------------------

The following describes the exit and exit status codes of an instance.

[Html]
#! /Bin/bash
Echo hello
Echo $? # The exit status is 0 because the command is successfully executed.
 
Lskdf # invalid command.
Echo $? # Non-zero exit status, because the command execution fails.
 
Echo
Exit 113 # Return the 113 exit status to shell.
# To verify this result, you can use "echo $? ".
# In general, 'exit 0' indicates success, and a non-zero exit code indicates an error or an abnormal condition.

#! /Bin/bash
Echo hello
Echo $? # The exit status is 0 because the command is successfully executed.

Lskdf # invalid command.
Echo $? # Non-zero exit status, because the command execution fails.

Echo
Exit 113 # Return the 113 exit status to shell.
# To verify this result, you can use "echo $? ".
# In general, 'exit 0' indicates success, and a non-zero exit code indicates an error or abnormal condition. Experiment results:

[Html]
Root @ ubuntu :~ /Resource/study/shell_study # chmod 777 exit_test
Root @ ubuntu :~ /Resource/study/shell_study #./exit_test
Hello
0
./Exit_test: line 5: lskdf: command not found
127
 
Root @ ubuntu :~ /Resource/study/shell_study # echo $?
113

Root @ ubuntu :~ /Resource/study/shell_study # chmod 777 exit_test
Root @ ubuntu :~ /Resource/study/shell_study #./exit_test
Hello
0
./Exit_test: line 5: lskdf: command not found
127

Root @ ubuntu :~ /Resource/study/shell_study # echo $?
113
How to flip a condition


[Html]
Root @ ubuntu :~ /Resource/study/shell_study # true
Root @ ubuntu :~ /Resource/study/shell_study # echo "exit status of \" true \ "= $? "
Exit status of "true" = 0
Root @ ubuntu :~ /Resource/study/shell_study #! True
Root @ ubuntu :~ /Resource/study/shell_study # echo "exit status of \" true \ "= $? "
Exit status of "true" = 1
Root @ ubuntu :~ /Resource/study/shell_study # echo "exit status of \" true \ "= $? "
Exit status of "true" = 0
Root @ ubuntu :~ /Resource/study/shell_study # echo "exit status of \" true \ "= $? "
Exit status of "true" = 0
Root @ ubuntu :~ /Resource/study/shell_study #! True
Root @ ubuntu :~ /Resource/study/shell_study # echo "exit status of \" true \ "= $? "
Exit status of "true" = 1
Root @ ubuntu :~ /Resource/study/shell_study # echo "exit status of \" true \ "= $? "
Exit status of "true" = 0

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.