0804練習與作業
練習
1 、找出ifconfig 命令結果中本機的所有IPv4 地址
答: 使用cut將電腦的IP地址提取出來。
經過觀察,這些信息極為不規則,沒有明顯的分隔符。這時們需要首先提取出IP地址所在的行,然后去定義一個合適的分隔符,再利用cut進行提取即可。(思路:化繁為簡,化不規則為規則,這樣才能更好的套用我們的命令模型)
2 、查出分區空間使用率的最大百分比值
答:# df|tr -s " "|sort -t" " -k 5 -nr |head -2|tail -1|cut -d" " -f5
3 、查出用戶UID 最大值的用戶名、UID 及shell 類型
答:# getent passwd|sort -t: -k 3 -n|cut -d: -f1,3,7|tail -1
4 、查出/tmp 的權限,以數字方式顯示
答:#stat /tmp|head -n 4|tail -n 1|cut -d"(" -f2|cut -d"/" -f1
5 、統計當前連接本機的每個遠程主機IP 的連接數,并按從大到小排序。
答:利用iptables -F關閉防火墻
netstat -nt顯示連接主機的ip
$ netstat -nt|tr -s " "|cut -d" " -f5|cut -d: -f1|sort -rn|uniq -c
6 、顯示/proc/meminfo 文件中以大小S開頭的行;( 要求:使用兩種方式)
答:(1)grep ^S /proc/meminfo v
(2)grep S.* /proc/meminfo
7 、顯示/etc/passwd 文件中不以/bin/bash 結尾的行
答:]# grep -v '/bin/bash$' /etc/passwd
8、顯示用戶rpc 默認的shell 程序
v答: 找這樣的用戶rpc,則必須先錨定為一行的行首,其次長度一定,那么再進行行尾的錨定。
9、找出/etc/passwd 中的兩位或三位數.
答:grep “\<[[:digit:]]\{2,3\}\>” /etc/passwd 只是數字所以記得要錨定。
10、顯示/etc/grub2.cfg 文件中,至少以一個空白字符開頭的
且后面存非空白字符的行
答:]# grep "^[[:space:]]\+[^[:space:]].*" /etc/grub2.cfg
11、找出"netstat -tan" 命令的結果中以'LISTEN' 后跟0 、1或多個空白字符結尾的行。
答:]# netstat -tan|grep "\<LISTEN[[:space:]]*$"
12、添加用戶bash 、testbash 、basher 以及nologin( 其shell為/sbin/nologin), 而后找出/etc/passwd 文件中用戶名同shell名的行。
答:先按要求添加用戶
然后,輸入# getent passwd | grep "\(\b[[:alnum:]]\{1,\}\):.*\1$" /etc/passwd
13、顯示當前系統root 、mage 或wang 用戶的UID 和默認shell。
答:# grep -E "^root\b|^wang\b" /etc/passwd |cut -d: -f3,7
14、找出/etc/rc.d/init.d/functions 文件中行首為某單詞(包
括下劃線) 后面跟一個小括號的行。
答:grep “^([[:alpha:]_])+\(\)” /etc/rc.d/init.d/functions
15、使用egrep 取出/etc/rc.d/init.d/functions 中其基名。
答:# ls -d /etc/rc.d/init.d/functions |egrep -o "[^/]+$"
16、使用egrep 取出上面路徑的目錄名
答:# echo "/etc/rc.d/init.d/functions" |egrep -o "^.*/"
17、統計以root 身份登錄的每個遠程主機IP 地址的登錄次數
答:# who |tr -s " " ":"|grep "^root\b"|cut -d "(" -f2|cut -d ")" -f1|uniq -c
18、利用擴展正則表達式分別表示0-9 、10-99 、100-199、 200-249 、250-255。
答: 0-9 grep -E "^[[:digit:]]\b"
10-99 grep -E "^[1-9][0-9]\b" /testdir/a.txt
100-199 grep -E "^1[0-9][0-9]\b" /testdir/a.txt
200-249 grep -E "^2[0-4][0-9]\b" /testdir/a.txt
250-255 grep -E "^25[0-5]\b" /testdir/a.txt
19、顯示ifconfig 命令結果中所有IPv4
答: 以centos為例
輸入:ifconfig |grep -E "(Bcast)"|tr -s " " ":"|cut -d: -f6
原創文章,作者:178babyhanggege,如若轉載,請注明出處:http://www.www58058.com/30845