Basic syntax of if in shell and common judgment usage

Source: Internet
Author: User
Tags comparison

This article mainly introduces the basic syntax of if and common judgment usage in the shell, this article explains the basic syntax of IF, the judgment of the string, the judgment of the number, the judgment of the file attribute, the logic judgment and so on, the friend who needs can refer to the

If, for, while almost all the keywords in all programming languages, in Shell programming is no exception, where if is the most frequently used, because shell programming does not exist in the concept of objects (object), so in the comparison of the case of the judgment is the main string, The value of a number is judged by comparison:

The basic syntax of an IF

1.if and [have spaces between

2.[] There must also be a space between the conditions of judgment

3.] with; cannot have spaces between

 Two. The judgment of the string

1.if [str1=str2];then fi #当两个字符串相同时返回真

2.if [str1!=str2];then fi #当两个字符串不相等时返回真

3.if [-N str1];then fi #当字符串的长度大于0时返回真 (to determine whether a variable has a value)

4.if [z str1];then fi #当字符串的长度为0时返回真

Three. The judgment of numbers

1.int1-eq Int2 #int1和int2相等

2.int1-ne Int2 #int1不相等int2

3.INT1-GT Int2 #int1大于int2

4.int1-ge Int2 #int1大于等于int2

5.int1-lt Int2 #int1小于int2

6.int1-le Int2 #int1小于等于int2

 Four. The judgment of file attributes

1.-r file #用户可读为真

2.-w file #用户可写为真

3.-x file #用户可执行为真

4.-f file #文件存在且为正规文件为真

5.-d file #如果是存在目录为真

6.-c file #文件存在且为字符设备文件

7.-b file #文件存在且为块设备文件

8.-s file #文件大小为非0为真, you can determine whether the files are empty

9.-E file #如果文件存在为真

 Five. Logical judgment

1.-a #与

2.-o #或

3.! #非

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.