Shell if Else statement

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 If Truefi     

If  expression returns True,then, the statement will be executed, and if it returns FALSE, no statement is executed. The

must end with a fi closing if,fi is if the spelling is reversed and will be met later.

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

#!/bin/sha=tenb=#下面两种写法都是正确的, otherwise the notation is wrong if [$a = = $b]then    echo'A is equal to B'fi if  then    Echo ' a is not equal to B ' fi

Operation Result:

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

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

if [Expression]  Then  If Trueelse If True fi       

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

As an example:

#!/bin/SHa=Tenb= -if[$a = = $b]; Then   Echo "A is equal to B"Else   Echo "a is not equal to B"fiif[$a = =$b] Then    Echo "A is equal to B"Else    Echo "a is not equal to B"fi:<<EOF The following syntax is wrong, please noteif[$a = = $b] Then   Echo "A is equal to B"Else     Echo "a is not equal to B"fiEOF

Execution Result:

Equal to BA are not equal to B
3) If ... elif ... fi statement

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

if[Expression1 ] ThenStatement (s) to be executedifExpression1Istrueelif[Expression2 ] ThenStatement (s) to be executedifExpression2Istrueelif[Expression3 ] ThenStatement (s) to be executedifExpression3IstrueElseStatement (s) to be executedifNo expression istruefi

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:

#!/bin/SHa=Tenb= -if[$a = = $b]; Then   Echo "A is equal to B"elif[$a-gt $b]; Then    Echo "A is greater than B"elif[$a-lt $b]; Then    Echo "A is less than B"Else    Echo "None of the condition met"fi

Operation Result:

 less than B

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

 #!/bin/bash  if  test $[2  *1  +5 ]; then  echo    the-numbers is euqal!   "; fi ; 
#!/bin/bashif test $ (2*3)-eq $ (1+5thenEcho  'thenumbers is euqal! ' fi;

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

#!/bin/Bashnum1=$[2*3]num1_1=$[2*6]num2=$[1+5]num2_1=$[1+5]ifTest $[NUM1]-eq $[num2]; Then    Echo "The numbers is euqal!"Else    Echo 'The tow numbers is not equal!'fiifTest $num 1_1-eq $num 2_1; Then    Echo "The numbers is euqal!"Else    Echo 'The tow numbers is not equal!'fi

Output:

The numbers is euqal! The tow numbersis not equal!

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

Shell if Else 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.