Shell basic operator
Shell, like other programming languages, supports a variety of operators, including:
- The arithmetic operator
- The relationship operator
- Boolean operator
- String operator
- File test operator
expr is an expression evaluation tool that uses it to complete the evaluation of expressions.
For example, two numbers add up (note that inverse quotes are used 'instead of single quotes'):
#!/bin/bash val=`expr 2 + 2` echo "两数之和为 : $val"
Execute the script and the output looks like this:
两数之和为 : 4
Two points to note:
- It is not right to have spaces between expressions and operators, such as 2 plus 2, and must be written as 2 plus 2, unlike most programming languages we are familiar with.
- The complete expression is to be '' contained, note that this character is not commonly used in single quotes, under the Esc key.
Arithmetic operator
The following table lists commonly used arithmetic operators, assuming that variable a is 10 and variable b is 20:
Operator | Description | Example |
---|---|---|
+ | Addition | The 'expr $a plus $b' result is 30. |
- | Subtraction | The 'expr $a - $b' result is -10. |
* | Multiplication | The result $a 'expr$b' is 200. |
/ | Division | The 'expr $b / $a' result is 2. |
% | Take the rest | 'expr $b % $a' results in 0. |
= | Assignment | The value $b a will assign the value of variable b to a. |
== | Equal. Used to compare two numbers, the same returns true. | The $a returns $b false. |
!= | Not equal. True is returned for comparing two numbers, which are not the same. | The $a returns true $b! . |
Note: Conditional expressions have to be placed between square brackets and have spaces, such as: " $a $b is wrong and must be written as $a $b" .
Instance
An example of an arithmetic operator is as follows:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn 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 * b : $val" val=`expr $b / $a` echo "b / a : $val" val=`expr $b % $a` echo "b % a : $val" if [ $a == $b ] then echo "a 等于 b" fi if [ $a != $b ] then echo "a 不等于 b" fi
Execute the script and the output looks like this:
a + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a 不等于 b
Note: A backslash must be added to the front of the multiplier( ) to multiply; t hen... F i is a conditional statement, which will be explained later. The expr syntax of the shell in MAC is$ (expression), where the expression does not require the escape symbol """""
The relationship operator
Relationship operators only support numbers, not strings, unless the value of the string is a number.
The following table lists commonly used relationship operators, assuming variable a is 10 and variable b is 20:
Operator | Description | Example |
---|---|---|
-eq | Detect whether the two numbers are equal and return true equally. | The $a -eq $b return false. |
-ne | Detect whether the two numbers are equal and return true without equality. | The $a -ne $b returns true. |
-gt | Detects if the number on the left is greater than the number on the right, and if so, returns true. | The $a -gt $b return false. |
-lt | Detects if the number on the left is less than the number on the right and, if so, returns true. | The $a -lt $b returns true. |
-ge | Detects whether the number on the left is greater than or equal to the one on the right, and if so, returns true. | The $a -ge $b return false. |
-le | Detects whether the number on the left is less than or equal to the right, and if so, returns true. | ($a -le $b) returns true. |
Instance
An example of a relationship operator is as follows:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn a=10 b=20 if [ $a -eq $b ] then echo "$a -eq $b : a 等于 b" else echo "$a -eq $b: a 不等于 b" fi if [ $a -ne $b ] then echo "$a -ne $b: a 不等于 b" else echo "$a -ne $b : a 等于 b" fi if [ $a -gt $b ] then echo "$a -gt $b: a 大于 b" else echo "$a -gt $b: a 不大于 b" fi if [ $a -lt $b ] then echo "$a -lt $b: a 小于 b" else echo "$a -lt $b: a 不小于 b" fi if [ $a -ge $b ] then echo "$a -ge $b: a 大于或等于 b" else echo "$a -ge $b: a 小于 b" fi if [ $a -le $b ] then echo "$a -le $b: a 小于或等于 b" else echo "$a -le $b: a 大于 b" fi
Execute the script and the output looks like this:
10 -eq 20: a 不等于 b 10 -ne 20: a 不等于 b 10 -gt 20: a 不大于 b 10 -lt 20: a 小于 b 10 -ge 20: a 小于 b 10 -le 20: a 小于或等于 b
Boolean operator
The following table lists common Boolean operators, assuming variable a is 10 and variable b is 20:
Operator | Description | Example |
---|---|---|
! | Non-operation, the expression true returns false, otherwise true is returned. | the return of true. |
-o | Or, if there is an expression that is true, true is returned. | ($a -lt 20 -o $b -gt 100) returns true. |
-a | With the operation, both expressions are true before true is returned. | the $a -lt 20 -a $b -gt 100) return false. |
Instance
An example of the Boolean operator is as follows:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn a=10 b=20 if [ $a != $b ] then echo "$a != $b : a 不等于 b" else echo "$a != $b: a 等于 b" fi if [ $a -lt 100 -a $b -gt 15 ] then echo "$a -lt 100 -a $b -gt 15 : 返回 true" else echo "$a -lt 100 -a $b -gt 15 : 返回 false" fi if [ $a -lt 100 -o $b -gt 100 ] then echo "$a -lt 100 -o $b -gt 100 : 返回 true" else echo "$a -lt 100 -o $b -gt 100 : 返回 false" fi if [ $a -lt 5 -o $b -gt 100 ] then echo "$a -lt 5 -o $b -gt 100 : 返回 true" else echo "$a -lt 5 -o $b -gt 100 : 返回 false" fi
Execute the script and the output looks like this:
10 != 20 : a 不等于 b 10 -lt 100 -a 20 -gt 15 : 返回 true 10 -lt 100 -o 20 -gt 100 : 返回 true 10 -lt 5 -o 20 -gt 100 : 返回 false
The logical operator
The following describes the logical operator of the shell, assuming that the variable a is 10 and the variable b is 20:
Operator | Description | Example |
---|---|---|
&& | Logical AND | The $a return false. $b |
|| | Logical OR | the return $a true || $b -lt 100 -gt 100 |
Instance
An example of a logical operator is as follows:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn a=10 b=20 if [[ $a -lt 100 && $b -gt 100 ]] then echo "返回 true" else echo "返回 false" fi if [[ $a -lt 100 || $b -gt 100 ]] then echo "返回 true" else echo "返回 false" fi
Execute the script and the output looks like this:
返回 false 返回 true
String operator
The following table lists common string operators, assuming that the variable a is "abc" and the variable b is "efg":
Operator | Description | Example |
---|---|---|
= | Detects whether two strings are equal, and equal returns true. | The $a return $b false. |
!= | Detects whether two strings are equal and does not return true. | The $a returns true $b! . |
-z | Detects whether the string length is 0 and returns true for 0. | the false $a -z-z. |
-n | Detects whether the string length is 0, not 0 returns true. | the return $a true. |
Str | Detects whether the string is empty and does not return true. | The $a returns true. |
Instance
An example of a string operator is as follows:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn a="abc" b="efg" if [ $a = $b ] then echo "$a = $b : a 等于 b" else echo "$a = $b: a 不等于 b" fi if [ $a != $b ] then echo "$a != $b : a 不等于 b" else echo "$a != $b: a 等于 b" fi if [ -z $a ] then echo "-z $a : 字符串长度为 0" else echo "-z $a : 字符串长度不为 0" fi if [ -n $a ] then echo "-n $a : 字符串长度不为 0" else echo "-n $a : 字符串长度为 0" fi if [ $a ] then echo "$a : 字符串不为空" else echo "$a : 字符串为空" fi
Execute the script and the output looks like this:
abc = efg: a 不等于 b abc != efg : a 不等于 b -z abc : 字符串长度不为 0 -n abc : 字符串长度不为 0 abc : 字符串不为空
File test operator
The file test operator is used to detect the various properties of the Unix file.
Property detection is described below:
Operator | Description | Example |
---|---|---|
-b file | Detect if the file is a block device file and, if so, return true. | The "-b$file" return false. |
-c file | Detects whether the file is a character device file and, if so, returns true. | the false $file -c. |
-d file | Detects whether the file is a directory and, if so, returns true. | The .d $file return false. |
-f file | Detects whether the file is a normal file (neither a directory nor a device file) and, if so, returns true. | the return $file true. |
-g file | Detects if the file has an SGID bit set and, if so, returns true. | The "-g$file" returns false. |
-k file | Detects if the file has a sticky bit (Sticky Bit) set, and if so, returns true. | The "-k$file" returns false. |
-p file | Detect if the file is a named pipe and, if so, return true. | The "-p$file" returns false. |
-u file | Detects whether the file has a SUID bit set and, if so, returns true. | The "-u$file" return false. |
-r file | Detects whether the file is readable and, if so, returns true. | the return $file true. |
-w file | Detects whether the file is writeable and, if so, returns true. | The "-w$file" returns true. |
-x file | Detect whether the file is executable and, if so, return true. | the return $file true. |
-s file | Detects whether the file is empty (whether the file size is greater than 0) and does not return true. | The .s $file returns true. |
-e file | Detects the existence of a file, including a directory, and if so, returns true. | the return $file true. |
Instance
The variable file represents the file "/var/www/w3cschool/test.sh", which is 100 bytes in size and has rwx permissions. The following code will detect the various properties of the file:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn file="/var/www/w3cschool/test.sh" if [ -r $file ] then echo "文件可读" else echo "文件不可读" fi if [ -w $file ] then echo "文件可写" else echo "文件不可写" fi if [ -x $file ] then echo "文件可执行" else echo "文件不可执行" fi if [ -f $file ] then echo "文件为普通文件" else echo "文件为特殊文件" fi if [ -d $file ] then echo "文件是个目录" else echo "文件不是个目录" fi if [ -s $file ] then echo "文件不为空" else echo "文件为空" fi if [ -e $file ] then echo "文件存在" else echo "文件不存在" fi
Execute the script and the output looks like this:
文件可读 文件可写 文件可执行 文件为普通文件 文件不是个目录 文件不为空 文件存在