Shell script (2)-if statement

Source: Internet
Author: User

If statement
1. What is a conditional test operation?
To make a shell script "intelligent", the problem is to differentiate between different situations to determine what action to take. The shell environment, based on the return status value ($?) after the command executes, determines whether the execution succeeds, and when the return value is 0 o'clock, the execution succeeds, otherwise it indicates a failure or an exception. You can test specific conditions by using the tool-test command for the conditional test action.
2. Condition Test classification
1) file test: Determine whether the file or directory is based on the given path.
Common options:
-D: Test for Directory
-F: Test for file
-E: Test whether the file or directory exists
-R: Tests whether the current user has Read permissions
-W: Tests whether the current user has write permissions
-X: Tests whether the current user has Execute permissions

2) Integer value comparison: The integer value comparison is based on two given integer values, judging the relationship between the first number and the second number
Common options:
-eq: Equals
-LT: Less than
-GT: Greater Than
-ne: Not equal to
-ge: greater than or equal to
-le: Less than or equal to

3) string comparison: Character creation comparisons are typically used to check user input, system conditions, and so on. In a shell environment that is interactive, it can also be used to determine if the location parameter entered by the user is in compliance with the requirements
Common operations:
=: The first character is the same as the second one
! =: The first is not the same as the second character
-Z: Check if character creation is empty

4) Logic test: Determine the dependency between two conditions or multiple conditions
&&: Indicates "and", the return value is 0 only if the first and the two conditions are true
| | : means "or", as long as one of the two conditions is true, the return value is 0
! : Indicates "no"
3. If statement
1) Single Branch if statement
If condition test action
Then
Command sequence
Fi
2) Dual-branch if statement
If condition test action
Then
Command sequence 1
Else
Command Sequence 2
Fi
3) Multi-branch if statement
If condition test action
Then
Command sequence 1
elif Condition Test Action 2
Then
Command Sequence 2
。。。
Else
Command sequence
Fi
4. Application examples
1) Create a new script to test the score

2) script

3) test Script

4) Script Interpretation
First condition Test action: When the input score is greater than or equal to 90, less than or equal to 100, the output "excellent"; Read-p is used to assign a value to the "GRADE" variable.

Shell script (2)-if statement

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.