The IF statement in the shell

Source: Internet
Author: User

Syntax format

If Command;then

Commands

Fi

The command contains the following:

Shell command

Any shell command, if the shell command returns 0, represents true, otherwise, represents false. and multiple command can be used as the judgment condition of if, that can be written as:

If Command1;command2;command3;then

Commands

Fi

At this point, if Command3 returns a result of 0, run commands, otherwise not run, although Command1 and Command2 will also run, but the return result of their run has no effect

Test condition or [condition]

The two are equivalent, where the condition can be a numeric comparison, which can be a string comparison, which can be a file comparison:

Number comparison

NUM1 eq num2 equals

Num1 ne num2 Not equal to

NUM1 GT num2 Greater than

Num1 GE num2 greater than or equal

NUM1 LT num2 Less than

Num1 le Num2 is less than or equal to

string comparison

str1 = str2 equals

Str1! = str2 Not equal to

str1 > str2 greater than

STR1 < str2 less than

Whether the length of the-n str str is greater than 0

The length of the-Z str str is 0

File comparison

Whether the-D file file is a directory

Whether the-F file files are

Whether the-e file file exists

-S file file exists and is not empty

-r file file exists and is readable

-W file file exists and is writable

-x file file exists and can be executed

-o file file exists and belongs to the current user

-G file file exists and belongs to the initial group (also called default group) where the current user resides

File1-nt file2 file1 is newer than file2

File1-ot File2 file1 is older than file2

Condition can also be compounded by && or | | Connection:

[Condition1] && [Condition2]

[Condition1] | | [Condition2]

The IF statement in the shell

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.