The shell supports multiple operators
1. Arithmetic operator 2. Relational operator 3. Boolean operator 4. String operator 5. File test operator
Native bash does not support simple mathematical operations, but can be implemented by other commands, such as awk and expr, expr is the most commonly used.
expr is an expression calculation tool, which can be used to complete the expression evaluation operation.
For example, add two numbers (note that backticks are used instead of single quotes):
! # / bin / bash Val = `expr 2 + 2 ` echo " two numbers is: $ Val "
Execute the script, the output is as follows:
The sum of the two numbers is: 4
Two points to note: 1. There must be a space between the expression and the operator, for example, 2 + 2 is wrong, it must be written as 2 + 2 , which is different from most programming languages we are familiar with. 2. The complete expression must be included in ``, note that this character is not a common single quote, under the Esc key.
Arithmetic operator
The following table lists commonly used arithmetic operators, assuming variable a is 10 and variable b is 20:
Operator | Explanation | Examples |
+ | addition | `expr $ a + $ b` results in 30 |
- | Subtraction | `expr $ a-$ b` results in -10 |
* | multiplication | `expr $ a \ * $ b` results in 200 |
/ | division | `expr $ b / $ a` results in 2 |
% | Balance | `expr $ b% $ a` results in 0 |
= | Assignment | a = $ b will assign the value of the variable b to a |
== | equal | Used to compare two numbers, and returns true if they are the same. [$ a == $ b] returns false |
!= | not equal | not equal. Used to compare two numbers, returns true if they are not the same. [$ a! = $ b] returns true |
Note: The conditional expression should be placed between square brackets and must have spaces, for example: [$ a == $ b] is wrong and must be written as [$ a == $ b]
Examples:
#!/bin/bash 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 equals b " fi if [$ a! = $ b] then echo " a does not equal b " fi
Execute the script, the output is as follows:
a + b: 30 a -b: -10 a * b: 200 b / a: 2 b % a: 0 a does not equal b
Note :
• The backslash (\) must be added before the multiplication sign (*) to realize the multiplication operation;
• if ... then ... fi is a conditional statement, which will be explained later.
• The shell's expr syntax in MAC is: $ ((expression)), where the "*" in the expression does not require the escape symbol "\".
Relational operator
Relational operators only support numbers, not strings, unless the string value is a number.
The following table lists commonly used relational operators, assuming variable a is 10 and variable b is 20:
Operator | Explanation | Examples |
-eq | Checks whether two numbers are equal and returns true if they are equal. | [$ a -eq $ b] returns false. |
-born | Check if two numbers are not equal, return true if they are not equal | [$ a -ne $ b] returns true. |
-gt | Check if the number on the left is greater than the one on the right, and if so, return true. | [$ a -gt $ b] returns false. |
-lt | Check if the number on the left is less than the one on the right, and if so, return true. | [$ a -lt $ b] returns true. |
-give | Check whether the number on the left is greater than or equal to the right, and if so, return true. | [$ a -ge $ b] returns false. |
-the | Check whether the number on the left is less than or equal to the right, and if so, return true. | [$ a -le $ b] returns true. |
Boolean operators The
following table lists commonly used Boolean operators, assuming variable a is 10 and variable b is 20:
Operator | Explanation | Examples |
! | No operation, false if expression is true, otherwise true. | [! false] returns true. |
-O | OR operation, if an expression is true, it returns true. | [$ a -lt 20 -o $ b -gt 100] returns true. |
-a | And operation, both expressions are true to return true. | [$ a -lt 20 -a $ b -gt 100] returns false. |
Logical Operators
Introduce Shell's logical operators, assuming variable a is 10 and variable b is 20:
Operator | Explanation | example |
&& | Logical AND | [[ $a -lt 100 && $b -gt 100 ]] 返回 false |
|| | Logical OR | [[$ a -lt 100 || $ b -gt 100]] returns true |
String operator
The following table lists commonly used string operators, assuming that the variable a is "abc" and the variable b is "efg":
Operator | Explanation | example |
= | Checks whether two strings are equal and returns true if they are equal. | [$ a = $ b] returns false. |
!= | Check whether two strings are equal, and return true if they are not equal. | [$ a! = $ b] returns true. |
-from | Check whether the length of the string is 0, and return true if it is 0. | [-z $ a] returns false. |
-n | Check whether the length of the string is not 0, and return true if it is not 0. | [ -n "$a" ] 返回 true。 |
$ | 检测字符串是否为空,不为空返回 true。 | [ $a ] 返回 true。 |
文件测试运算符
文件测试运算符用于检测 Unix 文件的各种属性。
属性检测描述如下:
运算符 | 说明 | 例子 |
-b file | 检测文件是否是块设备文件,如果是,则返回 true。 | [ -b $file ] 返回 false。 |
-c file | 检测文件是否是字符设备文件,如果是,则返回 true。 | [ -c $file ] 返回 false。 |
-d file | 检测文件是否是目录,如果是,则返回 true。 | [ -d $file ] 返回 false。 |
-f file | 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 | [ -f $file ] 返回 true。 |
-g file | 检测文件是否设置了 SGID 位,如果是,则返回 true。 | [ -g $file ] 返回 false。 |
-k file | 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 | [ -k $file ] 返回 false。 |
-p file | 检测文件是否是有名管道,如果是,则返回 true。 | [ -p $file ] 返回 false。 |
-u file | 检测文件是否设置了 SUID 位,如果是,则返回 true. | [ -u $file ] 返回 false。 |
-r file | 检测文件是否可读,如果是,则返回 true。 | [ -r $file ] 返回 true。 |
-w file | 检测文件是否可写,如果是,则返回 true。 | [ -w $file ] 返回 true。 |
-x file | 检测文件是否可执行,如果是,则返回 true。 | [ -x $file ] 返回 true。 |
-s file | 检测文件是否为空(文件大小是否大于0),不为空返回 true。 | [ -s $file ] 返回 true。 |
-e file | 检测文件(包括目录)是否存在,如果是,则返回 true。 | [ -e $file ] 返回 true。 |
其他检查符:
-S: 判断某文件是否 socket。
-L: 检测文件是否存在并且是一个符号链接。
实例
变量 file 表示文件 /var/www/runoob/test.sh,它的大小为 100 字节,具有 rwx 权限。下面的代码,将检测该文件的各种属性:
#!/bin/bash file="/var/www/runoob/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
脚本执行结果:
文件可读
文件可写
文件可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在