Wu Yuxiong - born naturally ShellX study notes: Shell basic operators

Like Shell and other programming language that supports multiple operators, including: 
arithmetic operators 
Relational operators 
Boolean operators 
String operators 
file test operators 
native bash does not support simple mathematical operations, but can be achieved through other commands, such as awk and expr, expr most commonly used. 
expr is an expression calculation tool, use it to complete the evaluation of an expression operation. 
For example, to add two numbers (note that the use of anti-quotation marks instead of single quotation marks ` ' ): 
# / bin / the bash! 

Val = 2 + 2' expr ' 
echo " two numbers is: Val $ " 
execute script, the output is as follows: 
two numbers is: 4 
points Note: 
between the expressions and operators include a space, for example, 2 + 2 is wrong, must be written in 2 + 2 , with which we are familiar with most programming language is not the same. 
The complete expression `` to be included, note that this character is not commonly used in single quotation marks, below the Esc key.
Arithmetic Operators 
The following table lists common arithmetic operators, assuming a variable is 10, the variable b is 20:

 

 

Examples of 
arithmetic operators examples are as follows: 
# / bin / the bash! 
A = 10 
B = 20 is 

Val = A + `$ expr $ 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]  
the then
   echo" A is equal to B " 
Fi 
IF [A = $! $ B] 
the then 
   echo " A not equal to B " 
Fi 

execute the script, the output results are as follows: 
A + B: 30 
A - B: -10 
A * B: 200 is 
B / A: 2 
b % A: 0 
A = b 

Note: 
multiplication ( * ) must be added in front of a backslash (\) to achieve multiplication;
 IF ... ... Fi is the then conditional statements 
in the MAC shell the expr syntax is: $ ((expression)), where the expression " * " need to escape symbol " \."
Relational operators 
Relational operators only supports digits, does not support the string until the string is a digital value. 
The following table lists common relational operators, assuming a variable is 10, the variable b is 20:

 

 

Examples of 
relational operators examples are as follows: 
# / bin / the bash! 
A = 10 
B = 20 is IF [A $ - EQ $ B] 
the then 
   echo " $ A $ -eq B: A equal to B " the else 
   echo " $ A $ -eq b: a is not equal to B " 
Fi IF [A $ - NE $ B] 
the then 
   echo " $ A -ne $ b: a is not equal to B " the else 
   echo " $ A -ne $ b: a is equal to B " 
Fi IF [$ A - gt $ B] 
the then 
   echo " $ A $ B -gt: A greater than B " the else 
   echo






" $ A $ B -gt: A not greater than B " 
Fi 
IF [A $ - lt $ B] 
the then 
   echo " $ A $ -LT-B: A less than B " 
the else 
   echo " $ A $ -LT-B: not less than A B " 
Fi 
IF [A $ - GE $ B] 
the then 
   echo " $ A $ -ge B: A is greater than or equal to B " 
the else 
   echo " $ B $ -ge A: A less than B " 
Fi 
IF [$ A - Le $ B] 
the then 
   echo " $ B $ -le A: A less than or equal to B " 
the else 
   echo " $ A $ B -le:a is greater than b" 
Fi 

execute the script, the output results are as follows:
 10 -eq 20 is : A not equal to B
 10 -ne 20 is : A not equal to B
 10 -gt 20 is : not more than A B
 10 -LT-20 is : A less than B
 10 20 is -ge : a is less than b
 10 -le 20 is: a is less than or equal to b
Boolean Operators 
The following table lists common Boolean operators, assuming that the variable a is 10, b is variable 20:

 

 

实例
布尔运算符实例如下:
#!/bin/bash
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 小于 100 且 $b 大于 15 : 返回 true"
else
   echo "$a 小于 100 且 $b 大于 15 : 返回 false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a 小于 100 或 $b 大于 100 : 返回 true"
else
   echo "$a 小于 100 或 $b 大于 100 : 返回 false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a 小于 5 或 $b 大于 100 : 返回 true"
else
   echo "$a 小于 5 或 $b 大于 100 : 返回 false"
fi

执行脚本,输出结果如下所示:
10 != 20 : a 不等于 b
10 小于 100 且 20 大于 15 : 返回 true
10 小于 100 或 20 大于 100 : 返回 true
10 小于 5 或 20 大于 100 : 返回 false
逻辑运算符
以下介绍 Shell 的逻辑运算符,假定变量 a 为 10,变量 b 为 20:

 

 

实例
逻辑运算符实例如下:
#!/bin/bash
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

执行脚本,输出结果如下所示:
返回 false
返回 true
字符串运算符
下表列出了常用的字符串运算符,假定变量 a 为 "abc",变量 b 为 "efg"

 

 

实例
字符串运算符实例如下:
#!/bin/bash
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

执行脚本,输出结果如下所示:
abc = efg: a 不等于 b
abc != efg : a 不等于 b
-z abc : 字符串长度不为 0
-n abc : 字符串长度不为 0
abc : 字符串不为空
文件测试运算符
文件测试运算符用于检测 Unix 文件的各种属性。
属性检测描述如下:

其他检查符:
-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

执行脚本,输出结果如下所示:
文件可读
文件可写
文件可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在

 

Guess you like

Origin www.cnblogs.com/tszr/p/12111425.html