语法:
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。
一、判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中。
[root@localhost shell]# vim judge_cpuinfo.sh
#!/bin/bash
cpu=`cat /proc/cpuinfo |grep vendor_id |cut -d ":" -f2|tail -1`
if [[ "$cpu" =~ [[:space:]]*GenuineIntel$ ]]
then
echo "intel"
elif [[ "$cpu" =~ [[:space:]]*AuthenticAMD$ ]]
then
echo "AMD"
else
echo "other"
fi
[root@localhost shell]# bash judge_cpuinfo.sh
intel
二、根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)。
[root@localhost shell]# vim performance.sh
#!/bin/bash
read -p "请输入你的分数:" score
if [[ -z "$score" ]]
then
echo "输出不能为空!!!"
elif echo "$score"|grep "[a-zA-Z]" >/dev/null
then
echo "请输入有效数字!!!"
elif [[ $score -gt 100 || $score -lt 0 ]]
then
echo "请输出正确分数!!!!"
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 shell]# sh performance.sh
请输入你的分数:55
你的成绩为D
[root@localhost shell]# sh performance.sh
请输入你的分数:66
你的成绩为C
[root@localhost shell]# sh performance.sh
请输入你的分数:77
你的成绩为B
[root@localhost shell]# sh performance.sh
请输入你的分数:88
你的成绩为A
[root@localhost shell]# sh performance.sh
请输入你的分数:
输出不能为空!!!
[root@localhost shell]# sh performance.sh
请输入你的分数:a6
performance.sh: line 13: dev/null: No such file or directory
performance.sh: line 22: test: a6: integer expression expected
performance.sh: line 25: [: a6: integer expression expected
你的成绩为D
[root@localhost shell]# sh performance.sh
请输入你的分数:ah
请输入有效数字!!!
[root@localhost shell]# sh performance.sh
请输入你的分数:a6
请输入有效数字!!!
三、判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)。
[root@localhost shell]# vim sshd_running.sh
#!/bin/bash
#查看进程
num1=`ps -ef|grep sshd|grep -v grep|wc -l`
if test "$num1" -ge 1
then
echo sshd is running
else
echo sshd is off
fi
#查看端口号
num2=`ss -lntup|grep 22|wc -l`
if [[ "$num2" -ge 1 ]]
then
echo sshd is running
else
echo sshd is running
fi
[root@localhost shell]# sh sshd_running.sh
sshd is running
sshd is running
四、检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)。
[root@localhost shell]# vim ping.sh
#!/bin/bash
for i in {128..130}
do
ping -i 0.5 -c 2 -w 1 192.168.132.$i >/dev/null
if [ $? -eq 0 ]
then
echo "192.168.132.$1 is online"
else
echo "192.168.132.$I is off"
fi
done
[root@localhost shell]# sh ping.sh
192.168.132. is online
192.168.132. is online
192.168.132. is off
五、编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。
[root@localhost shell]# vim free_mem.sh
#!/bin/bash
#########################
#File name:free_mem.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 08:18:01
#Description:
#########################
size=`free -m|grep Mem|tr -s " "|cut -d ' ' -f4`
if [[ $size -lt 3000 ]]
then
echo "free:"$size" free less 100!!!M" |mail -s "danger" root@localhost
fi
[root@localhost shell]# sh free_mem.sh
[root@localhost shell]# chmod a+rx free_mem.sh ##使用计划任务
[root@localhost shell]# crontab -e
*/10 * * * * /test/free_mem.sh &>/dev/null