Shell condition Test Statement and three if statements

Source: Internet
Author: User
Tags disk usage

7-shell_ condition Operation Test and if statement

The first question to make a shell script "smart" is how to differentiate between different situations to determine what to do. For example, a warning is sent when the disk usage exceeds 95%, it can be created automatically when the backup directory is not present, and installation is no longer available when the source code compiler is configured to fail.

The return status value of the shell environment based on the command execution ($?) To determine if the execution succeeds when the return value is 0, indicating success, and the value is other, indicating failure.

Use the Special test Tool-test command, you can test the specific conditions, and based on the return value to determine whether the condition is true (return value 0 is established)

Two forms of using the test command

A:test conditional expression

b:[conditional expression]

The two methods work exactly the same, the latter being common. But the latter needs to be aware that there is at least one space between the square brackets "[", "]" and the conditional expression.

1. File Testing

File testing refers to the path given, to determine whether the corresponding file or target, or to determine if the file has readable writable, executable and other permissions. Common actions are as follows:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/4D/wKioL1Y2AhvClthbAAFOux8jVMQ485.jpg "height=" 233 "/>

After performing a conditional test operation, pass the predefined variable "$?" "The return status of the test command can be obtained to determine whether the condition is true."

For example, if the operation determines whether the/media/cdrom/directory exists, the return value is 1.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/50/wKiom1Y2AeGxE9cjAAAzPAjo8e4485.jpg "height="/>

If the test condition does not exist, the test return value will not be 0 (typically 1):

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/75/50/wKiom1Y2AeHyeIxdAABUAUSidg4324.jpg "height="/>

2. Integer value comparison

The relationship between the first and second number is judged mainly by the given two values, such as whether it is greater than, less than, or equal to the second number. Common options are as follows:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/50/wKiom1Y2AeGRntj6AAFgqW_vHtk470.jpg "height=" 315 "/>

Integer value comparisons are much more in shell scripting applications. For example: Determine the number of logged-in users, the number of open processes, whether disk usage is exceeded, and whether the software version meets the requirements. When used in practice, a value is often obtained by means of a variable reference, command substitution, and so on.

For example, to determine the number of currently logged-on users, when more than five outputs "Too may", you can execute the following statement. Of these, the number of logged-in users is available through the WHO | Wc-l "Query, in order to replace the method.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/75/50/wKiom1Y2AeHBuF1FAABrhknBDg8812.jpg "height="/>

0

For example, to determine the current available memory (buffers/cache) size, output a specific value when it is below 2048MB, you can perform the following actions. Where the "free-m" command outputs memory information in MB units.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/75/50/wKiom1Y2AeLjBJR5AACJbddEgVA056.jpg "height=" 111 "/>

3. String comparisons

Typically used to check whether user input, system environment, etc. meet the criteria, in a shell script that provides interactive operations, or to determine whether a user entered a location that meets the requirements, the common options are as follows:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/50/wKiom1Y2AeKipCgUAADKlGEFfp4817.jpg "height="/>

For example: Judging the locale of the current system, when found not "en." US "Output prompt message" not en. US ":

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/75/50/wKiom1Y2AeLyp3v9AABWTCUvK7M875.jpg "height="/>

For example: In a shell script, the user is often required to enter "yes" or "no" to confirm a task, the operation shows the process of confirming the interaction, of course, the actual use will also be based on the variable "ACK" to take the value of the corresponding further operation.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/75/50/wKiom1Y2AeKh7LE7AACJ5ipHJ6g217.jpg "height="/>

4. Logic Test

is to judge the dependencies between two or more conditions, and when the system task depends on several different conditions, it is necessary to have a test process depending on whether these conditions are set up at the same time or if one of them is established. Common options are as follows:

&&: Logical AND, "and", the return value of the entire test command is 0 only if the first and the two conditions are true. When using the test command, && can be changed to "-a"

|| : Logical OR, means "or", only one of the two conditions is true, the return value of the entire test command is 0. When using the test command, the "| |" Can be changed to "-O"

! : Logical no, means "no", the return value of the entire test command is 0 only if the specified condition is not true.

"&&", "| |" Often also used to interval different command operations, which are similar in function.

For example, to determine whether the kernel version of the current Linux system is greater than 2.4, you can do the following.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/50/wKiom1Y2AeLjrCD1AACA0D3pWNY405.jpg "height="/>

Using the IF Condition statement

In a shell script, the IF statement is the most commonly used process control method, which is used to perform different actions (if ...) according to the test results of a particular condition. So...... )

1. Single-percent if statement

The "branch" of the If statement refers to the execution statement (one or more) that corresponds to the different test results. For a single-branch selection structure, the corresponding command sequence is executed only when the conditional test is established, otherwise no action is taken, and the single branch structure is as follows:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/75/50/wKiom1Y2AeKB3jsuAADnG7aEJXE353.jpg "height=" 332 "/>

In the above statement structure, the conditional test operation is either a "[conditional expression]" statement or another executable command sentence, which refers to one or more executable command lines, including nested use of if statements or other process control statements.

Single Branch execution flow: First judge the result of the condition test statement, the return value is 0, indicating that the condition is true, then execute the command sequence after then, continue to run the other script code until the end of the judgment of the FI, the return value is other, ignore then after the command sequence, Execute additional script code after jumping directly to fi.

Case:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/75/50/wKiom1Y2AeLTQwcqAAFl5gY6bY8802.jpg "height=" 274 "/>

2. Dual-Branch if statement

For the two-branch structure, it is required to execute different command sequences for the conditions of "condition" and "condition is not established" respectively.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/50/wKiom1Y2AeOSRpjFAAEtfXXWN10833.jpg "height=" 346 "/>

The execution flow of the two-branch if statement: first judge the result of the condition test operation, if it is true, then the command sequence 1 is executed, ignoring the else command sequence 2, until the end of the judgment is met, and if it is not, then the command sequence 1 is ignored and then immediately jumps to the command sequence 2 after the else. Until the end of the meeting fi is judged.

Case:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/75/50/wKiom1Y2AePCTjLJAAG9bwbjYHw483.jpg "height=" 332 "/>

3. Multi-Branch If statement

Since the IF statement can be based on the test results, do not set up separate operations, so can be nested use, to make multiple judgments. For example: first to determine whether a student scored a few, if a few are again judged whether higher than 90 points, the format is as follows:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/75/50/wKiom1Y2AeOBboZ2AAEUdnhNot0465.jpg "height="/>

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/50/wKiom1Y2AePgbWPkAACijhzIp8k775.jpg "height=" 291 "/>

Case:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/75/4D/wKioL1Y2Ah6AudF0AAHx9ViThzw172.jpg "height=" 340 "/>

This article is from the "Wang Gaoli" blog, make sure to keep this source http://wanggaoli.blog.51cto.com/10422005/1708574

Shell condition Test Statement and three if statements

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.