2023年8月2日发(作者:)
linux多分⽀结构case,LinuxShell编程(8)-流程控制语句(⼆)分⽀条件语。。。⼀、单分⽀if语句1. 语法if [ 条件判断式 ];then程序fi或者if [ 条件判断式 ]then程序fi单分⽀条件语句需要注意if 语句使⽤ fi 结尾,和⼀般语⾔使⽤⼤括号结尾不同[ 条件判断式 ] 就是使⽤ test 命令判断,所以中括号和条件判断式之间必须有空格then 后⾯跟符合条件之后执⾏的程序,可以放在 [] 之后,⽤ ; 分隔。也可以换⾏输⼊,就不需要 ; 了2. 应⽤判断登录⽤户是否是root创建脚本⽂件,⽂件内容如下#!/bin/bash#env是linux的⼀个外部命令,可以显⽰当前⽤户的环境变量,其中⼀⾏显⽰当前⽤户currentUser=$(env | grep "^USER=" | cut -d "=" -f 2)if [ "$currentUser" == "root" ]thenecho "Current user is root."fi为 脚本添加执⾏权限[root/tmp]# chmod +x ⽤户执⾏此脚本[root/tmp]# ./ent user is root.⾮ root ⽤户执⾏此脚本[vagrant/tmp]$ ./判断分区使⽤率#!/bin/bash# 统计根分区使⽤率# 把根分区使⽤率作为变量值赋予 raterate=$(df -h | grep '/dev/sda1' | awk '{print $5}' | cut -d '%' -f 1)# 判断使⽤率是否⼤于 80if [ "$rate" -gt 80 ]thenecho "Warning! 分区使⽤率过⾼!"fi⼆、双分⽀if语句1. 语法if [ 条件判断式 ]then条件成⽴时执⾏的程序else条件不成⽴时执⾏的程序fi2. 应⽤判断输⼊内容是否是⼀个⽬录创建脚本⽂件,⽂件内容如下#!/bin/bash#定义输⼊的变量 dir ⽤read -t 等待时间 -p "提⽰信息" 变量名 定义输⼊的变量read -t 30 -p "Please input you dir :" dir#[ -d "$dir" ] 判断变量是否是⽬录if [ -d "$dir" ]then#如果是⽬录则执⾏程序echo "Your input is a dir"else#如果不是⽬录这执⾏这个程序echo "Your input is not a dir"fi #结束if为 脚本添加执⾏权限[root/tmp]# chmod +x 判断输⼊内容是否是⼀个⽬录[root/tmp]# ./se input you dir :/rootYour input is a dir[root/tmp]# ./se input you dir :abcYour input is not a dir判断 apache 是否启动,没启动则启动它#!/bin/bash# 截取httpd进程,并把结果赋予变量httpdhttpd=$(ps aux | grep 'httpd' | grep -v 'grep')# 判断 httpd 是否为空if [ -n "$httpd" ]then# 若不为空,则说明apache正常运⾏echo "$(date) httpd is ok" >> /tmp/autohttpd_e# 若为空,则说明apache停⽌,启动 apache/etc/rc.d/init.d/httpd start &>> /tmp/autohttpd_o "$(date) restart httpd" >> /tmp/autohttpd_三、多分⽀if语句1. 语法if [ 条件判断式1 ]then条件判断式1成⽴时执⾏的程序elif [ 条件判断式2 ]then条件判断式2成⽴时执⾏的程序... 省略更多条件 ...else当所有条件都不成⽴时,最后执⾏的程序fi2. 应⽤判断⽤户输⼊的是什么类型的⽂件file_#!/bin/bash# 接收⽤户输⼊的⽂件名,赋值给fileread -p "Please input your file: " file# 判断 $file 是否为空if [ -z "$file" ]thenecho "Error,Input is NULL!"exit 1elif [ ! -e "$file" ]thenecho "$file is not a file!"exit 2elif [ -f "$file" ]thenecho "$file is a regular file!"elif [ -d "$file" ]thenecho "$file is a directory!"elseecho "$file is an other file!"fi为 file_脚本添加执⾏权限[root/tmp]# chmod +x file_输⼊为空,返回错误输出,查看错误码为1[root/tmp]# ./file_se input your file:Error,Input is NULL![root/tmp]# echo $?1输⼊不是⽂件,返回错误输出,查看错误码为2[root/tmp]# ./file_se input your file: abcabc is not a file![root/tmp]# echo $?2输⼊是⼀个普通⽂件[root/tmp]# ./file_se input your file: file__ is a regular file![root/tmp]# echo $?0输⼊是⼀个⽬录[root/tmp]# ./file_se input your file: /tmp/tmp is a directory![root/tmp]# echo $?0输⼊是⼀个其他⽂件[root/tmp]# ./file_se input your file: /dev/null/dev/null is an other file![root/tmp]# echo $?0命令⾏界⾯加减乘除计算器#!/bin/bash# 通过read命令接收⽤户输⼊要计算的数值,并赋值给 num1 和 num2read -p "please input num1: " -t 30 num1read -p "please input num2: " -t 30 num2# 通过read命令接收⽤户输⼊要计算的运算符号,并赋值给 opread -p "please input operator: " -t 30 op# 判断 $num1,$num2 和 $op 都不能为空if [ -z "$num1" -o -z "$num2" -o -z "$op" ]thenecho "Error: the input cannot be null";exit 10fi# 判断 $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: the num1 and num2 must be number"exit 11fi# 加法运算if [ "$op" == "+" ]thenresult=$(($num1 + $num2))# 减法运算elif [ "$op" == "-" ]thenresult=$(($num1 - $num2))# 乘法运算elif [ "$op" == "*" ]thenresult=$(($num1 * $num2))# 除法运算elif [ "$op" == "/" ]then# 除法运算中除数不能为0if [ $num2 -eq 0 ]thenecho "Error: The divisor cannot be 0"exit 12elseresult=$(($num1 / $num2))fi# 运算符不是 + | - | * | /elseecho "Error: the operator must be + | - | * | /"exit 13fi# 输出运算结果echo "$num1 $op $num2 = $result"为 脚本添加执⾏权限[root/tmp]# chmod +x 输⼊num1和num2为空,返回错误输出,查看错误码为10[root/tmp]# ./se input num1:please input num2:please input operator: +Error: the input cannot be null[root/tmp]# echo $?10输⼊num1不是数字,返回错误输出,查看错误码为11[root/tmp]# ./se input num1: aplease input num2: 1please input operator: +Error: the num1 and num2 must be number[root/tmp]# echo $?11输⼊除数为0,返回错误输出,查看错误码为12[root/tmp]# ./se input num1: 2please input num2: 0please input operator: /Error: The divisor cannot be 0[root/tmp]# echo $?12输⼊运算符号 #,返回错误输出,查看错误码为12[root/tmp]# ./se input num1: 1please input num2: 2please input operator: #Error: the operator must be + | - | * | /[root/tmp]# echo $?13正常的加减乘除运算[root/tmp]# ./se input num1: 1please input num2: 2please input operator: +1 + 2 = 3[root/tmp]# ./se input num1: 1please input num2: 2please input operator: -1 - 2 = -1[root/tmp]# ./se input num1: 1please input num2: 2please input operator: *1 * 2 = 2[root/tmp]# ./se input num1: 6please input num2: 3please input operator: /6 / 3 = 2四、多分⽀case条件语句case 语句和 if...lse 语句⼀样都是多分⽀条件语句。不过和 if 多分⽀条件语句不同的是,case 语句只能判断⼀种条件关系,⽽ if 语句可以判断多种条件关系。1. 语法case $变量名 in"值1")变量的值等于值1时,执⾏程序1;;"值2")变量的值等于值2时,执⾏程序2;;...省略其他分⽀..."值n")变量的值等于值n时,执⾏程序n;;*)变量的值都不等于以上的值时,执⾏此程序;;esac注意,最后的 *) 中 * 不加双引号2. 应⽤使⽤case重写计算器#!/bin/bash# 通过read命令接收⽤户输⼊要计算的数值,并赋值给 num1 和 num2read -p "please input num1: " -t 30 num1read -p "please input num2: " -t 30 num2# 通过read命令接收⽤户输⼊要计算的运算符号,并赋值给 opread -p "please input operator: " -t 30 op# 判断 $num1,$num2 和 $op 都不能为空if [ -z "$num1" -o -z "$num2" -o -z "$op" ]thenecho "Error: the input cannot be null";exit 10fi# 判断 $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: the num1 and num2 must be number"exit 11ficase $op in# 加法运算"+")result=$(($num1 + $num2));;# 减法运算"-")result=$(($num1 - $num2));;# 乘法运算"*")result=$(($num1 * $num2));;# 除法运算"/")# 除法运算中除数不能为0if [ $num2 -eq 0 ]thenecho "Error: The divisor cannot be 0"exit 12elseresult=$(($num1 / $num2))fi;;# 运算符不是 + | - | * | /*)echo "Error: the operator must be + | - | * | /"exit 13;;esac# 输出运算结果echo "$num1 $op $num2 = $result"正常的加减乘除运算[root/tmp]# ./se input num1: 1please input num2: 2please input operator: +1 + 2 = 3[root/tmp]# ./se input num1: 1please input num2: 2please input operator: -1 - 2 = -1[root/tmp]# ./se input num1: 1please input num2: 2please input operator: *1 * 2 = 2[root/tmp]# ./se input num1: 6please input num2: 3please input operator: /6 / 3 = 2输⼊⾮运算符时,输出错误信息[root/tmp]# ./se input num1: 1please input num2: 1please input operator: %Error: the operator must be + | - | * | /
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690916715a462885.html
评论列表(0条)