系列笔记传送门:linux
shell中的算术运算符web
shell中的运算命令
算术运算符经常使用于运算命令,shell中运算命令有如下几种:shell
示例1,(())命令实践
双小括号的做用是进行数值运算与数值的比较,经常使用。编程
[root@moli_linux1 shell_test] echo $((1+1)) #计算1+1后输出 2 [root@moli_linux1 shell_test] echo $((9-3)) 6 [root@moli_linux1 shell_test] ((i=5)) [root@moli_linux1 shell_test] echo $((i+=1)) #获取i值,计算i+1,把i+1从新赋给i,输出i 6 [root@moli_linux1 shell_test] echo $i 6 [root@moli_linux1 shell_test] ((a=1+2**3-4%3)) #<==表达式运算后将结果赋值给变量a,先乘除后加减 [root@moli_linux1 shell_test] echo $((1+2**3-4%3)) #<==这里直接将运算表达式进行运算并将结果输出,输出要加上$符号 8 [root@moli_linux1 shell_test] b=$((1+2**3-4%3)) #<==这里直接将运算表达式进行运算并将结果输出,输出结果赋值给变量b [root@moli_linux1 shell_test]# echo $b 8 [root@moli_linux1 shell_test] a=$((100*(100+1)/2)) #<==利用公式计算1+2+3....100 [root@moli_linux1 shell_test] echo $a 5050
示例2,用(())命令进行比较vim
[root@moli_linux1 shell_test] echo $((3>6)) #<==3>6是不成立的,所以输出0。表示比较时,1为真,0为假 0 [root@moli_linux1 shell_test] echo $((3>1)) #<==3>1是成立的,所以输出1. 1 [root@moli_linux1 shell_test] echo $((3==3)) #<==3=3是成立的,所以输出1 1
注意:上面提到的数字和变量必须是整型的,不能是浮点型或字符串。下面的bc和awk命令能够用于进行浮点数运算。segmentfault
[root@moli_linux1 ~] echo $((a=1.0+2.5)) -bash: a=1.0+2.5: 语法错误: 无效的算术运算符 (错误符号是 ".0+2.5") [root@moli_linux1 ~]#
示例3,有关++,--的运算bash
[root@moli_linux1 shell_test] a=10 [root@moli_linux1 shell_test] echo $((a++)) 变量a在运算符++以前,输出整个表达式时会输出a的值,再进行表达式的自增 10 [root@moli_linux1 shell_test] echo $a 此时输出a变量的值,是自增后的值,即为11 11 [root@moli_linux1 shell_test] echo $((a--)) 11 [root@moli_linux1 shell_test] echo $a 10 [root@moli_linux1 shell_test] echo $((++a)) 变量a在运算符++以后,输出整个表达式时会先输出整个表达式自增的值,即11 11 [root@moli_linux1 shell_test] echo $a 11 [root@moli_linux1 shell_test] echo $((--a)) 10 [root@moli_linux1 shell_test] echo $a 10
变量在++.--运算符以前,输出表达式的值为变量自身,再进行表达式的自增或自减;变量在++.--运算符以后,会先进行表达式的自增或自减,再输出表达式的值。运维
let运算命令
let运算命令的语法格式为:let 赋值表达式
let赋值表达式至关于”((赋值表达式))“测试
[root@moli_linux1 shell_test] i=5 [root@moli_linux1 shell_test] let i=i+1 [root@moli_linux1 shell_test] echo $i 6
expr运算命令
语法:expr 表达式spa
[root@moli_linux1 shell_test] expr 2 + 2 4 [root@moli_linux1 shell_test] expr 2 - 2 0 [root@moli_linux1 shell_test] expr 2 * 2 expr: 语法错误 [root@moli_linux1 shell_test] expr 2 \* 2 4 [root@moli_linux1 shell_test] expr 2 / 2 1
注意:使用expr时:
expr在Shell中可配合变量进行计算,但须要用反引号将计算表达式括起来
[root@moli_linux1 shell_test] i=5 [root@moli_linux1 shell_test] i=` expr $i + 6 ` 此处反引号括起来的表达式,变量和数字符号两边都要有空格 [root@moli_linux1 shell_test] echo $i 11
一、判断一个未知的变量是否是整数
原理: 利用expr作计算时变量或字符串必须是整数的规则,把一个变量和一个整数(非零)相加。看命令的返回值是否为0。若是为0,就认为与整数相加的变量是整数,若是不是0,就不是整数
[root@moli_linux1 shell_test] i=5 #此时的i是整数,数字5 [root@moli_linux1 shell_test] expr $i + 6 &>/dev/null #把i与一个整数相加,&>/dev/null 表示不保留任何输出 [root@moli_linux1 shell_test] echo $? 0 #返回0说明i是整数 [root@moli_linux1 shell_test] i=moli [root@moli_linux1 shell_test] expr $i + 6 &>/dev/null [root@moli_linux1 shell_test] echo $? 2 #返回1说明i不是是整数
二、判断传参是否是整数
[root@moli_linux1 shell_test] cat t3.sh #!/bin/bash expr $1 + 1 >/dev/null 2>&1 [ $? -eq 0 ] &&echo int||echo chars #这是一个条件表达式语法,返回值为0则输出int,不然输出chars [root@moli_linux1 shell_test] sh t3.sh 1 int [root@moli_linux1 shell_test] sh t3.sh a chars
三、编写shell脚本,查找一段语句中字符数小于6的单词
思路,须要知道字符串长度的计算方法,利用for循环。
字符串长度能够利用expr命令得出,如几种计算字符串的方法
[root@moli_linux1 ~] char="i am a student" [root@moli_linux1 ~] expr length "$char" 14 [root@moli_linux1 ~] echo ${#char} 14 [root@moli_linux1 ~] echo ${char} | wc -L 14 [root@moli_linux1 ~] echo ${char} | awk '{print length($0)}' 14
知道怎么计算字符串长度的方法,就能够利用计算出来的长度和整数作比较就能够得出结果了,例如判断下面这条语句。(有关for循环,if判断稍后再讲)i am oldboy linux welcome to our training
[root@moli_linux1 shell_test] cat wordLength.sh #!/bin/bash for n in i am oldboy welcome to our training do if [ `expr length $n` -le 6 ] then echo $n fi done [root@moli_linux1 shell_test] sh wordLength.sh i am oldboy to our
四、基于Shell变量输出read命令的运算实践
Shell变量除了能够直接赋值或脚本传参外,还可使用read命令从标准输入中得到。
语法格式:read [参数] [变量名]
经常使用参数:
实现read命令的基本读入功能
[root@moli_linux1 ~] read -t 10 -p "Please input a num:" num #做为接受read的变量,不应带$符号 #读入一个输入,赋值给num变量,注意,num变量前须要有空格 Please input a num:10 #输入10,把数字10赋值给num变量 [root@moli_linux1 ~] echo $num 10 [root@moli_linux1 ~] read -t 10 -p "Please input two num:" a1 a2 #读入两个输入,注意要以空格隔开,分别赋值给a1 a2变量,a1变量先后都要有空格 Please input two num:10 20 [root@moli_linux1 ~] echo $a1 $a2 10 20 [root@moli_linux1 ~] read a3 a4 30 40 [root@moli_linux1 ~] echo $a3 30 [root@moli_linux1 ~] echo $a4 40
read命令小实践
[root@moli_linux1 shell_test] cat t2_read_change.sh #!/bin/bash #no.1 read -t 10 -p "Pleasa input two int number:" a b #经过read命令读入两个值,赋给变量a和变量b [ ${#a} -le 0 ]&&{ #利用条件表达式,根据变量值的长度是否小于1,来肯定第一个数是否为空 echo "The first number is null." exit 1 } [ ${#b} -le 0 ]&&{ #利用条件表达式,根据变量值的长度是否小于1,来肯定第二个数是否为空 echo "The second number is null." exit 2 } #no.2 判断传参是不是整数 expr $a + 1 &>/dev/null RETVAL_A=$? expr $b + 1 &>/dev/null RETVAL_B=$? #若是A和B中有一个不是整数,那么就打印提示并退出。 if [ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ];then echo "one of the num is not num,please input again." exit 3 fi #no.3 echo "a+b=$(($a+$b))" echo "a-b=$(($a-$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))"
在bash中各类条件结构和流程控制结构中都要进行各类测试,根据测试结果执行不一样的操做
执行条件测试表达式后一般会返回真或假。
在bash编程里,条件测试经常使用的语法有四种,
条件测试语法 | 说明 | |
---|---|---|
test <表达式> | 利用test命令进行条件测试表达式的方法。test命令和表达式之间至少有一个空格。 | |
[ <表达式> ] | 这是最经常使用的条件测试表达式。经过[ ]来测试括号内的表达式的方法。[ ]左右边界至少有一个空格 | |
[[ <表达式> ]] | 和[]相似,比较不经常使用,可是它可使用通配符等进行模式匹配,一般[[ ]]左右边界至少有一个空格 | |
((<表达式>)) | 利用双小括号,两端不须要有空格,经常使用于if里的计算。 |
注意:&&,||,<,>等操做符能够应用在[[ ]]中,但不能应用在[ ]中,在[ ]中通常使用 -a,-o,-gt等。
文件测试表达式
test条件测试表达式实践
test命令的语法是:test <测试表达式>
对于下面的命令,
test -f file && echo true || echo false
该语句表示若是文件file存在,就输出true,不然输出false。-f参数是文件测试表达式之一,用于测试文件是否为普通文件。
test表达式也能够只用一半逻辑,&& 或者 ||,如:
test -f file && echo true #若是表达式成立就输出true test -f file || echo false #若是表达式不成立就输出flase
test中 -选项用来测试字符串长度,若是为0则表达式成立,不然不成立
[root@moli_linux1 ~] test -z "study" && echo 1 || echo 0 0 #由于study字符串长度不是0,所以输出0
[ ]条件测试表达式实践
[ ]条件测试表达式的语法: [ <测试表达式> ]
对于下面语句:
[ -f /tmp/test.txt ] && echo 1 || echo 0
若是/tmp/下有test.txt文件存在,则输出1.不然输出0.
文件测试表达式示例
普通文件测试文件类型
[root@moli_linux1 shelltest] touch oldboy #建立普通文件oldboy [root@moli_linux1 shelltest] ll oldboy -rw-r--r-- 1 root root 0 1月 23 18:32 oldboy [root@moli_linux1 shelltest] [ -f oldboy ] && echo 1 || echo 0 #文件存在,条件为真,输出1 1
目录文件(测试文件类型)
[root@moli_linux1 shelltest] mkdir oldgirl #建立目录文件oldgirl [root@moli_linux1 shelltest] [ -f oldgirl ] && echo 1 || echo 0 #输出为0,说明文件oldgirl不是普通文件,oldgirl是目录的确不是普通文件 0 [root@moli_linux1 shelltest] [ -e oldgirl ] && echo 1 || echo 0 1 #输出为1,证实文件oldgirl存在,无论oldfirl是目录仍是普通文件 [root@moli_linux1 shelltest] [ -d oldgirl ] && echo 1 || echo 0 1 #输出为1,证实oldgirl是目录 [root@moli_linux1 shelltest] [ -d oldboy ] && echo 1 || echo 0 0#输出为0,证实oldboy不是目录
字符串测试表达式
字符串测试操做符的做用:比较两个字符串是否相同,测试字符串的长度是否为零,字符串是否为null等。
示例:
[root@moli_linux1 shell_test] [ -n "abc" ] && echo 1 || echo 0 1 #字符串abc的长度不为0,表达式为真,输出1 [root@moli_linux1 shell_test] [ -z "abc" ] && echo 1 || echo 0 0 #字符串abc的长度不为0,表达式为假,输出0 [root@moli_linux1 shell_test] test -n "abc" && echo 1 1 #使用test等同于[] [root@moli_linux1 shell_test] char=abc [root@moli_linux1 shell_test] test -n "$char" && echo 1 1 #使用变量时要注意使用双引号 [root@moli_linux1 shell_test] [ -z "$char" ] && echo 1 || echo 0 0 [root@moli_linux1 shell_test] char1=abc [root@moli_linux1 shell_test] char2=abc [root@moli_linux1 shell_test] char3=moli [root@moli_linux1 shell_test] [ "$char1" = "$char2" ] && echo 1 || echo 0 1 #变量char1等于char2,表达式为真,输出1 [root@moli_linux1 shell_test] [ "$char1" = "$char3" ] && echo 1 || echo 0 0 #变量char1不等于char2,表达式为假,输出0 [root@moli_linux1 shell_test] [ "$char1" != "$char3" ] && echo 1 || echo 0 1 #变量char1不等于char3,表达式为真,输出1
整数二元比较操做符
示例:
[root@moli_linux1 ~]# a1=98 [root@moli_linux1 ~]# a2=99 [root@moli_linux1 ~]# [ $a1 -eq $a2 ] && echo 1 || echo 0 #判断a1和a2是否相等 0 [root@moli_linux1 ~]# [ $a1 -gt $a2 ] && echo 1 || echo 0#判断a1是否大于a2 0 [root@moli_linux1 ~]# [ $a1 -lt $a2 ] && echo 1 || echo 0#判断a1是否小于a2 1 [root@moli_linux1 ~]# [ $a1 -ne $a2 ] && echo 1 || echo 0#判断a1和a2是否不相等 1
逻辑操做符
示例:
[root@moli_linux1 shelltest]# [ -f oldboy.txt -o -f oldgirl.txt ] && echo 1 1 [root@moli_linux1 shelltest]# [ -f oldboy.txt -a -f oldgirl.txt ] && echo 1 1 [root@moli_linux1 shelltest]# [ -f oldboy.txt -a -f younggirl.txt ] || echo 1 1 [root@moli_linux1 shelltest]# [ -f oldboy.txt -o -f younggirl.txt ] && echo 1 || echo 0 1
-o规则:
当左边为真,右边为真,结果为真
当左边为真,右边为假,结果为真
当左边为假,右边为真,结果为真
当左边为假,右边为假,结果为假
-a规则:
当左边为真,右边为真,结果为真
当左边为真,右边为假,结果为假
当左边为假,右边为真,结果为假
当左边为假,右边为假,结果为假
总结
示例1
经过命令行传入字符或数字,判断传入的字符或数字,执行相应的操做。
[root@moli_linux1 script]# cat test6.sh #!/bin/bash read -p "pls input a char:" var #读取用户输入赋值给var变量 [ "$var" == "1" ] &&{ #条件判断,看变量值是否为1 echo 1 exit 0 } [ "$var" == "2" ] &&{ #条件判断,看变量值是否为2 echo 2 exit 0 } [ "$var" != "2" -a "$var" != "1" ] &&{ #若是不等于1或者2,就打印错误提示并退出 echo error exit 0 }
示例2
打印选择菜单,安装选择项一键安装不一样的web服务。
[root@moli_linux1 script]# mkdir -p /root/server/script/lamp.sh [root@moli_linux1 script]# mkdir -p /root/server/script/lnmp.sh [root@moli_linux1 script]# echo "echo the lamp is installed" > lamp.sh [root@moli_linux1 script]# echo "echo the lnmp is installed" > lnmp.sh [root@moli_linux1 script]# vim menu.sh #!/bin/bash path=/root/server/script [ ! -d "$path" ] && mkdir -p $path #!/bin/bash path=/root/server/script [ ! -d "$path" ] && mkdir -p $path #menu cat <<END 1.[install lamp] 2.[install lnmp] 3.[exit] pls input the num you want: END read num expr $num + 1 &>/dev/null [ $? -ne 0 ] && { echo "the num you input must be {1|2|3}" exit 1 } [ $num -eq 1 ] &&{ echo "starint installing lamp" sleep 2; [ -x "$path/lamp.sh" ]||{ echo "$path/lamp.sh does not exit or can nor be exec" exit 1 } $path/lamp.sh exit $? } [ $num -eq 2 ] &&{ echo "starint installing lnmp" sleep 2; [ -x "$path/lnmp.sh" ]||{ echo "$path/lnmp.sh does not exit or can nor be exec" exit 1 } $path/lnmp.sh exit $? } [ $num -eq 3 ] &&{ echo bye exit 3 } [[ ! $num =~ [1-3] ]] &&{ #<==[[]] echo "the num you input must be {1|2|3}" echo "Input Error" exit 4 }