The brackets inside the Shell (including the single and double brackets) can be used to test some conditions: arithmetic comparisons, such as whether a variable is 0, [$var-eq 0]. File property tests, such as whether a file exists, [-e $var], is a directory, [-D $var]. String comparisons, such as whether two strings are the same, [[$var 1 = $var 2]].
[] You can often use the test command instead, which is described later. Arithmetic Comparisons
To judge a variable or value by arithmetic conditions:
[$var-eq 0] # when $var equals 0 o'clock, return true
[$var-ne 0] # when $var not equal to 0 o'clock, return true
It is important to note that there must be a space between [and] and the operand , or an error will be given. For example, this will be the following error:
[$var-eq 0]
Other comparison operators:
operator |
meaning |
-gt |
Greater than |
-lt |
Less than |
-ge |
Greater than or equal to |
-le |
Less than or equal to |
You can test with multiple conditions with-a (and) or-O (or):
[$var 1-ne 0-a $var 2-gt 2] # Use logic with-a
[$var 1-ne 0-o $var 2-gt 2] # using logical OR-O
File System Properties Test
Use different criteria flags to test for different file system properties.
operator |
meaning |
[F $file _var] |
Variable $file _var is a normal file path or file name (file), returns the true |
[-X $var] |
Variable $var contains a file executable (execute), returns a true |
[-D $var] |
If the variable $var contains a directory, it returns a true |
[-E $var] |
Variable $var contains a file exists (exist), returns a true |
[-C $var] |
Variable $var contains a file that is a path to a character device file (character), returns the true |
[B $var] |
A variable $var contains a file that is a path (block) of a piece of device files, returns a true |
[-W $var] |
Variable $var contains a file that is writable (write), returns a true |
[-R $var] |
Variable $var contains a file that is readable (read), returns the true |
[-L $var] |
Variable $var contains a symbolic link, returns the true |
Use the following methods:
Fpath= "/etc/passwd"
if [-e $fpath]; then
echo File exits;
Else
Echo does not exit;
Fi
String Comparisons
In the case of string comparisons, it is best to use double brackets [[]]. Because single-bracket parentheses can cause some errors, it's best to avoid them.
Check to see if two strings are the same:
[[$str 1 = $str 2]]
Returns True when str1 equals str1 equals str2. In other words, str1 and str2 contain the same text. The single equals number can also be written as a double equals number, that is, the string above is equivalent to [[$str 1 = $str 2]].
Note = There is a space before and after, and if you forget to add a space, it becomes an assignment statement, not a comparison relationship.
Other comparisons of strings:
operator |
meaning |
[[$str 1!= $str 2]] |
If the str1 is not the same as the STR2, return the True |
[[z $str 1]] |
If the str1 is an empty string, returns the True |
[[-N $str 1]] |
Returns True if the str1 is a non-empty string |
Using logical operators && and | | You can easily combine multiple conditions, such as:
str1= "not Empty"
str2= ""
if [[-N $str 1]] && [[z $str 2]];
Then
Echo str1 is nonempty and str2 is empty string.
Fi
The test command can also never perform conditional detection, using test to avoid the use of too many parentheses, and testing conditions in [] can also be done through test.
If [$var-eq 0]; Then echo "True"; Fi
Equivalent to
if test $var-eq 0; Then echo "True"; Fi
Reference:
-[1] Linux Shell Script Introduction