shell流程控制之条件判断,一分钟秒懂!用最简单的条件语句执行程序

目录

题目:

一、if条件语句的语法

1、单分支结构

2、 双分支结构

3、多分支结构 


题目:

1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。

        条件判断语句是一种最简单的流程控制语句。该语句使得程序根据不同的条件来执行不同的程序分支。

一、if条件语句的语法

1、单分支结构

第一种语法: 
if <条件表达式> 
then 
    指令 
fi

第二种语法: 
if <条件表达式>;then 
    指令 
fi

2、 双分支结构

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

3、多分支结构 

if 条件表达式1 ;then
    命令序列1 
elif 
    条件表达式2 ;then
    命令序列2 
elif 
    条件表达式3 ;then
    命令序列3 
else
    命令序列n 
fi
        在上面的语法中,当整个if elif 语句结构中的第 1 个条件表达式为真,则执行第 1 then 子句中的语句statement1;否则,继续下面的判断。如果条件表达式 2 的值为真,则执行第 2 then 子句中的语句,以此类推。如果所有的条件表达式的值都为假,则执行最后的else 子句中的语句。最后是 if elif 结构的结束标志fi

1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中 

[root@localhost day3]# vim practice_cpu.sh 
#!/bin/bash
CODE=`cat /proc/cpuinfo | grep "vendor_id" | uniq | cut -d ":" -f 2`

if [[ $CODE =~ [[:space:]]*GenuineIntel$ ]] ;then
    echo "CUP is GenuineIntel"
elif [[ $CODE =~ [[:space:]]*AuthenticAMD$ ]] ;then
    echo "CPU is AuthenticAMD"
else
    echo "Unrecognized"
fi

[root@localhost day3]# bash practice_cpu.sh 
CUP is GenuineIntel

2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)

[root@localhost day3]# vim practice_score.sh 
#!/bin/bash
read -p "please input your score(0-100):" score
if test -z "$score" ;then
    echo "you must input your score"
    exit 1
fi

expr $score + 1 &> /dev/null
return_score=$?
if ! [ "$return_score" -eq 0 ];then
    echo "please input a number"
    exit 2
fi

if [[ "$score" -gt 100 || "$score" -lt 0 ]];then
    echo "Please enter a number between 0 and 100"
elif [ "$score" -ge 85 ];then
    echo "A"
elif test "$score" -ge 70 ;then
    echo "B"
elif [ "$score" -ge 60 ];then
    echo "C"
else
    echo "D"
fi

[root@localhost day3]# bash practice_score.sh 
please input your score(0-100):200
Please enter a number between 0 and 100
[root@localhost day3]# bash practice_score.sh 
please input your score(0-100):-8
Please enter a number between 0 and 100
[root@localhost day3]# bash practice_score.sh 
please input your score(0-100):av
please input a number
[root@localhost day3]# bash practice_score.sh 
please input your score(0-100):95
A
[root@localhost day3]# bash practice_score.sh 
please input your score(0-100):81
B

3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)

[root@localhost day3]# vim practice_sshd.sh
#!/bin/bash
echo "查看进程:"
CODE1=`ps -ef | grep sshd | grep -v grep | wc -l`
CODE2=`netstat -lntup | grep -w 22 | wc -l`

if [ $CODE1 -ge 1 ];then
    echo sshd is running
else
    echo sshd is not running
fi
echo "查看端口:"
if [ $CODE2 -ge 1 ];then
    echo sshd is running
else
    echo sshd is not running
fi

[root@localhost day3]# bash practice_sshd.sh 
查看进程:
sshd is running
查看端口:
sshd is running

4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)

[root@servera day3]# vim practice_ping.sh

for i in {0..2}
do
    ping -c 3 -W 1 192.168.10.20"$i" &> /dev/null
    if [ $? -eq 0 ];then
        echo "192.168.10.20$i is running"
    else
        echo "192.168.10.20$i is dead"
    fi
done


[root@servera day3]# bash practice_ping.sh
192.168.10.200 is running
192.168.10.201 is running
192.168.10.202 is running
[root@servera day3]# bash practice_ping.sh
192.168.10.200 is running
192.168.10.201 is dead
192.168.10.202 is running

5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。 

[root@localhost day3]# vim practice_mail.sh
#!/bin/bash
rpm -q sendmail &> /dev/null
CODE=$?
if [ $CODE -ne 0 ] ;then
    yum install -y sendmail  &> /dev/null
    echo "Downloaded sendmail"
else
    echo "sendmail alreay install"
fi
rpm -q mailx &> /dev/null
CODE1=$?
if [ $CODE1 -ne 0 ] ;then
    yum install -y mailx  &> /dev/null
    echo "Downloaded mailx"
else
    echo  "mailx alreay"
fifree=`free -m | grep Mem | tr -s " " | cut -d " " -f 4`
if [ "$free" -le 6000 ] ;then
    echo "剩余内存:${free},低于100M" | mail -s "内存报警" redhat@localhost
fi 

[root@localhost day3]# chmod a+rx free_mem.sh 
[root@localhost day3]# crontab -e
*/10 * * * * /root/shell/day3/practice_mail.sh &>/dev/null
[root@localhost day3]# su - redhat

[redhat@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/redhat": 3 messages 3 new
>N  3 root                  Fri Aug 19 15:04  21/757   "内存报警"
& 3
Message  3:
From root@servera  Fri Aug 19 15:04:06 2022
Return-Path: <root@localhost>
From: root <root@localhost>
Date: Fri, 19 Aug 2022 15:05:01 +0800
To: syc@servera
Subject: 内存报警
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
Status: R

warning: your compter memory is less than 6000M

& 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值