shell ---3

news/2025/2/13 23:00:01/

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

[root@shell test]# vim free_mem.sh
#!/bin/bash
free_mem=$(free -m  | grep "Mem:" | tr -s " " | cut -d " " -f4)
if [ "$free_mem" -le 100 ];thenecho "剩余内存:${free_mem},不足100M " | mail -s "内存不足" root@shell
fi
[root@shell test]# chmod a+rx free_mem.sh
[root@shell test]# crontab -e 
*/10 * * * * /test/free_mem.sh &> /dev/null

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

[root@shell test]# vim sshd.sh
#!/bin/bashnu=$( ps -ef | grep sshd | grep -v grep | wc -l)
if [ $nu -ge 1 ]
thenecho "sshd is running"
elseecho "sshd is not running"systemctl start sshd > /dev/null
fi
[root@shell test]# chmod a+xr sshd.sh 
[root@shell test]# bash sshd.sh 
sshd is running

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

[root@shell test]# vim ping.sh
#!/bin/bash
#########################
#File name:ping.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-22 20:03:32
#Description:
#########################
ping -c 2 -w 1 192.168.10.1 &> /dev/null
if [ $? -eq 0 ]
thenecho host is running 
elseecho host is not running
fi
[root@shell test]# chmod a+rx ping.sh
[root@shell test]# bash ping.sh 
host is not running

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

#########################
#File name:score.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-22 20:38:15
#Description:
#########################
read -p "please input your score:" score if [ -z "$score" ];then  echo "you must input your score"  exit 1fiif [ "$score" -lt 0 -o "$score" -gt 100 ];then  echo "score invalid"exit 2i
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
[root@shell test]# vim score.sh
[root@shell test]# chmod a+rx score.sh 
[root@shell test]# ./score.sh 
please input your score:33
D

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

[root@shell test]# vim vendor.sh
#!/bin/bash
#########################
#File name:vendor.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-22 22:49:37
#Description:
#########################
vendor=$(grep "vendor_id" /proc/cpuinfo |uniq|awk -F: '{print $NF}') if [[ "$vendor" =~ [[:space:]]*GenuineIntel$ ]]  
thenecho "intel" elif [[ "$vendor" =~ [[:space:]]*AuthenticAMD$ ]] then
echo "AMD" elseecho "unknow" 
fi
[root@shell test]# chmod a+rx vendor.sh 
[root@shell test]# ./vendor.sh 
intel

http://www.ppmy.cn/news/573933.html

相关文章

智力题:用5L容量和3L容量的瓶子怎么装出4L的水

智力题:用5L容量和3L容量的瓶子怎么装出4L的水 方案一:方案二: 方案一: 先将3L的瓶子装满,把3L瓶子的所有水都倒入5L瓶子再把3L瓶子的水装满,全部导入5L瓶子,5L瓶子装不完,3L瓶子里…

Redis 入门及应用 ( 三 ) 与springboot整合

4.redis 高级概念 4.1.事务 multi —标记一个事务块的开始 exec —执行事务块中的命令 discard — 取消事务,放弃执行事务块内的所有命令。 watch — 监视一个(或多个) key ,如果在事务执行之前这个(或这些) key 被其他命令所改动,那么事务…

程序解:现有3L容器和5L容器各一个,问如何量出4L水(水无限)

穷举所有的可能性,从杯子里面水变化角度来讲,每个状态到下一个状态只有6种行为:3L杯子装满,5L杯子装满,3L倒空,5L倒空,3L倒到5L,5L倒到3L。最终结果如果5L的里面有4L水则得到一个解。…

python实现无刻度3升水和5升水准确得到4升水的代码步骤

瓶子灌水问题: 两个没有刻度的杯子,一个容积是3L,一个容积是5L,怎么计量出4L水? 进阶1: 两个没有刻度的杯子,一个容积是A,一个容积是B,最终可计量水的体积。 c 跟A, B 什么关系? A 3; B 5 C {1, 2, 3, 4, 5} A 2; B 4 C {2,…

逻辑思维面试题-河里的水是无限的,现在有两个水桶分别是5L,6L,问如何从河里取3L水?

河里的水是无限的,现在有两个水桶分别是5L,6L,问如何从河里取3L水? 解1 设: A为5L 。 B为6L。 解: (1)5L的装满,全倒向6L中;此时B中有5L水(空1L). (2)5L的再装满,再倒向…

C语言再学习 -- 文件

文件是什么 一个文件(file)通常就是磁盘上的一段命名的存储区。C 将文件看成是连续的字节序列,其中每一个字节都可以单独地读取。 二进制和文本模式 1、在windows系统中,文本模式下,文件以"\r\n"代表换行。若…

L3-Day13

⏰打卡时间:9月12日(周四) 6:00-17:00训练技巧顺序:【完全听写法】️【车轮法】️【影子跟读法】⏱【练习时间】30 mins句1: But at one time, some businesses refused to employ people who were on a blacklist for belonging …

YoloV4学习笔记5_yolov4-tiny(win10+VC2015+opencv4.4+yolov4)

YoloV4学习笔记5_yolov4-tiny(win10VC2015opencv4.4yolov4) 对于cpu识别或者资源不太充足的设备,yolov4的完整版太慢了。 yolov4-tiny相当于yolov4的缩小版本,速度比完整版快10倍,当然也牺牲了一些准确度。 》YOLOv4-…