Basic usage of Shell's If-else

Source: Internet
Author: User

The IF statement determines which branch to execute by using relational operators to determine whether an expression is true or false. The Shell has three kinds of if ... else statements:

    • If ... fi statement;
    • If ... else ... fi statement;
    • If ... else ... elif. Fi statement.
1) If ... else statement

Syntax for the IF ... else statement:

If [Expression]then   Statement (s) to was executed if expression is Truefi

If expression returns True,then, the statement is executed and no statement is executed if False is returned.

Finally must end with FI to close the if,fi is if the inverted spelling, the back will also meet.

Note: There must be a space between expression and square brackets ([]), or there will be a syntax error.

As an example:

  1. #!/bin/sh
  2. a = Ten
  3. b = -
  4. if [ $a = = $b ]
  5. Then
  6. Echo "A is equal to B"
  7. Fi
  8. if [ $a ! = $b ]
  9. Then
  10. Echo "A is not equal to B"
  11. Fi

Operation Result:

A is not equal to B
2) If ... else ... fi statement

Syntax for the If ... else ... fi statement:

If [Expression]then   Statement (s) to being executed if expression is TrueElse   Statement (s) to be executed if express Ion is not Truefi

If expression returns true, then the statement behind the then will be executed, otherwise the statement behind the else is executed.

As an example:

    1. #!/bin/sh
    2. a = Ten
    3. b = -
    4. if [ $a = = $b ]
    5. Then
    6. Echo "A is equal to B"
    7. Else
    8. Echo "A is not equal to B"
    9. Fi

Execution Result:

A is not equal to B
3) If ... elif ... fi statement

The If ... elif. FI statement can judge multiple conditions, with the syntax:

If [expression 1]then   Statement (s) to BES executed if expression 1 is trueelif [expression 2]then   Statement (s) To being executed if expression 2 is trueelif [expression 3]then   Statement (s) to was executed if Expression 3 is Trueels E   Statement (s) to being executed if no expression is Truefi

Which expression evaluates to true, executes the statement after which expression is executed, and if it is false, no statement is executed.

As an example:

  1. #!/bin/sh
  2. a = Ten
  3. b = -
  4. if [ $a = = $b ]
  5. Then
  6. Echo "A is equal to B"
  7. elif [ $a -gt $b ]
  8. Then
  9. Echo "A is greater than B"
  10. elif [ $a -lt $b ]
  11. Then
  12. Echo "A is less than B"
  13. Else
  14. echo "None of the condition met"
  15. Fi

Operation Result:

A is less than B


The IF ... else statement can also be written as a line and run as a command, like this:

    1. if Test $[2*3] -eq $[1+5]; then echo ' The numbers is equal! ' ; fi ;


The IF ... Else statement is also often used in conjunction with the Test command, as follows:

  1. NUM1 = $[2*3]
  2. num2 = $[1+5]
  3. if Test $[num1] -eq $[num2]
  4. Then
  5. echo ' The numbers is equal! '
  6. Else
  7. echo ' The numbers is not equal! '
  8. Fi

Output:

The numbers is equal!

The test command is used to check if a condition is true, similar to square brackets ([]).

Basic usage of the shell's If-else

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.