Operator |
Describe |
Example |
File comparison Operators |
-e filename |
True if filename exists |
[-e/var/log/syslog] |
-D filename |
True if filename is a directory |
[-d/tmp/mydir] |
-F filename |
True if filename is a regular file |
[-f/usr/bin/grep] |
-L filename |
True if filename is a symbolic link |
[-l/usr/bin/grep] |
-R filename |
True if filename is readable |
[-r/var/log/syslog] |
-W filename |
True if filename is writable |
[-w/var/mytmp.txt] |
-X filename |
True if filename is executable |
[-l/usr/bin/grep] |
Filename1-nt filename2 |
If filename1 is newer than filename2, it is true |
[/tmp/install/etc/services-nt/etc/services] |
Filename1-ot filename2 |
If filename1 is older than filename2, it is true |
[/boot/bzimage-ot Arch/i386/boot/bzimage] |
String comparison operators (note the use of quotation marks, which is a good way to prevent whitespace from disturbing the code) |
-Z String |
True if string length is zero |
[-Z "$myvar"] |
-N String |
True if string length is nonzero |
[-N "$myvar"] |
string1 = string2 |
True if string1 is the same as string2 |
["$myvar" = "One of the three"] |
String1! = string2 |
True if string1 is different from string2 |
["$myvar"! = "one of the three"] |
Arithmetic comparison operators |
Num1-eq num2 |
Equals |
[3-eq $mynum] |
Num1-ne num2 |
Not equal to |
[3-ne $mynum] |
Num1-lt num2 |
Less than |
[3-lt $mynum] |
Num1-le num2 |
Less than or equal to |
[3-le $mynum] |
NUM1-GT num2 |
Greater than |
[3-gt $mynum] |
Num1-ge num2 |
Greater than or equal to |
[3-ge $mynum]
|
Arithmetic operators
+-*/% indicates subtraction and take-rest operations
+ = = *=/= with C language meaning
Bitwise operators
> >>= means move one operation left and right
& &= | |= indicates bitwise AND, bit, or operation
~ ! Represents a non-operational
^ ^= represents XOR or manipulation
Relational operators
= = = = = Greater than, less than, greater than or equal, less than equals, equals, not equal to operation
&& | | Logical AND logical OR operational
Test command
The test command is used to check if a condition is true, and it can test for 3 aspects of numeric, character, and file, with its tester and corresponding functions as follows.
(1) Numerical test:
-eq is equal to true.
-ne is not equal to true.
-GT is greater than true.
-ge is true if it is greater than or equal.
-lt is less than true.
-le is less than or equal to true.
(2) String test:
= Equals is true.
! = is not equal to true.
-Z String string length pseudo is true.
The-n string string is true if the length is not pseudo.
(3) file test:
-e file name is true if the file exists.
-r file name True if the file exists and is readable.
-W file name True if the file exists and is writable.
-X file name is true if the file exists and is executable.
-S file name True if the file exists and has at least one character.
-d file name if the file exists and is true for the directory.
-F filename is true if the file exists and is a normal file.
-C file name True if the file exists and is a character-type special file.
-B file name True if file exists and is a block special file |
Conditional variable substitution:
The Bash shell can make conditional substitutions of variables, replacing only when certain conditions occur, replacing
The condition is placed in {}.
(1) ${value:-word}
When the variable is undefined or the value is empty, the return value is the contents of Word, otherwise the value of the variable is returned.
(2) ${value:=word}
Similar to the former, except that if the variable is undefined or the value is empty, the word value is returned to value at the same time
(3) ${value:?message}
If the variable is assigned a value, replace it normally. Otherwise, send message messages to the standard error output (if this substitution appears in the shell program, the program will terminate running)
(4) ${value:+word}
If the variable is assigned, its value is replaced with Word, otherwise no substitution is made
(5) ${value:offset}
${value:offset:length} extracts substrings from the variable, where offset and length can be arithmetic expressions.
(6) ${#value}
The number of characters in a variable
(7) ${value#pattern}
${value# #pattern}
Remove the part of value that matches the pattern in the condition that the beginning of value matches the pattern
#与 # #的区别在于一个是最短匹配模式, one is the longest matching pattern.
(8) ${value%pattern}
${value%%pattern}
In (7) similar, just from the tail of value in pattern matching,% and percent of the difference between # and # #一样
(9) ${value/pattern/string}
${value//pattern/string}
Replace the contents of the variable with the content of the pattern match with the contents of the string, the difference between//and the
Note: In the above condition variable substitution, except (2), the rest does not affect the value of the variable itself
#!/bin/bash
var1= "1"
Var2= "2"
The following is the " and " operator-A, which is also noted with a Test command, and a semicolon following the IF condition
if test $var 1 = "1"-a $var 2 = "2"; Then
echo "Equal"
Fi
The following is the " or " operator- o, with one that can be true
if test $var 1! = "1"-o $var 2! = "3"; Then
echo "Not Equal"
Fi
Here is the " not " operator !
The IF condition is true when executed, if used! operator, the original expression must be false
if! Test $var 1! = "1"; Then
echo "Not 1"
Fi
The above three if are true, so three echo will print
Example:
[JavaScript]View Plaincopyprint?
- #!/bin/sh
- Aa="August, "
- bb="August, 20122"
- cc="123"
- dd="123"
- #-O
- If [ "$aa" = "$bb"-O "$cc" = "$dd"]; Then
- echo "yes"
- Else
- echo "No"
- Fi
- #-A and!
- If [ "$AA"! = "$bb"-a "$cc" = "$dd"]; Then
- echo "yes"
- Else
- echo "No"
- Fi
Operation Result:True
------------------------------------------------------------------------------------------------------
Shell string comparison, judging whether it is a number
Binary comparison operators, comparing variables or comparing numbers. Note the difference between a number and a string.
1 integer Comparisons
-eq equals, such as: if ["$a"-eq "$b"] -ne not equal to, such as: if ["$a"-ne "$b"] -GT greater than, such as: if ["$a"-gt "$b"] -ge greater than equals, such as: if ["$a"-ge "$b"] -lt less than, such as: if ["$a"-lt "$b"] -le less than equals, such as: if ["$a"-le "$b"] < less (requires double brackets), such as: (("$a" < "$b") <= is less than or equal (requires double brackets), such as: (("$a" <= "$b")) > Greater than (requires double brackets), such as: (("$a" > "$b") >= greater than or equal (requires double brackets), such as: (("$a" >= "$b"))
Integer comparison instance [CSS]View Plaincopyprint?
- #!/bin/bash
- file=' Folder_url_top24/url_usa_top24_0 '
- Filesize= ' ls-l folder_url_top24/url_usa_top24_0 | awk-f ' ["] ' {print $} '
- Filesize=
- #while [!-f $file-o "$fileSize"-lt " $FILESIZE"]
- #while [!-f $file-o "$fileSize"-lt ]
- while (("$fileSize" < )
- Do
- Echo "Down again ..."
- Done
Among them, the following three kinds of integer comparisons are established:1) while [!-F $file-O "$fileSize"-lt "$FILESIZE"] 2) while [!-F $file-O "$fileSize"-lt 1000] 3) (("$fileSize" < 1000)) It is recommended to use the first
2 string Comparisons = equals, such as: if ["$a" = "$b"] = = equals, such as: if ["$a" = = "$b"], and = equivalent Note: The behavior of the = = function is different in [[]] and [], as follows: 1 [[$a = = z*]] # if $ A starts with "Z" (pattern match) then true 2 [[$a = = "z*"] # if $ A equals z* (character match) Then the result is true 3 4 [$a = = z*] # File globbing and word splitting will occur 5 ["$a" = = "z*"] # If a $ A equals z* (character match) Then the result is true A little explanation, about file globbing is a shorthand for the document, such as "*.c" is, again like ~ also. But file globbing is not a strict regular expression, although in most cases the structure is more like. ! = does not equal, such as: if ["$a"! = "$b"] This operator will use pattern matching in the [[]] structure. < less than, in ASCII alphabetical order. such as: if [["$a" < "$b"] If ["$a" \< "$b"] Note: the "<" in the [] structure needs to be escaped. > Greater than, in ASCII alphabetical order. such as: if [["$a" > "$b"] If ["$a" \> "$b"] Note: the ">" in the [] structure needs to be escaped. Refer to Example 26-11 for an example of this operator application. The-Z string is "null". That's the length of 0. -N string not "null" Determine if the number of arguments the shell has passed is empty: [Python]View Plaincopyprint?
- #!/bin/bash
- port=6379 # command Line no parameters, default specified port number is 6379
- If [$#-ge 1]; then # the number of command-line arguments is greater than or equal to 1, use the passed-in parameter port
- port=$1 # Get the specified port number
- Fi
- echo "Redis port: $port"
- Redis-cli-h 172.1628. 10.114-p $port
String comparison Example: If ["$var 1" = "$var 2"] Code: [CSS]View Plaincopyprint?
- #!/bin/sh
- Aa="August, "
- bb="August, "
- If [ "$aa" = "$bb"]; Then
- echo "yes"
- Else
- echo "No"
- Fi
Determine substring inclusion relationship: =~ Code: [Python]View Plaincopyprint?
- a1="Ithomer"
- a2="Ithomer.net"
- a3="Blog.ithomer.net"
- if [[ "$a 3" =~ "$a 1"]]; Then
- echo "$a 1 is a substring of $a3! "
- Else
- echo "$a 1 is not a substring of $a3! "
- Fi
- if [[ "$a 3" =~ "$a 2"]];then
- echo "$a 2 is a substring of $a3! "
- Else
- echo "$a 2 is not a substring of $a3! "
- Fi
Attention: The use of-N in the [] structure must be used to test the variable. Use a string that is not ""! -Z or the string itself that is not referenced by "" is placed in the [] structure. Although it is generally possible to work, it is unsafe. It's a good habit to use "" to test strings. awk ' {print $} ' class.txt | grep ' ^[0-9.] ' > Res |
Recommended reference:
Linux Shell function return value
Linux Shell comparison operators (recommended)
Linux Shell Learning Brief summary (recommended)
Shell Learning notes----If condition judgment, judging condition
Original address: http://blog.csdn.net/ithomer/article/details/6836382
The shell comparison operator for Linux