成于坚持,败于止步
=
通用赋值操作符, 可用于算术和字符串赋值
算术操作符
+
加法计算
-
减法计算
*
乘法计算
/
除法计算
**
幂运算(c语言中没用过的)
%
模运算, 或者是求余运算(返回一次除法运算的余数)
这里重点说一下“=”
root@ubuntu:~/resource/shell-study/0506-2013# var=27 root@ubuntu:~/resource/shell-study/0506-2013# echo $var 27 root@ubuntu:~/resource/shell-study/0506-2013# var= 27 27: command not found root@ubuntu:~/resource/shell-study/0506-2013# string=hello root@ubuntu:~/resource/shell-study/0506-2013# echo $string hello root@ubuntu:~/resource/shell-study/0506-2013# string= hello The program 'hello' can be found in the following packages: * hello * hello-debhelper Try: apt-get install <selected package>上面可以看到,=的左右两边是不能出现空格的,否则则会出现上面的报错信息,另外还有一点就是赋值语句中的“=”和字符串比较语句中的“=”要注意区分, 书中作者也给出了一种更好更安全的使用方法
#!/bin/bash string1="hello" string2="hello" if [ "X$string1" = "X$string2" ];then echo "X+string1=X+string2" else echo "error" fi exit 0这种方法在两个比较的字符串中同时添加了一个共同项“X”,这样可以防止两个变量中的一个为空所产生的错误,字符"X"作为前缀在等式两边是可以相互抵消的
#!/bin/bash ARGS=2 BADARGS=65 if [ $# -ne "$ARGS" ];then echo "Usage: `basename $0` first-number second number" exit $BADARGS fi gcd () { dividend=$1 divisor=$2 reminder=1 until [ "$reminder" -eq 0 ] do let "reminder = $dividend % $divisor" dividend=$divisor divisor=$reminder done } gcd $1 $2 echo "Gcd of $1 and $2 = $dividend" exit 0+=
expr通用求值表达式: 通过给定的操作(参数必须以空格分开)连接参数, 并对参数求值. 可以使算术操作, 比较操作, 字符串操作或者是逻辑操作.
expr 3 + 5
返回8
expr 5 % 3
返回2
expr 1 / 0
返回错误消息, expr: division by zero,不允许非法的算术操作.
expr 5 \* 3
返回15,在算术表达式expr中使用乘法操作时, 乘法符号必须被转义.
y=`expr $y + 1`
增加变量的值, 与let y=y+1和y=$(($y+1))的效果相同.
在Bash中的整型变量事实上是一个有符号的long(32-bit)整型值, 所表示的范围是-2147483648到2147483647. 如果超过这个范围进行算术操作的话, 那么将不会得到你期望的结果,当然,在2.05b版本之后, Bash开始支持64位整型了
Bash不能够处理浮点运算. 它会把包含小数点的数字看作字符串,如果非要做浮点运算的话, 可以在脚本中使用bc, 这个命令可以进行浮点运算, 或者调用数学库函数
位操作符
<<
左移一位(每次左移都相当于乘以2)
<<=
"左移-赋值"
let "var <<= 2" 这句的结果就是变量var左移2位(就是乘以4)
>>
右移一位(每次右移都将除以2)
>>=
"右移-赋值" (与<<=正好相反)
&
按位与
&=
"按位与-赋值"
|
按位或
|=
"按位或-赋值"
~
按位反
!
按位非
^
按位异或XOR
^=
"按位异或-赋值"
逻辑操作符
&&
与(逻辑)
if [ $condition1 ] && [ $condition2 ] # 与 if [ $condition1 -a $condition2 ] 相同 # 如果condition1和condition2都为true, 那结果就为true. if [[ $condition1 && $condition2 ]] # 也可以. # 注意: &&不允许出现在[ ... ]结构中.||
if [ $condition1 ] || [ $condition2 ] # 与 if [ $condition1 -o $condition2 ] 相同 # 如果condition1或condition2中的一个为true, 那么结果就为true. if [[ $condition1 || $condition2 ]] # 也可以. # 注意||操作符是不能够出现在[ ... ]结构中的.
,
混杂操作符,逗号操作符可以连接两个或多个算术运算. 所有的操作都会被运行(可能会有负作用), 但是只会返回最后操作的结果
let "t1 = ((5 + 3, 7 - 1, 15 - 4))" echo "t1 = $t1" # t1 = 11 let "t2 = ((a = 9, 15 / 3))" # 设置"a"并且计算"t2". echo "t2 = $t2 a = $a" # t2 = 5 a = 9测试实例:
#!/bin/bash a=24 b=47 if [ "$a" -eq 24 ] && [ "$b" -eq 47 ];then echo "Test #1 succeeds." else echo "Test #1 fails." fi #error method #if [ "$a" -eq 24 && "$b" -eq 47 ];then # echo "Test #1 succeeds." #else # echo "Test #1 fails." #fi #right method if [ "$a" -eq 24 -a "$b" -eq 47 ];then echo "Test #2 succeeds." else echo "Test #2 fails." fi if [ "$a" -eq 98 ] || [ "$b" -eq 47 ];then echo "Test #3 succeeds." else echo "Test #3 fails." fi if [ "$a" -eq 98 -o "$b" -eq 47 ];then echo "Test #4 succeeds." else echo "Test #4 fails." fi a=rhino b=crocodile if [ "$a" = rhino ] && [ "$b" = crocodile ];then echo "Test #5 succeeds." else echo "Test #5 fails." fi exit 0
先到这里了,O(∩_∩)O~
我的专栏地址:http://blog.csdn.net/column/details/shell-daily-study.html
待续。。。。