判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
使用grep过滤 /proc/cpuinfo 中有 vendor_id 的行
uniq 除去重复值
awk -F: '{print $NF}' 输出vendor_id后的字段
-F /表示以 ’/’ 分开域
NF 表示的是浏览记录的域的个数
$NF 表示的最后一个Field(列),即输出最后一个字段的内容
1 #!/bin/bash
2 #########################
3 vendor=$(grep "vendor_id" /proc/cpuinfo | uniq | awk -F: '{print $NF}')
4 if [[ "$vendor" =~ [[:space:]]*GenuineIntel$ ]]
5 then
6 echo "intel"
7 elif [[ "$vendor" =~ [[:space:]]*AuthenticAMD$ ]]
8 then
9 echo "amd"
10 else
11 echo "unknow"
12 fi
13
[root@r8 shell_ex]# bash cpuinfo_shell.sh
amd
根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
注意边界问题:
输入的是个整数
范围在0-100
利用前面的测试结果进行简化脚本
1 #!/bin/bash
2 #########################
3 read -p "please input your score:" score
4 if [ -z "$score" ];then
5 echo "you must input your score"
6 exit 1
7 fi
8
9 expr $score "+" 10 &> /dev/null
10 exit_code=$?
11
12 if [ $exit_code -ne 0 ];then
13 echo "please input a number(type)"
14 exit 2
15 fi
16
17 if ! [ $score -ge 0 -a $score -le 100 ];then
18 echo "please input a number in range(1,100)"
19 exit 3
20 fi
21
22 if [ $score -ge 85 ];then
23 echo "A"
24
25 elif [ $score -ge 70 ];then
26 echo "B"
27
28 elif [ $score -ge 60 ];then
29 echo "C"
30
31 else
32 echo "D"
[root@r8 shell_ex]# bash shell_score.sh
please input your score:90
A
[root@r8 shell_ex]# bash shell_score.sh
please input your score:44
D
[root@r8 shell_ex]# bash shell_score.sh
please input your score:asdad
please input a number(type)
[root@r8 shell_ex]# bash shell_score.sh
please input your score:9999
please input a number in range(1,100)
判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动
过滤关于 sshd 服务的状态(也可测试关于ssh的进程数量来进行条件判断)
为running则输出相应内容
反之则输出对应的内容,并且启动sshd
1 #!/bin/bash
2 #########################
3
4 #res0=$(ps -ef | grep sshd | grep -v grep | wc -l)
5 systemctl status sshd | grep "running" > /dev/null
6 if [ $? -eq 0 ]
7 then
8 echo 'sshd is running'
9 else
10 echo 'sshd is not running'
11 systemctl start sshd > /dev/null
12 fi
[root@r8 shell_ex]# systemctl stop sshd
[root@r8 shell_ex]# bash sshd_shell.sh
sshd is not running
[root@r8 shell_ex]# bash sshd_shell.sh
sshd is running
检查主机是否存活,并输出结果
采用for循环
获取ping相应主机后的丢包率,来进行条件判断,进而输出相应内容
1 #!/bin/bash
2 #########################
3 for IP in 192.168.10.20{0..3}
4 do
5 ping -c 2 -w 1 $IP | grep "100%" &> /dev/null
6 if [ $? -eq 1 ];then
7 echo "$IP is running"
8 else
9 echo "$IP is not running"
10 fi
11 done
[root@r8 shell_ex]# bash ping_host.sh
192.168.10.200 is running
192.168.10.201 is not running
192.168.10.202 is not running
192.168.10.203 is not running
编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每 10分钟检查一次。
截取剩余内存进行条件判断
tr -s " " 删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串 删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串
cut -d " " 截取第四个字段
1 #!/bin/bash
2 #########################
3 if [ "$USER" != "root" ];then
4 echo "please switch user root"
5 fi
6
7 free_mem=$(free -m | grep "Mem:" | tr -s " " | cut -d " " -f4)
8 if [ "$free_mem" -le 12000 ];then
9 echo "剩余内存:${free_mem},低于12000M" | mail -s "内存警报" natasha@r8
10 fi
11
[root@r8 shell_ex]# crontab -e
*/10 * * * * bash /root/shell_ex/free_mem.sh &> /dev/null
[natasha@r8 ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/natasha": 4 messages 4 new
>N 1 root Thu Aug 18 18:28 21/726 "内存警报"