Shell变量数值计算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,输出i6[root@moli_linux1 shell_test] echo $i6[root@moli_linux1 shell_test] ((a=1+23-4%3))#<==表达式运算后将结果赋值给变量a,先乘除后加减[root@moli_linux1 shell_test] echo $((1+23-4%3))#<==这里直接将运算表达式进行运算并将结果输出,输出要加上$符号8[root@moli_linux1 shell_test] b=$((1+2**3-4%3))#<==这里直接将运算表达式进行运算并将结果输出,输出结果赋值给变量b[root@moli_linux1 shell_test]# echo $b8[root@moli_linux1 shell_test] a=$((100*(100+1)/2))#<==利用公式计算1+2+3….100[root@moli_linux1 shell_test] echo $a5050示例2,用(())命令进行比较[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是成立的,因此输出11注意:上面提到的数字和变量必须是整型的,不能是浮点型或字符串。下面的bc和awk命令可以用于进行浮点数运算。[root@moli_linux1 ~] echo $((a=1.0+2.5))-bash: a=1.0+2.5: 语法错误: 无效的算术运算符 (错误符号是 “.0+2.5”)[root@moli_linux1 ~]# 示例3,有关++,–的运算[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变量的值,是自增后的值,即为1111[root@moli_linux1 shell_test] echo $((a–))11[root@moli_linux1 shell_test] echo $a10[root@moli_linux1 shell_test] echo $((++a))变量a在运算符++之后,输出整个表达式时会先输出整个表达式自增的值,即1111[root@moli_linux1 shell_test] echo $a11[root@moli_linux1 shell_test] echo $((–a))10[root@moli_linux1 shell_test] echo $a10变量在++.–运算符之前,输出表达式的值为变量自身,再进行表达式的自增或自减;变量在++.–运算符之后,会先进行表达式的自增或自减,再输出表达式的值。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 $i6expr运算命令语法:expr 表达式[root@moli_linux1 shell_test] expr 2 + 24[root@moli_linux1 shell_test] expr 2 - 20[root@moli_linux1 shell_test] expr 2 * 2expr: 语法错误[root@moli_linux1 shell_test] expr 2 * 24[root@moli_linux1 shell_test] expr 2 / 21注意:使用expr时:运算符及用于计算的数字左右至少有一个空格,否则会报错使用乘号时,必须使用反斜线屏蔽其特定含义,因为shell可能会误解星号的含义expr在Shell中可配合变量进行计算,但需要用反引号将计算表达式括起来[root@moli_linux1 shell_test] i=5[root@moli_linux1 shell_test] i=expr $i + 6此处反引号括起来的表达式,变量和数字符号两边都要有空格[root@moli_linux1 shell_test] echo $i11Shell脚本中常见的需求判断一个未知的变量是不是整数原理: 利用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/bashexpr $1 + 1 >/dev/null 2>&1[ $? -eq 0 ] &&echo int||echo chars#这是一个条件表达式语法,返回值为0则输出int,否则输出chars[root@moli_linux1 shell_test] sh t3.sh 1int[root@moli_linux1 shell_test] sh t3.sh achars编写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 -L14[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/bashfor n in i am oldboy welcome to our trainingdo if [ expr length $n -le 6 ] then echo $n fidone[root@moli_linux1 shell_test] sh wordLength.sh iamoldboytoour基于Shell变量输出read命令的运算实践Shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入中获得。语法格式:read [参数] [变量名]常用参数:-p prompt : 设置提示信息-t timeout:设置输入等待的时间,单位默认为秒实现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 $num10[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 $a210 20[root@moli_linux1 ~] read a3 a430 40[root@moli_linux1 ~] echo $a330[root@moli_linux1 ~] echo $a440read命令小实践[root@moli_linux1 shell_test] cat t2_read_change.sh#!/bin/bash#no.1read -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/nullRETVAL_A=$? expr $b + 1 &>/dev/nullRETVAL_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 3fi#no.3echo “a+b=$(($a+$b))“echo “a-b=$(($a-$b))“echo “ab=$(($a$b))“echo “a/b=$(($a/$b))”