SHELL第三次作业

语法:

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值