Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare Linux shell operators

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you a comparison of how to carry out the Linux shell operator. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Operator

Describe the sample file comparison operator

-efilename if filename exists, true [- e / var/log/syslog]-dfilename if filename is directory, true [- d / tmp/mydir]-ffilename if filename is regular file, true [- f / usr/bin/grep]-Lfilename if filename is symbolic link, true [- L / usr/bin/grep]-rfilename if filename is readable, true [- r / var/log/syslog]-wfilename if filename is writable True [- w / var/mytmp.txt]-xfilename if filename is executable, true [- L / usr/bin/grep] filename1-ntfilename2 if filename1 is newer than filename2, true [/ tmp/install/etc/services-nt / etc/services] filename1-otfilename2 if filename1 is older than filename2, true [/ boot/bzImage-ot arch/i386/boot/bzImage] string comparison operator (note the use of quotation marks, this is a good way to prevent spaces from disturbing code)

-zstring if string length is zero, true [- z "$myvar"]-nstring if string length is non-zero, true [- n "$myvar"] string1=string2 if string1 is the same as string2, true ["$myvar" = "one two three"] string1 arithmetic comparison operator if string1 is different from string2, then true ["$myvar"! = "one two three"] arithmetic comparison operator

Num1-eqnum2 equal to [3-eq $mynum] num1-nenum2 is not equal to [3-ne $mynum] num1-ltnum2 less than [3-lt $mynum] num1-lenum2 less than or equal to [3-le $mynum] num1-gtnum2 greater than [3-gt $mynum] num1-genum2 greater than or equal to [3-ge $mynum]

The arithmetic operator +-* /% means addition, subtraction, multiplication, division and remainder + =-* = / = the same as in C language.

Bit operator

The relational operator =! = means greater than, less than, greater than or equal to, less than or equal to, less than or 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 a certain condition occurs, and the replacement condition is 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} is 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) the number of characters in the ${# value} variable

(7) ${value#pattern} ${value##pattern} remove the part of the value that matches the pattern, provided that 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} is similar to (7), except that it matches pattern from the tail of value, and the difference between% and% is the same as # and #

(9) ${value/pattern/string} ${value//pattern/string} to 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 rest does not affect the value of the variable itself.

#! / bin/bashvar1= "1" var2= "2" is the "and" operator-a, and note that a test command is fine, and there is a semicolon if test $var1= "1"-a $var2= "2" after the if condition; under the then echo "equal" fi is the "or" operator-o, and if one is true, you can if test $var1! = "1"-o $var2! = "3" Below the then echo "not equal" fi is the "not" operator! The if condition is executed when true, if used! Operator, then the original expression must be falseif! 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"]; thenecho "yes" elseecho "no" fi#-an and! if ["$aa"! = "$bb"-a "$cc" = "$dd"] Thenecho "yes" elseecho "no" fiducial racket; awk "yes" elseecho "no" fielding racket; awk "file" o "$fileSize"-lt "$FILESIZE"] # while [!-f $file-o "$fileSize"-lt 1000] while ("$fileSize")

< 1000))doecho "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,则使用传入的参数portport=$1# 获取指定端口号fiecho "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" ]; thenecho "yes"elseecho "no"fi   判断子字符串包含关系: =~ 代码: a1="ithomer"a2="ithomer.net"a3="blog.ithomer.net"if [[ "$a3" =~ "$a1" ]]; thenecho "$a1是$a3的子串!"elseecho "$a1不是$a3的子串!"fiif [[ "$a3" =~ "$a2" ]];thenecho "$a2是$a3的子串!"elseecho "$a2不是$a3的子串!"fi   注意: 使用-n在[]结构中测试必须要用""把变量引起来.使用一个未被""的字符串来使用! -z或者就是未用""引用的字符串本身,放到[]结构中。虽然一般情况下可以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯. awk '{print $2}' class.txt | grep '^[0-9.]' >

Res

The above is the comparison of Linux shell operators shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report