四、流程控制之条件判断

四、流程控制之条件判断

4.1 if单分支结构

bash 复制代码
if <条件表达式>
then
    指令
fi

4.2 if双分支结构

复制代码
if <条件表达式>
then
    指令序列1
else
    指令序列2
fi

4.3 if多分支结构

bash 复制代码
if 条件表达式1
then
    命令序列1
elif 条件表达式2
then
    命令序列2
elif 条件表达式3
then
    命令序列3
else
    命令序列n
fi

4.4 案例

1、编写脚本choice1.sh,判断当前脚本的执行者,若不是root账户则提示,并退出。

bash 复制代码
# 检查当前账户的4种方法
[root@server ~]# whoami
root
[root@server ~]# echo  $USER
root

[root@server ~]# id -u
0
[root@server ~]# echo  $UID
0

[root@server ~]# mkdir /shell/chap04
[root@server ~]# vim  /shell/chap04/choice1.sh
#!/bin/bash

if [ "$USER" != "root"  ]
then    
        echo  "please switch  user root"
fi   

# root账户下执行
[root@server ~]# bash  /shell/chap04/choice1.sh 
#在普通用户下执行
[root@server ~]# id 1000
用户id=1000(redhat) 组id=1000(redhat) 组=1000(redhat)
[root@server ~]# su - redhat
[redhat@server ~]$ bash /shell/chap04/choice1.sh 
please switch  user root

2、编写脚本choice2.sh,利用单分支结构实现输入2个整数,输出最大值

bash 复制代码
[root@server ~]# vim /shell/chap04/choice2.sh
#!/bin/bash

read  -p  "请输入第一个整数: " x 
read  -p  "请输入第二个整数: " y
max=$x
if (($max<y))
then
        max=$y
fi

echo  "最大值:$max"

3、编写脚本choice3.sh,判断系统内存剩余容量大小,若低于100M则发送消息进行告警。

bash 复制代码
[root@server ~]# vim /shell/chap04/choice3.sh
#!/bin/bash
free_mem=$( free -m  |  grep  Mem  |  tr -s  " "  |  cut -d " " -f4 )

if [ "$free_mem" -lt 100 ]
then
        echo  "警告,剩余内存:$free_mem ,低于警戒线100MB"
else
        echo  "剩余内存:$free_mem,空间足够."
fi

#命令说明
# free -m :表示显示内存即虚拟内存的信息,-m以MB单位显示数字
# grep  Mem:表示过滤包含Mem所在行
# tr  -s " ": 压缩空格为一个
# cut -d " " -f4 :以空格为间隔符,取其第4部分
#此处如果需要发送邮件告警,需要配置邮件认证信息,详细配置可参考rhce服务部分第一章节的实验6的邮件配置

4、编写脚本choice4.sh,判断sshd是否运行

bash 复制代码
# 根据进程数判断
[root@server ~]# ps -ef  |  grep  sshd | grep -v grep | wc -l

# 根据端口号判断
[root@server ~]# netstat  -lntup  |  grep  22 | wc -l
bash 复制代码
[root@server ~]# vim  /shell/chap04/choice4.sh
#!/bin/bash

num=$(ps -ef  |  grep  sshd | grep -v grep | wc -l)

if [ "$num" -gt 0 ]
then
        echo  "sshd is running"
else
        echo  "sshd is not running"
fi

5、编写脚本choice5.sh,判断主机是否存活。

bash 复制代码
[root@server ~]# vim  choice5.sh
#!/bin/bash

read -p "请输入测试主机的IP地址:"  ip

ping  -c 2 -w 3 $ip &> /dev/null
# -c 2 表示发送2个数据包,-w 3 表示等待3秒结束,注意:不能等待1秒结束,有可能第二个数据包没有ping完就结束了,会返回错误的状态码

if [ $? -eq 0  ]
then
        echo  "主机$ip 已运行"  #可以设置打印内容颜色显示,具体用法参考1.3章节内容
else
        echo  "主机$ip 未运行"
fi

6、编写脚本choice6.sh,判断当前主机cpu生产商

bash 复制代码
[root@server ~]# vim /shell/chap04/choice6.sh
#!/bin/bash

vendor=$(grep  "vendor_id"  /proc/cpuinfo | uniq | cut -d " " -f2)

if [ "$vendor" == "GenuineIntel" ]
then
        echo  "Intel"
elif [ "$vendor" == "AuthenticAMD" ]
then
        echo  "AMD"
else
        echo   "other"
fi

7、编写脚本choice7.sh,根据用户输入一个字符,判断是数字、字母、其它字符。

bash 复制代码
[root@server ~]# vim /shell/chap04/choice7.sh
#!/bin/bash

read -p  "请输入数字、字母、符号: "  str

if  echo  $str | grep  [a-zA-Z] > /dev/null
then
        echo  "字母"
elif   echo  $str | grep  [0-9] > /dev/null
then
        echo  "数字"
