[Shell] if common judgment Conditions
IF you have written a simple shell script before the judgment, it is not a job O & M, and the work related to the system is relatively small, so it is not very skilled.
Recently, as the system has encountered various messy problems and no one has helped me, I have to write shell scripts myself, all of which are basic scripts, however, because the shell syntax is somewhat different from the general advanced language, you need to systematically look at the commonly used part. The if statement is very important.
There are a lot of such articles, but they will be included if you have any experience. We will try it later under subtotal.
Basic Structure: if statement blocks must end with if
if conditionthen statementselif condition then statementselse statementsfi
Tips: note that
There must be a space on the left and right of the judgment condition square brackets and the judgment statement between the if Statements (several times wrong)
##### Determine whether the characters are equal
#! /Bin/bashsystem = 'uname-S' if [$ system = "Linux"] # square brackets have spaces on both sides, space is also required on both sides of the equal sign then echo "Linux" else echo "Other system" fi
Common types of judgment and writing (analogous string)
1 string judge str1 = str2 when two strings have the same content and length, it is true str1! = Str2: true when the string str1 and str2 are different-n str1: true when the string length is greater than 0 (the string is not empty)-z str1: true when the string length is 0 (empty string) str1 when str1 is not null, true 2 digits are judged int1-eq int2 two numbers are equal to true int1-ne int2 two numbers are not equal to true int1-gt int2 int1 greater than int2 true int1-ge int2 int1 greater than or equal to int2 true int1-lt int2 int1 less than int2 true int1-le int2 int1 less than or equal to int2 true 3 file judgment-r file user readable true-w file user writable true-x file users can execute the "true-f file" file as a regular file. The "True-d file" file is a directory. The "True-c file" file is a special character file. The "True-B file" file is a special file. when the file size is not 0, it is true-t file. When the file descriptor (default value: 1) specified settings When the slave is a terminal, 4 complex logic judgments-a and-o or! Non
Tips:
*-Eq-ne-lt-nt can only be used as an integer and is not applicable to strings. A string equals to a value =
* = The value is assigned elsewhere, and the value is equal to the string in if []. The shell does not contain =, which is equal to the value in C.
* An integer conditional expression greater than, less than, and no> and <in shell is treated as angle brackets, with only-ge,-gt,-le, and lt
[Reference] (http://rfyiamcool.blog.51cto.com/1030776/738624)
This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/38458889