練習(xí)1:
統(tǒng)計出/etc/passwd文件中其默認shell為非/sbin/nologin的用戶個數(shù),并將用戶都顯示出來
root@ubuntu1804:~# grep '[^/nologin]' /etc/passwd| grep -onE '^[^:]+'
1:root
2:daemon
3:bin
4:sys
5:sync
6:games
7:man
練習(xí)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
練習(xí)3:
統(tǒng)計當前連接本機的每個遠程主機IP的連接數(shù)魄缚,并按從大到小排序
ss -tn|tail -`echo $(ss -tn | wc -l)-1|bc` |tr -s ' ' ':'|cut -d: -f6|sort -rn|uniq -c
練習(xí)4:
編寫腳本disk.sh宝与,顯示當前硬盤分區(qū)中空間利用率最大的值
#!/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%
練習(xí)5
編寫腳本 systeminfo.sh,顯示當前主機系統(tǒng)信息冶匹,包括:主機名习劫,IPv4地址,操作系統(tǒng)版本嚼隘,內(nèi)核版本诽里,CPU型號,內(nèi)存大小飞蛹,硬盤大小
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 "內(nèi)核版本為: `uname -r`"
echo "硬盤名稱以及大小為: `fdisk -l |head -1|tr ' ' '%'|cut -d'%' -f 2,3,4|tr '%' ' '`"