Man test can see the string
-n string the length of STRING is nonzero STRING equivalent to -n string -z string the length of string is zero STRING1 = STRING2 the strings are equal STRING1 != STRING2 the strings are not equal INTEGER1 -eq INTEGER2 integer1 is equal to integer2 integer1 -ge Integer2 integer1 is greater than or equal to integer
String test operators:
Common string test operators
-Z "string", if the string length is 0 true,-Z can be understood as zero
-N "string", if the string length is 0 true,-N can be understood as no zero
"String 1" = "String 2", if string 1 equals string 2 is true, you can use "= =" instead of "="
"String 1"! = "String 2", if string 1 is not equal to string 2 is true, but can not use "! = = "Replace"! =
Special attention:
1. The string test operation symbol in the above table must be enclosed in double quotation marks.
2. There must be spaces at both ends of the comparison symbol
3. String or string variable comparisons are double-quoted and then compared
4, string or string variable comparison, it is best to compare symbols at both ends have spaces.
Practice:
[[email protected] ~]# [ -n "abc" ]& &echo 1| | echo 01[[email protected] ~]# [ -n "" ]&&echo 1| | echo 0 0[[email protected] ~]# [ -z "ABC" ]&&echo 1| | echo 0 0[[email protected] ~]# [ ! -z "ABC" ]&&echo 1| | echo 01[[email protected] ~]# [ -n "$test" ]&&echo 1| | echo 01[[email protected] ~]# test= "Oldboy" [[Email protected] ~]# [ -n "Test" ]&&echo 1| | echo 01[[email protected] ~]# [ -n "$test" ]&&echo 1| | echo 01[[email protected] ~]# [ "$test" = "Oldboy" ]&&echo 1| | Echo 0 1
This article is from the "Sandshell" blog, make sure to keep this source http://sandshell.blog.51cto.com/9055959/1964139
Shell string Expression Introduction-practice and enterprise case Scripting anatomy