[Linux] Structured command IF

Source: Internet
Author: User

The syntax structure is as follows:

1. If-then statements

IF-thenstatement if  command   #根据conmmand的退出状态码, select Execute statement then    commandsfie.g.#! usr/bin/Bashtestuser=Richif grep $testuser/etc/passwdthen        for  user $testuser is:        -a/home/$testuser/fi

2. If-then-else statements

1 #!usr/bin/bash2if  command     #根据conmmand的退出状态码, select Execute statement 3  Then 4     Commands5Else6    commands7 fi

3. Nesting if (elif)

1#!usr/bin/Bash2 ifCommand3  Then4 commands5 elif Command6  Then7 commands8 elif Command9  ThenTen commands One Else A commands -Fi

4. Test command (single testing condition)

1 # There are 3 main types of test conditions2*Numerical comparison3*string comparison4*file Comparison5 6#usr/bin/Bash7 iftest Condition8  Then9 commandsTen fi One  A#!usr/bin/Bash - if[condition] # condition before and after space -  Then the commands -Fi

5. Compound condition test

1 # and test: Two conditions must be met (true)2 if[Condition1] &&[Condition2]3  Then4 commands5 fi6 7 8 #OR测试: Satisfies any one of the conditions (either true)9 if[Conditon1] | |[Condition2]Ten  Then One commands AFi

6. If-then Advanced Features

1 # Double parenthesis: for mathematical expressions2 ((expression))3 4 if((expression))5  Then6 commands7 ((expression))8 fi9 Ten # Two-way brackets: for string expressions (e.g. regular expressions) One [[expression]] A  - if[[expression]] -  Then the commands -Fi

7. Case command

1 # Compare the specified variable to a different pattern 2 3 4  Case inch 5 Pattern1 | pattern2) 6     commands1;; 7 pattern3) 8     commands2;; 9 *)    commands3;;  Esac

[Linux] Structured command IF

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.