In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what are the comparison operators of shell in Linux. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
Operator description sample file comparison operator-e filename if filename exists, true [- e / var/log/syslog]-d filename if filename is a directory, true [- d / tmp/mydir]-f filename if filename is a regular file, true [- f / usr/bin/grep]-L filename if filename is a symbolic link, true [- L / usr/bin/grep]-r filename if filename is readable True [- r / var/log/syslog]-w filename if filename is writable, true [- w / var/mytmp.txt]-x filename if filename is executable, true [- L / usr/bin/grep] filename1-nt filename2 if filename1 is newer than filename2, true [/ tmp/install/etc/services-nt / etc/services] filename1-ot filename2 if filename1 is older than filename2 True [/ boot/bzImage-ot arch/i386/boot/bzImage] string comparison operator (note the use of quotation marks, which is a good way to prevent spaces from disturbing code)-z string true [- z "$myvar"]-n string if string length is zero, true [- n "$myvar"] string1 = string2 if string length is non-zero True ["$myvar" = "one two three"] string1! = string2 if string1 is different from string2, then ["$myvar"! = "one two three"] arithmetic comparison operator Num1-eq num2 equals [3-eq $mynum] num1-ne num2 is 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 operator
+-* /% means addition, subtraction, multiplication, division and remainder.
+ =-= * = / = same as the meaning in C language
Bit operator
> = indicates that the bit moves one bit left and right.
& & = | = indicates bitwise and, bitwise or operation
~! Represents a non-operation
^ = represents an XOR operation
Relational operator
= =! = means greater than, less than, greater than or equal to, less than or equal to, not equal to operation
& & | Logic and, logic or operation
Test command
The test command is used to check whether a condition is true. It can be tested in three aspects: numeric values, characters and files. The test characters and corresponding functions are as follows.
(1) numerical test:
True if-eq equals.
True if ne is not equal to.
-true if gt is greater than.
-true if ge is greater than or equal to.
-true if lt is less than.
-true if le is less than or equal to.
(2) string testing:
It is true if it equals.
! = unequal is true.
The-z string length false is true.
-n string length is true if the string length is not false.
(3) document testing:
The-e file name is true if the file exists.
The-r file name is true if the file exists and is readable.
The-w file name is true if the file exists and is writable.
The-x file name is true if the file exists and is executable.
The-s file name is true if the file exists and has at least one character.
The-d file name is true if the file exists and is a directory.
The-f file name is true if the file exists and is a normal file.
The-c file name is true if the file exists and is a character-type special file.
-b file name is true if the file exists and is a block special file
Conditional variable substitution:
Bash Shell can carry out conditional substitution of variables, that is, only when certain conditions occur.
The conditions are placed in {}.
(1) ${value:-word}
When the variable is undefined or the value is empty, the return value is the content 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 is assigned to value while returning the value of word
(3) ${value:?message}
If the variable has been assigned, replace it normally. Otherwise, send the message message to the standard error output (if this replacement appears in the Shell program, the program will stop running)
(4) ${value:+word}
If the variable has been assigned, its value will be replaced with word, otherwise no replacement will be made.
(5) ${value:offset}
${value:offset:length} extracts substrings from variables, where offset and length can be arithmetic expressions.
(6) ${# value}
The number of characters of the variable
(7) ${value#pattern}
${value##pattern}
Remove the part of the value that matches the pattern, provided the beginning of the value matches the pattern
The difference between # and # is that one is the shortest matching pattern and the other is the longest matching pattern.
(8) ${value%pattern}
${value%%pattern}
Similar to (7), except that the tail of value matches pattern, and the difference between% and% is the same as # and #
(9) ${value/pattern/string}
${value//pattern/string}
Replace the content of the variable, and replace the part that matches pattern with the content of string. The difference between / and / / is the same as above.
Note: in the above conditional variable substitution, except for (2), the value of the variable itself is not affected.
#! / bin/bash
Var1= "1"
Var2= "2"
Here is the "and" operator-a, plus note that a test command is fine, and there is a semicolon after the if condition
If test $var1 = "1"-a $var2 = "2"; then
Echo "equal"
Fi
The following is the "or" operator-o, one of which is true
If test $var1! = "1"-o $var2! = "3"; then
Echo "not equal"
Fi
Here is the "not" operator!
The if condition is executed when true, if used! Operator, then the original expression must be false
If! Test $var1! = "1"; then
Echo "not 1"
Fi
All three if are true, so all three echo will print.
Example:
#! / bin/shaa= "August 15, 2012" bb= "August 15, 20122" cc= "123" dd= "123" #-oif ["$aa" = "$bb"-o "$cc" = "$dd"]; then echo "yes" else echo "no" fi#-an and! if ["$aa"! = "$bb"-a "$cc" = "$dd"]; then echo "yes" else echo "no" fi
Running result:
True
-
Compare shell strings and judge whether they are numeric or not
Binary comparison operator, comparison variable or comparison number. Pay attention to the difference between numbers and strings.
1 integer comparison
-eq equals, for example: if ["$a"-eq "$b"]
-ne is not equal to, for example: if ["$a"-ne "$b"]
-gt is greater than, for example: if ["$a"-gt "$b"]
-ge is greater than or equal to, such as: if ["$a"-ge "$b"]
-lt is less than, for example: if ["$a"-lt "$b"]
-le is less than or equal to, such as: if ["$a"-le "$b"]
< 小于(需要双括号),如:(("$a" < "$b")) "$b")) >= greater than or equal (double parentheses are required), such as: (("$a" > = "$b"))
Integer comparison example
#! / bin/bashfile='folder_url_top24/url_usa_top24_0'fileSize= `ls-l folder_url_top24/url_usa_top24_0 | awk-F'["]'{print $5} '`FILESIZE=1000#while [!-f $file-o" $fileSize "- lt" $FILESIZE "] # while [!-f $file-o" $fileSize "- lt 1000] while ((" $fileSize ")
< 1000)) do echo "down again..."done 其中,下面三种整数比较都成立: 1) while [ ! -f $file -o "$fileSize" -lt "$FILESIZE" ] 2) while [ ! -f $file -o "$fileSize" -lt 1000 ] 3) (("$fileSize" < 1000)) 推荐使用第一种 2 字符串比较 = 等于,如:if [ "$a" = "$b" ] == 等于,如:if [ "$a" == "$b" ],与=等价 注意:==的功能在[[]]和[]中的行为是不同的,如下: 1 [[ $a == z* ]] # 如果$a以"z"开头(模式匹配)那么将为true 2 [[ $a == "z*" ]] # 如果$a等于z*(字符匹配),那么结果为true 3 4 [ $a == z* ] # File globbing 和word splitting将会发生 5 [ "$a" == "z*" ] # 如果$a等于z*(字符匹配),那么结果为true 一点解释,关于File globbing是一种关于文件的速记法,比如"*.c"就是,再如~也是. 但是file globbing并不是严格的正则表达式,虽然绝大多数情况下结构比较像. != 不等于,如:if [ "$a" != "$b" ] 这个操作符将在[[]]结构中使用模式匹配. < 小于,在ASCII字母顺序下.如: if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ] 注意:在[]结构中""需要被转义. 具体参考Example 26-11来查看这个操作符应用的例子. -z 字符串为"null".就是长度为0 -n 字符串不为"null" 判断shell传入的参数个数是否为空: #!/bin/bashport=6379 # 命令行没参数,默认指定端口号为 6379if [ $# -ge 1 ]; then # 命令行参数个数大于等于1,则使用传入的参数port port=$1 # 获取指定端口号fi echo "redis port: $port"redis-cli -h 172.1628.10.114 -p $port 字符串比较实例: if [ "$var1" = "$var2" ] 代码: #!/bin/shaa="August 15, 2012"bb="August 15, 2012"if [ "$aa" = "$bb" ]; then echo "yes"else echo "no"fi 判断子字符串包含关系: =~ 代码: a1="ithomer"a2="ithomer.net"a3="blog.ithomer.net"if [[ "$a3" =~ "$a1" ]]; then echo "$a1是$a3的子串!"else echo "$a1不是$a3的子串!"fiif [[ "$a3" =~ "$a2" ]];then echo "$a2是$a3的子串!"else echo "$a2不是$a3的子串!"fi 注意: 使用-n在[]结构中测试必须要用""把变量引起来.使用一个未被""的字符串来使用! -z或者就是未用""引用的字符串本身,放到[]结构中。虽然一般情况下可以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯. awk '{print $2}' class.txt | grep '^[0-9.]' >Res
The above is all the content of the article "what are the comparison operators of shell in Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.