Shell Getting Started Tutorial: Process Control (1) End state of a command

Source: Internet
Author: User
Tags arithmetic

In the bash shell, there are 2 main classes of Process Control commands: " condition ", " loop ". "Conditions" are: If, case, the "loop" has: for, while, until; command select is both "condition" and "loop". Either way, the condition test is required, and the direction of the program flow is determined according to the test results.

The use of these commands is described below.

Section One: End state of a command

Bash Shell Script can be made up of a number of commands. After each command executes, it returns an end-state value that, if executed successfully, returns 0 and returns a non-0 value if execution fails. Execution failures are also broken down into many situations, such as:

A command execution fails, generating a severe signal n, and its end state returns a value of 128+n. If a command is executed and the command is found to be absent, the return value is 127. command exists but no execution, returns the value 126.

Bash's built-in variable $?, used to store the status value returned after each command was executed. As the following example, when executing test.sh, bash cannot find this script in the search path, the value of the end state variable $ is 127; when executing./test.sh, the value of$ is 126 because the script does not have execution rights.

[Email protected] ~]# test.sh-bash:test.sh: No file or directory [[email protected] ~]# echo $?127[[email protected] ~]#./test.sh-b Ash:./test.sh: Insufficient permissions [[email protected] ~]# echo $?126[[email protected] ~]#

In the process control of bash, it is necessary to test the condition, which is to judge the true and false based on the end state :

If the end status is 0, the judgment is true, and if the end state is a non-0 value, it is false.

Section II: If Condition judgment

Let's look at a simple example:

If Grep-q ^jianbao/etc/passwd; Thenecho ' Jianbao This account exists ' fi

#格式一

if condition test; Then Command area fi

#格式二

if condition test; Then command Area 1else command Area 2fi

#格式三 (full syntax)

if condition test 1; Then command area 1elif condition Test 2; Then command Area 2else command Area 3fi

Example one:

if [-d/root/tmp]; Thenecho '/root/tmp directory exists ' Elseecho '/root/tmp directory does not exist ' fi

Example two:

#!/bin/bash# Mathematics declare-i a ba=$1;b=$2if (a<b); Thenecho "$a less than $b" elif ((a>b)); Thenecho "$a greater than $b" Elseecho "$a equals-$b" fi

(()) is Bash's compound command (Compound commands), built-in arithmetic. (arithmetic) The result of a calculation can be passed back to the true or False value:

If the result of arithmetic is not 0, the end state is passed back to 0, indicating that the calculation is true, and if the result of the calculation is 0, the end state returns 1, indicating that the calculation is false. Its meaning is the same as:

Let "arithmetic"

Example three:

#!/bin/bash# Mathematics declare-i A ba=$1;b=$2if let "a<b"; Thenecho "$a less than $b" Elif let "a>b"; Thenecho "$a greater than $b" Elseecho "$a equals-$b" fi

Shell Getting Started Tutorial: Process Control (1) End state of a command

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.