operator!/bin/bash #基本运算符 val= ' expr 2 + 2 ' echo ' two number of sum for ${val} ' #算数运算符 #* required backslash a=10 b=20 val= ' expr ${a} + ${b} ' echo ' A + b = ${val} "val= ' expr $a-$b ' echo ' A-B: $val ' val= ' expr $a \* $b ' echo ' A *: $val ' val= ' expr $b/$a ' echo ' b/a: $val "val=" expr $b% $a ' echo ' B% A: $val "#关系运算符 a=10 b=20 #运算符 Description Example #-eq detects whether two numbers are equal, returns true for equality.
[$a-eq $b] returns false. #-ne detects whether two numbers are equal and returns true if they are not equal.
[$a-ne $b] returns TRUE. #-gt detects if the number on the left is greater than the right and, if so, returns True.
[$a-gt $b] returns false. #-lt detects if the number on the left is less than the right and, if so, returns True.
[$a-lt $b] returns TRUE. #-ge detects if the number on the left is greater than or equal to the right, and returns true if it is.
[$a-ge $b] returns false. #-le detects if the left-hand number is less than or equal to the right, and returns true if it is. [$a-le $b] returns TRUE. # if [$a-eq $b] then echo "A and B are equal" else echo "A and B are unequal" fi #布尔运算符 #运算符 Illustration example #! Non-operation, the expression is true returns False, otherwise true.
[! false] returns TRUE. A #-o or operation that returns true if an expression is true.
[$a-lt 20-o $b-GT 100] returns TRUE. #-a and operation, two expressions are true to return true.
[$a-lt 20-a $b-GT 100] returns FALSE.
if [$a! = $b] Then echo "A is not equal to B" else echo "a equals B" fi if [$a > 0-a $b > 0] then echo "A and B are greater than 0" else echo "A and B both Greater than 0 does not establish "fi #逻辑运算符 #运算符 illustration example #&& logic and [[$a-lt && $b-GT 100]] returns false #| | Logical OR [[$a-lt | | $b-GT 100]] returns True if [[$a > 0 && $b > 0]] then echo "A>0 and B>0" Els E echo "a>0 and b>0 not established" fi #字符串运算符 #运算符 Description Example # = Detects whether two strings are equal and returns true for equality.
[$a = $b] returns FALSE. #! = detects whether two strings are equal and returns true for unequal.
[$a! = $b] Returns TRUE. #-Z detects if the string length is 0 and returns true for 0.
[-Z $a] returns false. #-N detects whether the string length is 0 and does not return true for 0.
[-N $a] returns true. # str detects if the string is empty and does not return true for null.
[$a] returns TRUE. A= "abc" b= "def" if [$a = $b] then echo "A and B equal" else echo "A and b not equal" fi #文件测试运算符 #操作符 Description Example #-B file Detection text Whether the piece is a block device file, or True if it is.
[-B $file] returns FALSE. #-C file detects whether the files are character device files and, if so, returns True.
[-C $file] returns false. #-D file detects whether the files are directories and, if so, returns True.
[-D $file] returns false. #-F file detection is a normal file (neither a directory nor a device file), asReturns true if the result is.
[-F $file] returns TRUE. The #-G file detects if the SGID bit is set, and returns True if it is.
[-G $file] returns false. #-K file detects if the files have a sticky bit set (Sticky bit), and returns True if yes.
[-K $file] returns false. The #-P file detects if the files are a well-known pipe, and if so, returns True.
[-P $file] returns false. #-U file detects if the SUID bit is set, and returns True if it is.
[-U $file] returns false. #-R file detects whether the files are readable and, if so, returns True.
[-R $file] returns TRUE. #-W file detects whether the files are writable and, if so, returns True.
[-W $file] returns TRUE. #-X file detects whether the files can be executed and, if so, returns True.
[-X $file] returns TRUE. #-S file detects whether the files are empty (the file size is greater than 0) and does not return true for null.
[-S $file] returns TRUE. #-E file detects the existence of files (including directories) and returns True if yes.
[-e $file] returns TRUE. #变量 file represents file= "/user/a7890/documents/shelltest/test1.sh" if [-R $file] then echo "test1.sh file readable" Else echo "test 1.sh file not readable "fi