当前位置: 首页 > Linux

LinuxShell编程(八)——流程控制语句(二)分支条件语句:if,case

时间:2023-04-07 01:46:04 Linux

一、单分支if语句1.语法if【条件判断公式】;then程序fiorif【条件判断公式】then程序fi的单分支条件语句需要注意的是if语句以fi结尾,区别于一般语言使用的大括号【条件判断公式】是通过test命令判断的,所以必须有方括号和条件判断公式之间是一个空格然后后面是条件后面要执行的程序可以放在[]后面,用;隔开。也可以换行输入,所以不需要;变量,一行显示当前用户currentUser=$(env|grep"^USER="|cut-d"="-f2)if["$currentUser"=="root"]thenecho"当前用户是root.》fi给if1.sh脚本添加执行权限[root/tmp]#chmod+xif1.shroot用户执行这个脚本[root/tmp]#./if1.sh当前用户是root。非root用户执行此脚本[vagrant/tmp]$./if1.sh确定分区使用率#!/bin/bash#统计根分区使用率#将根分区使用率作为变量值赋值给rate=$(df-h|grep'/dev/sda1'|awk'{print$5}'|cut-d'%'-f1)#判断使用率是否大于80if["$rate"-gt80]然后回显“警告!分区使用率过高!”分支if语句1.语法if[条件判断公式]then条件成立时执行的程序else条件不成立时执行的程序/bash#定义输入变量dir使用read-twaittime-p"提示信息"变量名定义输入变量read-t30-p"Pleaseinputyoudir:"dir#[-d"$dir"]判断变量是否为目录if[-d"$dir"]then#If是目录,执行程序echo"Yourinputisadir"else#如果不是目录,则执行此程序echo"Yourinputisnotadir"fi#endifisif2.shfoot这里添加执行权限[root/tmp]#chmod+xif2.sh判断输入的内容是否为目录[root/tmp]#./if2.shPleaseinputyoudir:/root你输入的是一个目录[root/tmp]#./if2.shPleaseinputyoudir:abc你输入的不是dir判断apache是??否启动,如果没有启动就启动#!/bin/bash#拦截httpd进程,并将结果赋值给变量httpdhttpd=$(psaux|grep'httpd'|grep-v'grep')#判断httpd是否为空if[-n"$httpd"]then#如果不为空说明apache运行正常echo"$(date)httpdisok">>/tmp/autohttpd_ok.logelse#如果为空,表示apache停止,启动apache/etc/rc.d/init.d/httpdstart&>>/tmp/autohttpd_err.logecho"$(date)restarthttpd">>/tmp/autohttpd_err.logfi三、多分支if语句1.语法if[条件判断公式1]then条件判断公式1成立时执行的程序elif【条件判断公式2】then条件判断公式2成立时执行的程序...省略更多条件...else当所有条件都不成立时,最后执行的程序为fi2。应用判断用户输入的文件类型file_type.sh#!/bin/bash#接收用户输入的文件名,赋值给fileread-p"Pleaseinputyourfile:"file#判断$file是否为空if[-z"$file"]thenecho"Error,InputisNULL!"退出1elif[!-e"$file"]thenecho"$fileisnotafile!"exit2elif[-f"$file"]thenecho"$fileisaregularfile!"elif[-d"$file"]thenecho"$fileisadirectory!"elseecho"$fileisanotherfile!"fi为file_type.sh脚本添加执行权限[root/tmp]#chmod+xfile_type.sh输入为空,返回错误输出,查看错误码为1[root/tmp]#./file_type.sh请输入你的文件:Error,InputisNULL![root/tmp]#echo$?1inputisnotafile,returnerroroutput,检查错误码为2[root/tmp]#./file_type.shPleaseinputyourfile:abcabcisnotafile![root/tmp]#echo$?2输入的是普通文件[root/tmp]#./file_type.shPleaseinputyourfile:file_type.shfile_type.shisaregularfile![root/tmp]#echo$?0inputisadirectory[root/tmp]#./file_type.shPleaseinputyourfile:/tmp/tmpisadirectory![root/tmp]#echo$?0inputisanotherfile[root/tmp]]#./file_type.sh请输入你的文件:/dev/null/dev/null是另一个文件![root/tmp]#echo$?0命令行界面加减乘除计算器.sh#!/bin/bash#接收用户通过read命令计算的值,赋值给num1和num2read-p"pleaseinputnum1:"-t30num1read-p"pleaseinputnum2:"-t30num2#接收用户通过read命令计算的运算符,赋值给opread-p"pleaseinputoperator:"-t30op#判断$num1,$num2和$op不能为空if[-z"$num1"-o-z"$num2"-o-z"$op"]thenecho"Error:theinputcannotbenull";exit10fi#判断$num1和$num2是否都是数字#将$num1和$num2的数字全部替换为空,并将替换结果赋值给tmp1和tmp2tmp1=$(echo$num1|sed's/[0-9]//g')tmp2=$(echo$num2|sed's/[0-9]//g')#判断tmp1和tmp2是否都为空,如果都为空则都是数字if[-n"$tmp1"-o-n"$tmp2"]thenecho"Error:thenum1andnum2mustbenumber"exit11fi#加法运算if["$op"=="+"]thenresult=$(($num1+$num2))#减法运算elif["$op"=="-"]thenresult=$(($num1-$num2))#乘法运算elif["$op"=="*"]thenresult=$(($num1*$num2))#除法运算elif["$op"=="/"]then#除法运算中除数不能为0if[$num2-eq0]thenecho"错误:除数不能为0"exit12elseresult=$(($num1/$num2))fi#运算符不是+|-|*|/elseecho"Error:operatormustbe+|-|*|/"exit13fi#输出运算结果echo"$num1$op$num2=$result"给calculator.sh脚本添加执行权限[root/tmp]#chmod+xcalculator.shinputnum1andnum2为空,返回错误输出,查看错误码为10[root/tmp]#./calculator.shpleaseinputnum1:pleaseinputnum2:pleaseinputoperator:+Error:inputcannotbenull[root/tmp]#echo$?10inputnum1isnotanumber,returnerroroutput,检查错误码是11[root/tmp]#./calculator.shpleaseinputnum1:apleaseinputnum2:1pleaseinputoperator:+Error:thenum1andnum2mustbenumber[root/tmp]#echo$?11inputdivisoris0,returnerroroutput,检查错误代码是12[root/tmp]#./calculator.shpleaseinputnum1:2pleaseinputnum2:0pleaseinputoperator:/Error:Thedivisorcannotbe0[root/tmp]#echo$?12inputoperationsymbol#,返回错误输出,查看错误码为12[root/tmp]#./calculator.shpleaseinputnum1:1pleaseinputnum2:2pleaseinputoperator:#Error:运算符必须是+|-|*|/[root/tmp]#echo$?13正常加减乘除[root/tmp]#./calculator.shpleaseinputtnum1:1pleaseinputnum2:2pleaseinputoperator:+1+2=3[root/tmp]#./calculator.shpleaseinputnum1:1pleaseinputnum2:2pleaseinputoperator:-1-2=-1[root/tmp]]#./calculator.shpleaseinputnum1:1pleaseinputnum2:2pleaseinputoperator:*1*2=2[root/tmp]#./calculator.shpleaseinputnum1:6pleaseinputnum2:3pleaseinputoperator:/6/3=2四、多分支case条件语句case语句与if...elif...else语句相同,但不同于if多分支条件语句,case语句只能判断一个条件关系,if语句可以判断多种条件关系。1.语法case$variablenamein"value1")当变量的值等于value1时,执??行程序1;;"value2")当变量的值等于value2时,执行程序2;;...省略其他分支..."valuen")当变量的值等于值n时,执行程序n;;*)当变量的值不等于上述值时,执行本程序;;esac注意,最后*)中的*不加双引号2.应用使用case重写计算器。calculator.sh#!/bin/bash#接收用户通过read命令计算的值,赋值给num1和num2read-p"pleaseinputnum1:"-t30num1read-p"pleaseinputnum2:"-t30num2#接收用户通过read命令计算的运算符号,赋值给opread-p"请输入运算符:"-t30op#判断$num1,$num2,$op不能为emptyif[-z"$num1"-o-z"$num2"-o-z"$op"]thenecho"错误:输入不能为空";exit10fi#判断$num1和$num2都是数字吗?#将$num1和$num2中的数字全部替换为空,并将替换结果赋值给tmp1和tmp2tmp1=$(echo$num1|sed's/[0-9]//g')tmp2=$(echo$num2|sed's/[0-9]//g')#判断tmp1和tmp2是否都为空,如果都为空则都是数字if[-n"$tmp1"-o-n"$tmp2"]thenecho"Error:thenum1andnum2mustbenumber"exit11ficase$opin#additionoperation"+")result=$(($num1+$num2));;#减法运算"-")result=$(($num1-$num2));;#乘法运算"*")result=$(($num1*$num2));;#除法运算"/")#除法运算中除数不能为0if[$num2-eq0]thenecho"Error:Thedivisorcannotbe0"exit12elseresult=$(($num1/$num2))fi;;#operation运算符不是+|-|*|/*)echo"Error:operatormustbe+|-|*|/"exit13;;esac#输出运算结果echo"$num1$op$num2=$result"正常加减乘除[root/tmp]#./calculator.shpleaseinputnum1:1pleaseinputnum2:2pleaseinputoperator:+1+2=3[root/tmp]#./calculator.shpleaseinputnum1:1pleaseinputnum2:2pleaseinputoperator:-1-2=-1[root/tmp]#./calculator.shpleaseinputnum1:1pleaseinputnum2:2pleaseinputoperator:*1*2=2[root/tmp]#./calculator.shpleaseinputnum1:6pleaseinputnum2:3pleaseinputoperator:/6/3=2当输入非运算符时,会输出错误信息[root/tmp]#./calculator.shpleaseinputnum1:1pleaseinputnum2:1pleaseinputoperator:%Error:operatormustbe+|-|*|/