else
        echo  "符号"
fi

#此处的判断也可以使用[[]]配合正则去判断

8、编写脚本choice8.sh,根据用户输入字符,判断是纯数字、纯字母、数字和字母、符号、其它。

bash 复制代码
[root@server ~]# vim /shell/chap04/choice8.sh
#!/bin/bash

read -p  "请输入数字、字母、符号: "  str

if  [[ "$str" =~ ^[a-Z]+$ ]]
then
        echo  "字母"
elif   [[ "$str" =~ ^[0-9]+$ ]]
then
        echo  "数字"
elif [[ "$str" =~ ^[0-9a-Z]+$ ]]
then
        echo  "字母和数字"
elif [[ "$str" =~ ^[^0-9a-Z]+$ ]]
then
        echo "符号"
else
        echo "其它"
fi

9、编写脚本choice9.sh,实现闰年的判断

bash 复制代码
[root@server ~]# vim  /shell/chap04/choice9.sh
#!/bin/bash

read -p "请输入4位数整数年份: " year
if [[ "$year" =~ ^[0-9]{4}$  ]]         
then
        if ((year%4==0)) && ((year%100!=0)) || ((year%400==0))
        then
                echo  "闰年"
        else
                echo  "平年"
        fi
else
        echo  "请输入有效4位整数年份"
fi

10、根据用户输入成绩,判断优良中差。

85-100 优秀--A

70-84 良好--B

60-69 合格--C

60分以下不合格--D

shell 复制代码
[root@server ~]# vim  /shell/chap04/choice10.sh
#!/bin/bash
read -p "please input your score:" score
if [ -z "$score" ];then
        echo "you must input your score"
        exit 1
fi
if [ "$score" -lt 0 -o "$score" -gt 100 ];then
        echo "invalid score"
        exit 2
fi
if [ "$score" -ge 85 ];then
        echo "A"
elif [ "$score" -ge 70 ];then
        echo "B"
elif [ "$score" -ge 60 ];then
        echo "C"
else
        echo "D"
fi

4、多条件判断语句case

case语句语法:

shell 复制代码
case 变量名 in
  值1) 
   指令1 
  ;; 
  值2) 
   指令2 
  ;; 
  值3) 
   指令3 
  ;; 
   *)
    默认
esac
  • case语句会将该变量的值与 )括号中的值相比较,如果与某个值相等,则执行对应语句。
  • 当遇到";;"符号时,就跳出case语句,执行esac语句后面的语句。
  • 如果没有与任何一个值相匹配,则执行*)后面的一组语句。

示例1:将判断分数范围用case语句实现

shell 复制代码
[root@server ~]# vim  /shell/chap04/choice11.sh
#!/bin/bash
read -p "please enter your score (0-100):" grade
case "$grade" in
  8[5-9]|9[0-9]|100)
    echo "A"
  ;;
  7[0-9]|8[0-4])
    echo "B"
  ;;
  6[0-9])
    echo "C"
  ;;
  *)
    echo "D"
esac

示例2:判断今天是星期几,根据星期几来判断今天是工作日还是休息日。

bash 复制代码
[root@server ~]# vim  /shell/chap04/choice12.sh
#!/bin/bash
a=`date +%w`    #将日期转换为星期几,用数字0-6表示
case $a in
[1-5])
        echo "今天是工作日,社畜没有休息日哦。";;
[067])
        echo "今天是休息日,请好好休息哦~";;
esac
相关推荐
AI砖家1 小时前
DeepSeek TUI 保姆级安装配置全指南 -Windows||macOS双平台全覆盖
服务器·前端·人工智能·windows·macos·ai编程·策略模式
虎头金猫1 小时前
Beszel 轻量服务器监控:多台服务器状态统一看,搭起来比 Prometheus 省事太多
linux·运维·服务器·分布式·kafka·开源·prometheus
凌晨一点的秃头猪1 小时前
在cursor里面设置免密码登录服务器理解
linux·运维·服务器
运维行者_1 小时前
理解应用性能监控
大数据·服务器·网络·数据库·人工智能·网络协议·安全
代码AC不AC1 小时前
【Linux】 信号 及 信号产生
linux·信号·信号产生
zt1985q1 小时前
本地部署搜索引擎 Yacy 并实现外部访问
运维·服务器·网络·网络协议·搜索引擎
不做无法实现的梦~1 小时前
桌面图标无法隐藏的解决办法
运维·服务器
量子炒饭大师1 小时前
【Linux系统编程】Cyberpunk在霓虹丛林中构建堡垒 ——【基础开发工具(1)】一文带你初步了解 软件包管理器 并 快速上手 yum和apt 工具
java·linux·运维·apt·yum·软件包管理器
号码认证服务1 小时前
企业固话号码认证能覆盖哪些手机品牌?支持华为、小米、OPPO、vivo等机型
服务器·网络·经验分享·python·华为·智能手机·云计算