练习1:
统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
root@ubuntu1804:~# grep '[^/nologin]' /etc/passwd| grep -onE '^[^:]+'
1:root
2:daemon
3:bin
4:sys
5:sync
6:games
7:man
练习2:
查出用户UID最大值的用户名、UID及shell类型
[16:19:16 root@Centos8 ~]#getent passwd `cat /etc/passwd|cut -d: -f3|sort -rn|head -1`|cut -d: -f1,7
nobody:/sbin/nologin
练习3:
统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
ss -tn|tail -`echo $(ss -tn | wc -l)-1|bc` |tr -s ' ' ':'|cut -d: -f6|sort -rn|uniq -c
练习4:
编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
disk1=$(df -h |grep '/dev/'|tail -`echo $(df -h |grep '/dev/'|wc -l)-1 |bc`|tr -s ' '|cut -d' ' -f5|sort -rn|head -1)
disk2=$(df -h|grep "$disk1"|tr -s ' '|cut -d' ' -f1)
echo "使用率最高的硬盘为:$disk2 占用率为:$disk1"
[17:03:45 root@Centos8 ~]#bash disk1.sh
使用率最高的硬盘为:/dev/nvme0n1p1 占用率为:100%
练习5
编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
echo "主机名: `hostname`"
echo "IP地址为: $(ifconfig|head -2|grep -Eo "inet[[:space:]][^ ]+"|grep -Eo "(([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-8]|25[0-5])\.){,3}([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])")"
echo "操作版本为: `cat /etc/redhat-release `"
echo "内核版本为: `uname -r`"
echo "硬盘名称以及大小为: `fdisk -l |head -1|tr ' ' '%'|cut -d'%' -f 2,3,4|tr '%' ' '`"