shell学习之脚本练习3

脚本练习:

1.ip_show.sh 网卡 显示当前的主机名称
2.host_messages.sh 显示当前主机的名称,ip登陆当前主机的用户
hostname: xxxxx
ipaddress: xxxx.xxxx.xxx.xxx
username: root
3…clear_log.sh 执行次脚本后可以清空日志

练习一

1.ip_show.sh 网卡 显示当前的网卡名称

[root@xixi ~]# ifconfig | awk '/inet/{print $2}'  
172.25.254.33
fe80::5054:ff:fe5d:dd27
127.0.0.1
::1
192.168.122.1
[root@xixi ~]# ifconfig | awk '/inet\>/{print $2}'
172.25.254.33                \>表示不需要后面的信息
127.0.0.1
192.168.122.1
[root@xixi ~]# nmcli device status | awk '$2~/ethernet/{print $4}'
eth0						过滤出网卡名

有多个ip的时候,应当查看他的设备的状态,然后将他的ip的名称抓出来。不要直接ifconfig 过滤ip

练习二

2.host_messages.sh 显示当前主机的名称,ip登陆当前主机的用户
hostname: xxxxx
ipaddress: xxxx.xxxx.xxx.xxx
username: root

练习三

  1. . clear_log.sh 执行次脚本后可以清空日志
awk '/./&&!/^#/&&/^\$!/&&$2~/^\/|^\-/{print $2}' /etc/rsyslog.conf | sed  's/-//g'
命令 作用
/./ 要求所在行有 .
!/^#/ 不以#开头
!/^$/ 不以$开头
$2~/^ \ / 第二列必须 / 开头
sed ‘s/-//g’ 去掉-

注意 $具有特殊含义,所以要用\转译一下。此外,过滤的时候一点要选择必须会出现的内容

发布了64 篇原创文章 · 获赞 4 · 访问量 3405

猜你喜欢

转载自blog.csdn.net/ly_qiu/article/details/105652034
今日推荐