当前位置: 首页 > Linux

Linux运维-Shell脚本(二)

时间:2023-04-07 01:43:36 Linux

系列笔记传送门: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使用test命令执行条件测试表达式。测试命令和表达式之间至少有一个空格。[]这是最常用的条件测试表达式。测试用[]括在方括号中的表达式的方法。[]左右边??界之间至少有一个空格[[]]类似于[],不太常用,但可以使用通配符等进行模式匹配,通常是[[]]左右边界至少有一个空格(())使用双括号,两端不需要空格。常用于if中的计算。注意:&&、||、<、>等运算符可以用在[[]]中,但不能用在[]中,一般在[]中使用-a、-o、-gt等。文件测试表达式测试条件测试表达式练习测试命令的语法为:test<测试表达式>对于下面的命令,test-ffile&&echotrue||echofalse这条语句的意思是如果文件file存在则输出true,否则输出false。-f参数是文件测试表达式之一,用于测试文件是否为普通文件。测试表达式也可以只使用半逻辑,&&或||,如:test-ffile&&echotrue#如果表达式为真,则输出truetest-ffile||echofalse#如果表达式不为真,则输出flasetest-option用于测试字符串长度,如果为0则表达式为真,否则不为真[root@moli_linux1~]test-z"study"&&echo1||echo00#因为学习字符串的长度不为0,所以输出0[]条件测试表达式练习[]条件测试表达式的语法:[<测试表达式>]对于下面的语句:[-f/tmp/test.txt]&&回显1||如果/tmp/下有测试则回显0。txt文件存在则输出1,否则输出0。--1rootroot0January2318:32oldboy[root@moli_linux1shelltest][-foldboy]&&echo1||echo0#文件存在,条件为真,输出11个目录文件(测试文件类型)[root@moli_linux1shelltest]mkdiroldgirl#创建目录文件oldgirl[root@moli_linux1shelltest][-foldgirl]&&echo1||echo0#输出为0,说明文件oldgirl不是普通文件,oldgirl是一个确实不是普通文件的目录0[root@moli_linux1shelltest][-eoldgirl]&&echo1||echo01#输出为1,证明文件oldgirl存在,不管oldfirl是目录还是普通文件[root@moli_linux1shelltest][-doldgirl]&&回显1||echo01#输出为1,证明oldgirl是一个目录[root@moli_linux1shelltest][-doldboy]&&echo1||echo00#输出为0,证明oldboy不是目录字符串测试表达式字符串测试运算符的作用:比较两个字符串是否相同,测试字符串长度是否为零,字符串是否为null,ETC。示例:[root@moli_linux1shell_test][-n"abc"]&&echo1||echo01#字符串abc的长度不为0,表达式为真,输出1[root@moli_linux1shell_test][-z"abc"]&&echo1||echo00#字符串abc的长度不为0,表达式为false,输出为0[root@moli_linux1shell_test]test-n"abc"&&echo11#使用test相当于[][root@moli_linux1shell_test]char=abc[root@moli_linux1shell_test]test-n"$char"&&echo11#使用变量时注意双引号[root@moli_linux1shell_test][-z"$char"]&&echo1||echo00[root@moli_linux1shell_test]char1=abc[root@moli_linux1shell_test]char2=abc[root@moli_linux1shell_test]char3=moli[root@moli_linux1shell_test]["$char1"="$char2"]&&echo1||echo01#变量char1等于char2,表达式为真,输出1[root@moli_linux1shell_test]["$char1"="$char3"]&&echo1||echo00#变量char1不等于char2,表达式如果为假,输出0[root@moli_linux1shell_test]["$char1"!="$char3"]&&echo1||echo01#变量char1不等于char3,表达式为真,输出1个整数二进制比较运算符号示例:[root@moli_linux1~]#a1=98[root@moli_linux1~]#a2=99[root@moli_linux1~]#[$a1-eq$a2]&&echo1||echo0#判断a1和a2是否相等0[root@moli_linux1~]#[$a1-gt$a2]&&echo1||echo0#判断a1是否大于a20[root@moli_linux1~]#[$a1-lt$a2]&&echo1||echo0#判断a1是否小于a21[root@moli_linux1~]#[$a1-ne$a2]&&echo1||echo0#判断a1和a2是否不等于1逻辑运算符例子:[root@moli_linux1shelltest]#[-foldboy.txt-o-foldgirl.txt]&&echo11[root@moli_linux1shelltest]#[-foldboy.txt-a-foldgirl.txt]&&echo11[root@moli_linux1shelltest]#[-foldboy.txt-a-fyounggirl.txt]||echo11[root@moli_linux1shelltest]#[-foldboy.txt-o-fyounggirl.txt]&&echo1||echo01-o规则:左边为真,右边为真,左边为真,右边为假,结果为真,左边为假,右边为真,结果为真,左边为假,右边为假,结果为假——一个规则:左边为真,右边为真,结果为真,左边为真,右边为假,结果为假左为假,右为真,结果为假左为假,右为假,结果为假条件表达式和测试表达式练习脚本总结1通过命令行传入字符或数字,判断传入的字符或数字,并进行相应的操作[root@moli_linux1script]#cattest6.sh#!/bin/bashread-p"plsinputachar:"var#读取用户输入并将其分配给var变量["$var"=="1"]&&{#条件判断,看变量值是否为1echo1exit0}["$var"=="2"]&&{#条件判断,看变量值是否为2echo2exit0}["$var"!="2"-a"$var"!="1"]&&{#如果不等于1或2,打印错误信息并退出echoerrorexit0}例2打印选择菜单,安装选项用一键安装不同的网络服务。[root@moli_linux1脚本]#mkdir-p/root/server/script/lamp.sh[root@moli_linux1脚本]#mkdir-p/root/server/script/lnmp.sh[root@moli_linux1脚本]#echo"echolamp.sh[root@moli_linux1script]#echo"echolnmpisinstalled">lnmp.sh[root@moli_linux1script]#vimmenu.sh#!/bin/bashpath=/root/server/脚本[!-d"$path"]&&mkdir-p$path#!/bin/bashpath=/root/server/script[!-d"$path"]&&mkdir-p$path#menucat</开发/空[$?-ne0]&&{echo"你输入的数字必须是{1|2|3}"exit1}[$num-eq1]&&{echo"开始安装灯"sleep2;[-x"$path/lamp.sh"]||{echo"$path/lamp.shdoesnotexitorcannorbeexec"exit1}$path/lamp.shexit$?}[$num-eq2]&&{echo"开始安装lnmp"睡觉2;[-x"$path/lnmp.sh"]||{echo"$path/lnmp.shdoesnotexitorcannorbeexec"exit1}$path/lnmp.shexit$?}[$num-eq3]&&{echo再见退出3}[[!$num=~[1-3]]]&&{#<==[[]]echo"你输入的数字必须是{1|2|3}"echo"输入错误"exit4}