1:将登陆最多你的主机的ip用命令截取出来并显示出登陆的次数
脚本内容:
#!/bin/bash
last -i | grep 0.0.0.0 -v | grep wtmp -v | cut -d " " -f 14 | uniq -c | sort -nr -t " " -k 5 | head -n 1
如图所示:
2.ping任意一个地址,能ping通提示ip is up否则就是ip is down,若是在脚本后面没跟ip地址,则报错
#!/bin/bash
[ -z "$1" ] && {
echo "Error:Please input ipaddress following ping.sh"
exit
}
ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down
完善修改输出字体的颜色
3.如果/的挂载使用率大于等于30%,在日志里面报警,并且一分钟之后再次检测
脚本内容如下:
实验结果:
查看的定时任务的内容
4:判断一个文件的类型
#!/bin/bash
[ -z "$1" ] && {
echo "Error:Please input a file name following test4.sh"
exit
}
[ -e "$1" ] && echo $1 is exits || echo $1 is not exits
[ -f "$1" ] && echo $1 is a common file
[ -L "$1" ] && echo $1 is a link file
[ -S "$1" ] && echo $1 is a socket file
[ -b "$1" ] && echo $1 is a block file
[ -d "$1" ] && echo $1 is a directory
[ -c "$1" ] && echo $1 ia a char file
实验总体显示结果如图:
5:字符小写转换大写
代码如下:
总体结果如图:
6.截取出交互式登陆过系统的用户
#!/bin/bash
grep -E "sh$" /etc/passwd | cut -d : -f 1
总体实验结果如下:
7.显示本机的主机ip
#/bin/bash
ifconfig eth0 | grep "\<inet " | cut -c 14-28
总体实验结果如下:
8.修改httpd的默认端口为自己想要的数字
#!/bin/bash
[ -z "$1" ] && {
echo "Error:Please input number following test7.sh"
}
sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd
netstat -antlupe | grep httpd
实验结果总体如图所示:
9.建立一个文件userfile,里面写入用户名,写一个脚本,自动建立这个userfile文件里写的用户(用户不存在的情况下),如果用户存在,则提示用户已经存在。
10.输出1~30
代码如下: