Shell knowledge point supplement (6)-exit/expr

Source: Internet
Author: User
1. Linux Command exit-description of the purpose of exiting the Current Shell

The exit command is used to exit the current shell. In the shell script, you can terminate the execution of the current script. If you do not exit, you may continue to execute subsequent programs.

 

Common Parameters

Format: Exit n

Exit. Set exit code to n. (Cause the shell to exit with a status of n .)

 

Format: Exit

Exit. The exit code is the exit code of the last command. (If n is omitted, the exit status is that oftheLastCommand executed .)

 

Format: $?

The exit code of the previous command.

 

Format: Trap "commands" Exit

Execute the command specified by commands when exiting. (A trap on exit is executed before the shellterminates .)

 

Exit code (exit status, or exit code) Conventions:

0 indicates success (zero-success)

Non-0 indicates failure (non-zero-Failure)

2 indicates 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

 

When you exit 0, you call the environment echo $? 0 is returned, that is, the calling Environment considers that your program is correctly executed.
When you exit 1, it is generally an error that defines this 1, or it can be another number. Many system programs have this error number as agreed. If the value is not 0, the program runs incorrectly. The call environment can determine whether the program runs OK Based on the returned value.

If you use script a to call script B, you must identify whether B returns normally in script a Based on exit 0 or 1.
After executing B, Judge $? Is the return 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
Bash code 
  1. cd $(dirname $0) || exit 1  
 
Example 3: In the script, determine the number of parameters. If the number does not match, print the usage mode and exit.
Bash code"Exit 2 fi" Quality = "high" allowScriptAccess = "always" type = "application/X-Shockwave-flash" pluginspage = "http://www.macromedia.com/go/getflashplayer">
  1. if [ "$#" -ne "2" ]; then  
  2.     echo "usage: $0 <area>  
  3.     exit 2  
  4. fi  
 
Example 4: delete a temporary file when exiting the script
Bash code 
  1. trap "rm -f tmpfile; echo Bye." EXIT  
 
Example 5 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  
 

2. expr command

Arithmetic Operations on Integer Variables

Expr 3 + 5
Space

Expr $ var1-5

Expr $ var1/$ var2 Division

Expr $ var1 \ * 10 multiplication escape characters

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.