第三周作业
1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
[22:15:59 root@centos7 etc]#grep -v "/sbin/nologin\(" /etc/passwd | cut -d: -f1 |wc -l**
12
**[22:16:07 root@centos7 etc]#grep -v "/sbin/nologin\)" /etc/passwd | cut -d: -f1 |nl
1 root
2 sync
3 shutdown
4 halt
5 zj
6 tester
7 tomcat
8 mysql
9 gentoo
10 user1
11 user3
12 user2
这题我的理解是: 同时要显示用户个数和显示用户 , 如果用WC -l 最后只显示总的用户数,需要另外执行一条语句 cut -d: -f1 把用户显示出来。 我的逻辑是先用cut -d: f1显示用户数,再用nl命令对用户进行 显示行号, 用户总数也就一目了然。
2、查出用户UID最大值的用户名、UID及shell类型
[22:37:32 root@centos7 etc]#cat /etc/passwd | cut -d: -f1,3,7 |sort -t: -k2 -nr |head -n1
nfsnobody:65534:/sbin/nologin
通过文件/etc/passwd 查看各个用户UID 及Shell 类型, 然后提取 用户名 和 UID 及shell 栏,按UID大小进行从大到小排序,最后用head命令显示第一行。
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[22:47:27 root@centos7 etc]#ss -nt | tail -n+2 | tr -s " " : | cut -d: -f6|sort | uniq -c | sort -nr
2 10.0.0.1
ss -nt 不解析服务名称并仅显示TCP套接字。 从第二行开始显示,压缩空格并用:分隔,提取远程主机IP,进行排序,显示每个IP的重复数目,从大到小排列。
4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
第一种 :
[23:06:43 root@centos7 tmp]#cat disk.sh
#!/bin/bash
MaxDiskUsed=df -h|tail -n +2|tr -s " " "%"|cut -d% -f5|sort -nr|head -n1
echo $MaxDiskUsed
第二种:
[23:23:44 root@centos7 tmp]#cat disk.sh
#!/bin/bash
#MaxDiskUsed=`df -h|tail -n +2|tr -s " " "%"|cut -d% -f5,6|sort -nr|head -n1`
#echo $MaxDiskUsed
MaxDiskUsed=df -h | grep -Eo "\b[0-9]+%.*" | sort -nr | head -n1
echo $MaxDiskUsed
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
!/bin/bash
RED="\E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
\(GREEN----------------------Host systeminfo--------------------\)END
echo -e "HOSTNAME: \(RED`hostname`\)END"
echo -e "IPADDR: \(RED` ifconfig ens33|grep -Eo '([0-9]{1,3}\.){3}[0-9]
{1,3}' |head -n1`\)END"
echo -e "OSVERSION: \(RED`cat /etc/redhat-release`\)END"
echo -e "KERNEL: \(RED`uname -r`\)END"
echo -e "CPU: \(RED`lscpu|grep 'Model name'|tr -s ' '|cut -d : -f2`\)END"
echo -e "MEMORY: \(RED`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`\)END"
echo -e "DISK: \(RED`lsblk |grep '^sd' |tr -s ' ' |cut -d " " -f4`\)END"
\(GREEN---------------------------------------------------------\)END
6、20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)

浙公网安备 33010602011771号