課堂練習
1.找出ifconfig 命令結果中本機的所有IPv4 地址
ifconfig|tr -cs '[0-9].' '\n'|sort -ut '.' -k3
2.查出分區空間使用率的最大百分比值
df|tr -s ' '|cut -d" " -f5|sort -n|tail -1
3.查出用戶UID 最大值的用戶名、UID 及shell 類型
getent passwd |sort -n -t: -k3|cut -d: -f1,3,7|tail -1
4.查出/tmp 的權限,以數字方式顯示
如果無特殊權限位,將rwx—>421,權限位每三個求和
str=`ll -d /tmp|cut -d " " -f1|cut -c2-10|tr rwx- 4210`
echo $(echo ${str:0:1}+${str:1:1}+${str:2:1}|bc) $(echo ${str:3:1}+${str:4:1}+${str:5:1}|bc) $(echo ${str:6:1}+${str:7:1}+${str:8:1}|bc)|tr -d [:space:]
輸出755
stat /tmp|sed -n 4p|cut -d: -f2|cut -d" " -f2|grep [[:digit:]]|cut -d/ -f1|cut- c3-6
stat /bin/cat |head -4|tail -1|tr " " "\n"|head -n2|tail -1|tr -cd '[:digit:]'
stat /tmp –c %a
5.統計當前連接本機的每個遠程主機IP 的連接數,并按從大到小排序
netstat -nt|tr -s " "| cut -d" " -f5|grep "[0-9]"|cut -d: -f1|uniq -c|sort
6.顯示/proc/meminfo 文件中以大小s 開頭的行;( 要求:使用兩種方式)
cat /proc/meminfo |grep -i ^s.* 或
cat /proc/meminfo |grep -e ^s.* -e ^S.* 或
grep '^[sS].*' /proc/meminfo
7.顯示/etc/passwd 文件中不以/bin/bash 結尾的行
cat /etc/passwd|grep -v /bin/bash$
8.顯示用戶rpc 默認的shell 程序
getent passwd rpc|cut -d: -f7 或
grep '^rpc\>' /etc/passwd|cut -d: -f7
9.找出/etc/passwd 中的兩位或三位數
cat /etc/passwd|grep -w '\b[1-9][0-9]\{1,2\}\b'
10.顯示/etc/grub2.cfg 文件中,至少以一個空白字符開頭的且后面存非空白字符的行
cat /etc/grub2.cfg |grep "^[[:space:]]\+.*[^[:space:]]"
11.找出"netstat -tan" 命令的結果中以'LISTEN' 后跟0 、1或多個空白字符結尾的行
netstat -tan|grep 'LISTEN[[:space:]]*$'
12.添加用戶bash 、testbash 、basher 以及nologin( 其shell為 為/sbin/nologin),
而后找出/etc/passwd 文件中用戶名同shell名
useradd bash; useradd testbash; useradd basher
useradd –s /sbin/nologin nologin
cat /etc/passwd|grep '^\<\(.*\)\>.*/\1$'
13.顯示當前系統root 、mage 或wang 用戶的UID 和默認shell
grep -E "^(root|wang|mage)\b" /etc/passwd|cut -d: -f3,7
14.找出/etc/rc.d/init.d/functions 文件中行首為某單詞(包括下劃線) 后面跟一個小括號的行
cat /etc/rc.d/init.d/functions |egrep "^[[:alnum:]_]+\(\)" —最后括號轉義
15.使用egrep 取出/etc/rc.d/init.d/functions 中其基名
echo "/etc/rc.d/init.d/functions" |egrep -o "[^/]+/?$"
注意對比下面
16.使用egrep 取出上面路徑的目錄名
echo "/etc/rc.d/init.d/functions" |egrep -o "(/.*/)"
17.統計以root 身份登錄的每個遠程主機IP 地址的登錄次數
last|grep -E -o "^root\>.*([[:digit:]]+\.)[[:digit:]]+"|tr -s " "|cut -d" " -f1,3|sort|uniq –c
18.利用擴展正則表達式分別表示0-9 、10-99 、100-199、200-249 、250-255
grep -E "[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]" file
19.顯示ifconfig 命令結果中所有IPv4 地址
ifconfig|grep -E -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])"
作業
1.取本機ip地址
ifconfig | egrep -o '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
2.取各分區利用率的數值
3.統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示
cat /etc/init.d/functions |tr -c '[:alpha:]' '\n'|sort|uniq –c
4./etc/rc.d/init.d/functions 取目錄名
/etc/rc.d/init.d/functions/取目錄名
5.正則表達式表示身份證號
cat identify |egrep -o "[[:digit:]]{18}|[[:digit:]]{17}X|[[:digit:]]{15}"
6.正則表達式表示手機號
cat phone |egrep -o "[1-9][0-9]{10}"
7.正則表達式表示郵箱
cat mail |egrep -o "[[:alnum:]_]+@[[:alnum:]]+.[[:alnum:]]+"
8.正則表達式表示QQ號
grep "[1-9][0-9]\{4,9\}$"
原創文章,作者:victorycommander,如若轉載,請注明出處:http://www.www58058.com/30430