文章目录
前言
今天七夕说一说最近在闲暇之余写得几个案例。
一、利用你学过的的知识点(awk、grep.sed),想办法根据要求截取字符串字符串:http://www.51xit.top/root/123.html
- ###文件插入字符串
- vi 123.txt
- http://www.51xit.top/root/123.html
要求:
- 1、取出 www.51xit.top/root/123.html
- 2、取出123.html
- 3、取出http://www.51xit.top/root/123.html
- 4、取出http:
- 5、取出http://
- 6、取出root/123.html
- 7、取出123
1.1、取出 www.51xit.top/root/123.html
方法一:
[root@localhost ~]# awk -F "//" '{print $2}' 123.txt
wwww.51xit.top/root/123.html
方法二:
[root@localhost ~]# cat 123.txt |awk -F '//' '{print $2}'
wwww.51xit.top/root/123.html
方法三:
[root@localhost ~]# cat 123.txt |grep -o "wwww.*"
wwww.51xit.top/root/123.html
1.2、取出123.html
方法一:
[root@localhost ~]# cat 123.txt |awk -F '/' '{print $5}'
123.html
方法二:
[root@localhost ~]# cat 123.txt |grep -o "[0-9]*\.html"
123.html
1.3、取出http://www.51xit.top/root/123.html
[root@localhost ~]# cat 123.txt |grep -o "http.*root"
http://wwww.51xit.top/root
1.4、取出http:
方法一
[root@localhost ~]# awk -F "//" '{print $1}' 123.txt
http:
方法二:
[root@localhost ~]# cat 123.txt |sed 's/\/\/wwww.*//'
http:
5、取出http://
方法一:
[root@localhost ~]# cat 123.txt |awk -F "w" '{print $1}'
http://
方法二:
[root@localhost ~]# cat 123.txt | sed 's/www.*//'
http://
1.6、取出root/123.html
方法一:
[root@localhost ~]# cat 123.txt | awk -F 'www.51xit.top/' '{print $2}'
root/123.html
方法二:
[root@localhost ~]# cat 123.txt |sed 's/^.*top\///'
root/123.html
1.7、取出123
方法一:
[root@localhost ~]# cat 123.txt |grep -o '[0-9]\{3\}'
123
方法二:
[root@localhost ~]# cat 123.txt |awk -F '/' '{print $5}'|awk -F'.' '{print $1}'
123
二、目前项目上线,有这样的需求,为了不让上线的服务器,不与线网的IP地址相冲突,写出一个shell脚本,把192.168.100.0/24网段在线的IP地址和不在线的IP地址列出来并且保存到文档中。
shell分析
- 1、24网段公有254个IP地址,从192.168.100.1到192.168.1.254,需要以个for循环进行
遍历 - 2、看一个IP地址是否在线,主要用ping命令来进行测试
vim ceshi.sh
#!/bin/bash
ips="192.168.100."
for i in `seq 1 254`
do
ping -c 2 $ips$i > /dev/null 2> /dev/null
if [ $? -eq 0 ]
then
echo "$ips$i is online" >> /opt/ipup.txt
else
echo "$ips$i is not online" >> /opt/ipdown.txt
fi
done
chmod +x ceshi.sh
三、shell要求每隔5分钟检查指定的用户是否登录系统,用户名从命令行中输入如果指定的用户已登录,则显示相关的信息。
shell分析
- 1、每隔5分钟,可用计划任务,也可以做死循环
- 2、根据题目要求,用户名要求输入,那就意味着和用户交互,
如果每5分钟都去交互一次,太麻烦了,所以死循环比较合适,只需要
交互一次
shell解析
- 1、在while死循环之前,先让用户输入用户名,如果在循环里面,那每隔5分钟,都要输入一次用户名,这样不合理
- 2、who命令可以查看当前登录系统的用户名列表用grep-w保证匹配的用户更加精确
vim ceshi2.sh
#!/bin/bash
read -p "Please input the username:" user
while :
do
if who | grep -qw $user
then
echo $user login.
else
echo $user not login.
fi
sleep 300
done
未完待续。。。。。。。