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.! #非