Summary of LinuxShell script introduction: 2

Source: Internet
Author: User
Summary of LinuxShell script introduction: 2. comparison and test procedure control are handled by comparison and test statements. We can use if, ifelse, and logical operators to perform the test, and use some comparison operators to compare data items. In addition, there is a test command that can also be learned and summarized using the... Linux Shell script strategy: 2. comparison and test procedures in process control are handled by comparison and test statements. We can use if, if else and logical operators to perform the test, and some comparison operators to compare data items. In addition, there is a test command that can be used for testing. Let's take a look at how to use these commands. Practice: [python] if condition: if condition; then commands; fi else if and else: if condition; then commands; elif condition; the then commands else commands fi if and else statements can be nested. The condition judgment part of if may become long, but it can be simplified using logical operators: [python] [condition] & action; # if condition is true, then, execute action [condition] | action; # if condition is false, execute action & is a logical and operator. | it is a logical or operator. Arithmetic comparison conditions are usually placed in closed brackets. Note that there is a space between [or] and the operand. If this parameter is not added, the script reports an error. For example, [python] [$ var-eq 0] or [$ var-eq 0] determines the arithmetic condition of the variable value: [python] [$ var-er 0] # returns true when $ var is equal to 0 [$ var-ne 0] # When $ var is not 0, return other important operators: [python]-gt: greater than-lt: less than-ge: greater than or equal to-le: less than or equal to string comparison when using string comparison, it is best to use double brackets, because sometimes an error occurs when using single brackets, so it is best to avoid them. [Python] [[$ str1 = $ str2]: Returns true if str1 equals str2. That is to say, str1 and str2 contain the same text [[$ str1 = $ str2]: this is another way to check whether the string is equal [[$ str1! = $ Str2]: if str1 and str2 are different, we can also check the alphabetic order of the string, as shown below: [python] [[$ str1> $ str2]: if the alphabetic order of str1 is greater than that of str2, true [[$ str1 <$ str2] is returned: if the alphabetic order of str1 is smaller than that of str2, true [[-z $ str1] is returned. if str1 contains a null string, returns the true [[-n $ str1]. if str1 contains a non-null string, returns the true result. Note that there is a space before and after the = sign. If you forget to add spaces, it is not a comparison, but a value assignment statement.

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.