[Shell] If common judgment Conditions

Source: Internet
Author: User
If judgment
I have also written a simple shell script before, and it is not transferred to O & M. It has less work related to the system, so it is not very skilled.
Recently, as the system has encountered various messy problems and no one has helped me, I have to write shell scripts myself, all of which are basic scripts, however, because the shell syntax is somewhat different from the general advanced language, you need to systematically look at the commonly used part. The IF statement is very important.
There are a lot of such articles, but they will be included if you have any experience. We will try it later under subtotal.


Basic Structure: If statement blocks must end with if
if conditionthen    statementselif condition    then statementselse    statementsfi

TIPS: note that
There must be a space on the left and right of the judgment condition square brackets and the judgment statement between the IF Statements (several times wrong)

##### Determine whether the characters are equal
#! /Bin/bashsystem = 'uname-S' if [$ system = "Linux"] # square brackets have spaces on both sides, space is also required on both sides of the equal sign then Echo "Linux" else echo "Other System" fi



Common types of judgment and writing (analogous string)
1 string judge str1 = str2 when two strings have the same content and length, it is true str1! = Str2: true when the string str1 and str2 are different-N str1: true when the string length is greater than 0 (the string is not empty)-Z str1: true when the string length is 0 (empty string) str1 when str1 is not null, true 2 digits are judged int1-EQ int2 two numbers are equal to true int1-ne int2 two numbers are not equal to true int1-GT int2 int1 greater than int2 true int1-ge int2 int1 greater than or equal to int2 true int1-lt int2 int1 less than int2 true int1-Le int2 int1 less than or equal to int2 true 3 File judgment-r file user readable true-W file user writable true-x file users can execute the "true-F file" file as a regular file. The "True-D file" file is a directory. The "True-C file" file is a special character file. The "True-B file" file is a special file. when the file size is not 0, it is true-T file. When the file descriptor (default value: 1) specified settings When the slave is a terminal, 4 complex logic judgments-A and-O or! Non

TIPS:
*-Eq-ne-lt-NT can only be used as an integer and is not applicable to strings. A string equals to a value =
* = The value is assigned elsewhere, and the value is equal to the string in if []. The shell does not contain =, which is equal to the value in C.
* An integer conditional expression greater than, less than, and no> and <in shell is treated as angle brackets, with only-Ge,-GT,-Le, and LT



[Reference] (http://rfyiamcool.blog.51cto.com/1030776/738624)

This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/38458889



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.