系列笔记传送门:Linux运维:Shell(一)Linux运维:Shell(二)Linux运维:Shell(三)Shell变量数值计算shell中的算术运算Arithmetic运算符通常用于shell中的算术命令。shell中有以下几类算术命令:例1、(())命令练习。[root@moli_linux1shell_test]echo$((1+1))#计算1+1并输出2[root@moli_linux1shell_test]echo$((9-3))6[root@moli_linux1shell_test]((i=5))[root@moli_linux1shell_test]echo$((i+=1))#获取i值,计算i+1,将i+1重新赋给i,输出i6[root@moli_linux1shell_test]echo$i6[root@moli_linux1shell_test]((a=1+2**3-4%3))#<==表达式运算后,将结果赋值给变量a,先乘除后加减[root@moli_linux1shell_test]echo$((1+2**3-4%3))#<==这里直接计算运算表达式并输出结果,输出必须加上$符号8[root@moli_linux1shell_test]b=$((1+2**3-4%3))#<==这里直接计算运算表达式并输出结果,并将输出结果赋值给变量b[root@moli_linux1shell_test]#echo$b8[root@moli_linux1shell_test]a=$((100*(100+1)/2))#<==用公式计算1+2+3....100[root@moli_linux1shell_test]echo$a5050例2,与(())命令比较[root@moli_linux1shell_test]echo$((3>6))#<==3>6不成立,所以输出0。比较时,1为真,0为假0[root@moli_linux1shell_test]echo$((3>1))#<==3>1成立,所以输出1.1[root@moli_linux1shell_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_linux1shell_test]a=10[root@moli_linux1shell_test]echo$((a++))变量a在运算符++之前,输出整个表达式时输出值a的,然后执行表达式的自增10[root@moli_linux1shell_test]echo$a此时输出一个变量的值,也就是自增后的值,即1111[root@moli_linux1shell_test]echo$((a--))11[root@moli_linux1shell_test]echo$a10[root@moli_linux1shell_test]echo$((++a))变量a在operator++之后,当输出整个表达式,会先输出整个表达式的自增值,即1111[root@moli_linux1shell_test]echo$a11[root@moli_linux1shell_test]echo$((--a))10[root@moli_linux1shell_test]echo$a10变量在++.--operator之前,输出表达式的值就是变量本身,然后表达式自增或自减;变量在++.--运算符之后,表达式先自增或自减,表达式的值就是输出值。let操作命令let操作命令的语法格式为:let赋值表达式let赋值表达式等价于“((赋值表达式))”[root@moli_linux1shell_test]i=5[root@moli_linux1shell_test]leti=i+1[root@moli_linux1shell_test]echo$i6expr操作命令语法:expr表达式[root@moli_linux1shell_test]expr2+24[root@moli_linux1shell_test]expr2-20[root@moli_linux1shell_test]expr2*2expr:语法错误[root@moli_linux1shell_test]expr2\*24[root@moli_linux1shell_test]expr2/21注意:使用expr时:运算符和计算用的数字左右必须至少有一个空格,否则会报错被举报。使用乘号时,必须使用反斜杠来屏蔽它的具体含义,因为shell可能会误解星号的意思expr在shell中可以用变量计算,但是计算表达式需要用反引号括起来[root@moli_linux1shell_test]i=5[root@moli_linux1shell_test]i=`expr$i+6`这里反引号括起来的表达式变量和数字两边必须有空格[root@moli_linux1shell_test]echo$i11Shell脚本中的常见要求1.判断a未知变量是否为整数原则:用expr计算时,变量或字符串必须为整数的规则,将变量与整数(非零)相加。查看命令的返回值是否为0。如果为0,则认为加在整数上的变量是整数。如果它不为0,则它不是整数。[root@moli_linux1shell_test]i=5#此时i为整数,数为5[root@moli_linux1shell_test]expr$i+6&>/dev/null#将i加到整数上,&>/dev/null表示不保留任何输出[root@moli_linux1shell_test]echo$?0#返回0表示i是整数[root@moli_linux1shell_test]i=moli[root@moli_linux1shell_test]expr$i+6&>/dev/null[root@moli_linux1shell_test]echo$?2#返回1表示i是否为整数2.判断传入的参数是否为整数[root@moli_linux1shell_test]catt3.sh#!/bin/bashexpr$1+1>/dev/null2>&1[$?-eq0]&&echoint||echochars#这是条件表达式语法,返回值为0则输出int,否则输出chars[root@moli_linux1shell_test]sht3.sh1int[root@moli_linux1shell_test]sht3。shachars3。写一个shell脚本查找句子中少于6个字符的单词idea,需要知道字符串长度的计算方法使用for循环。可以使用expr命令获取字符串的长度,比如计算字符串的几种方法[root@moli_linux1~]char="iamastudent"[root@moli_linux1~]exprlength"$char"14[root@moli_linux1~]echo${#char}14[root@moli_linux1~]echo${char}|wc-L14[root@moli_linux1~]echo${char}|awk'{printlength($0)}'14知道如何统计字符通过使用字符串长度的方法,可以将计算出的长度与整数进行比较得到结果,比如判断下面的语句。(for循环,if判断稍后再说)iamoldboylinuxwelcometoourtraining[root@moli_linux1shell_test]catwordLength.sh#!/bin/bashforniniamoldboywelcometoourtrainingdoif[`exprlength$n`-le6]thenecho$nfidone[root@moli_linux1shell_test]shwordLength.shiamoldboytoour4、基于Shell变量输出读取命令的操作实践除了直接赋值或脚本传参,shell变量还可以使用读取命令从标准输入中获取。语法格式:read[参数][变量名]常用参数:-pprompt:设置提示信息-ttimeout:设置等待输入的时间,默认单位为秒实现读取命令[root@]的基本读取功能moli_linux1~]read-t10-p"Pleaseinputanum:"num#作为接受读取的变量,不应该有$符号#读取一个输入赋值给num变量。请注意,num变量前必须有一个空格。Pleaseinputanum:10#Input10,将数字10赋给num变量[root@moli_linux1~]echo$num10[root@moli_linux1~]read-t10-p"Pleaseinputtwonum:"a1a2#Read两个输入,注意用空格隔开,分别给a1和a2变量赋值。a1变量前后必须有空格。请输入两个num:1020[root@moli_linux1~]echo$a1$a21020[root@moli_linux1~]reada3a43040[root@moli_linux1~]echo$a330[root@moli_linux1~]echo$a440readcommandsmallpractice[root@moli_linux1shell_test]catt2_read_change.sh#!/bin/bash#no.1read-t10-p"Pleasainputtwointnumber:"ab#通过read命令读取两个值并赋值给变量aandvariableb[${#a}-le0]&&{#用条件表达式判断变量值的长度是否小于1一个数是否为空echo"第一个数为null."exit1}[${#b}-le0]&&{#用条件表达式判断变量值长度是否小于1是否为空echo"第二个数为null"。exit2}#no.2判断传入的参数是否为整数expr$a+1&>/dev/nullRETVAL_A=$?表达式$b+1&>/dev/nullRETVAL_B=$?#如果A和B其中之一不是整数,则打印提示并退出if[$RETVAL_A-ne0-o$RETVAL_B-ne0];thenecho"其中一个num不是num,请重新输入。"退出3fi#no.3echo"a+b=$(($a+$b))"echo"a-b=$(($a-$b))"echo"a*b=$(($a*$b))"echo"a/b=$(($a/$b))"Shell在bash中各种条件结构和流控结构的条件测试和对比,进行各种测试,根据测试结果进行不同的操作.条件测试表达式执行后,通常返回true或false。在bash编程中,条件测试常用的语法有四种。条件测试语法描述test
