Shell script: If-else

Source: Internet
Author: User

The Shell has three kinds of if ... else statements:

1.if. FI statement;

2.if ... else ... fi statement;

3.if. Elif ... else .... fi statement.

If ... fi statement

Syntax for the IF ... else statement:

if [Expression]  Then If Truefi     

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

If ... else ... fi statement

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

if [Expression]  Then  If Trueelse If True fi       

If ... elif ... else ... fi statement

Syntax for the If ... else ... elif. Fi statement

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

If-else statement that mates with test

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

num1=1num2=2if test ${num1}-eq ${num2}then    Echo  'thenumbers is equal! ' Else    Echo ' The numbers is not equal! ' fi

Shell script: 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.