課堂作業
1.找出ifconfig命令結果中的IP地址
[root@localhost ~]# ifconfig |head -2|grep "inet" |tr " " ":"|cut -d: -f13
2、找出df中磁盤利用率的數
[root@localhost ~]# df |tail -5|tr ' ' ':' |tr -s :|cut -d: -f5 |tr "%" " "
簡便方法: [root@localhost ~]# df |tail -5 |tr -s " "|cut -d" " -f5 |tr -d %
注意:tr -s 是去掉相同的 tr -d 是刪除 tr -cd刪除取反的(忘記了,再記)
3、1.找出ifconfig命令結果中本機的所有ipv4地址
[root@localhost ~]# ifconfig |tr -cs '[0-9].' '\n' |sort -ut '.' -k3n
4、查處分區空間使用率的最大百分比值
[root@localhost ~]# df |tr -s " "|tr " " ":"|cut -d: -f5|tr -d %|sort -n|tail -1
5、查處用戶uid最大值的用戶名、uid及shell類型
[root@localhost ~]# getent passwd|cut -d: -f1,3,7|sort -t : -k2 -n|tail -1
6、查處/tmp的權限,以數字方式顯示
[root@localhost ~]# stat /tmp|grep "Uid"|cut -d: -f2 |tr -cs [0-9] ' '
方法二:[root@localhost ~]# stat /tmp|grep "Uid"|tr -cs [0-9] ' '|cut -d" " -f2
7、顯示/proc/meminfo文件中以大小s開頭的行(兩種方法)
[root@localhost ~]# grep -i '^s.*' /proc/meminfo [root@localhost ~]# grep '^[Ss].*' /proc/meminfo
8、顯示/etc/passwd文件中不宜/bin/bash結尾的行
[root@localhost ~]# grep -v "\(/bin/bash\)$" /etc/passwd
9、顯示用戶rpc默認的shell程序
[root@localhost ~]# getent passwd|grep '^rpc\>' |cut -d: -f7
10、找出/etc/passwd中的兩位或三位數
[root@localhost ~]# grep "\<[1-9]\{2,3\}\>" /etc/passwd
11、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
[root@localhost ~]# grep "^[[:space:]]\+[^[:space:]].*" /etc/grub.conf
12、找出‘netstat -tan’命令的結果中以‘LISTEN’后跟任意多個空白字符結尾的行
[root@localhost ~]# netstat -tan|grep "LISTEN[[:space:]]*$"
13、添加用戶bash,testbash,basher,nologin(shell為/sbin/nologin)。然后找出/etc/passwd文件中用戶名同shell名相同的行
[root@localhost ~]# grep "^\<\(.*\)\>.*\1$" /etc/passwd
14、顯示三個用戶root,mage,wang的uid和默認shell
[root@localhost ~]# egrep "^(mage|wang|root)\>" /etc/passwd |cut -d: -f1,7
15、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
[root@localhost ~]# egrep "^[[:alpha:]_]*\(\)" /etc/rc.d/init.d/functions
16、使用egrep取出/etc/c.d/init.d/functions中其基名
[root@localhost ~]# echo " /etc/rc.d/init.d/functions" |egrep -o "[^/]+/?$"
17、使用egrep取出/etc/ec.d/init.d/functions的目錄名
[root@localhost ~]# echo " /etc/rc.d/init.d/functions" |egrep -o "(/.*/)"
18、統計以root身份登錄的每個進程主機ip地址的登錄次數
[root@localhost ~]# last |egrep -o "^root\>.*([[:digit:]]\.){3}[[:digit:]]" |tr -s '' |cut -d ' ' -f3|sort |uniq -c
19、利用擴展正則表達式分別表示0-9,10-99,100-199,200-249,250-255,顯示ifconfig命令結果中所有ipv4地址
[root@localhost ~]# ifconfig |egrep -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
20、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示
[root@localhost ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' "\n" |sort|uniq -c|sort -nr
21、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/" 取目錄名
[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" |sed 's@[^/]\+/\?$@@'
22、正則表達式表示身份證(后續幾題暫時不會)
原創文章,作者:15152188070,如若轉載,請注明出處:http://www.www58058.com/30287