8月5號 練習+作業

1,找出ifconfig 命令結果中本機的所有IPv4 地址

[root@localhost ~]# ifconfig |tr -cs '[:digit:].' '\n' |sort -t. -k3 |tail -5
255.0.0.0
255.255.0.0
127.0.0.1
10.10.10.10
10.10.255.255

2,查出分區空間使用率的最大百分比值

[root@localhost ~]# df |tr -s ' ' % |cut -d% -f5 |sort -n |tail -1
21

3 查出用戶UID 最大值的用戶名、UID shell 類型

[root@localhost ~]# cat /etc/passwd |cut -d: -f1,3,7 |sort -t: -k2 -n |tail -1
nfsnobody:65534:/sbin/nologin

4 ,查出/tmp 的權限,以數字方式顯示

[root@localhost ~]# stat /tmp |head -4 |tail -1|tr -s '(/' '::' |cut -d: -f3
1777

[root@localhost ~]# stat /tmp |head -4 |tail -1 |tr -cs '[0-9]' '\n' |head -2 |tail -1
1777

5 統計當前連接本機的每個遠程主機IP 的連接數,并按從大到小排序

[root@localhost ~]# netstat -nt |tr -s ' ' ':' |cut -d: -f6 |tr -d '[:alpha:]' |sort -r |uniq -c
      2 10.10.10.1
      2

6,顯示/proc/meminfo 文件中以大小s 開頭的行;( 要求:使用兩種方式)

[root@localhost ~]# grep -i '^s' /proc/meminfo
[root@localhost ~]# grep '^[sS]' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:               240 kB
Slab:              58904 kB
SReclaimable:      32592 kB
SUnreclaim:        26312 kB

7,顯示/etc/passwd 文件中不以/bin/bash 結尾的行

[root@localhost ~]# grep -v '/bin/bash$' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

8,顯示用戶rpc 默認的shell 程序

[root@localhost ~]# grep '^rpc\>' /etc/passwd |cut -d: -f7
/sbin/nologin

9,找出/etc/passwd 中的兩位或三位數

[root@localhost ~]# grep '\<[0-9]\{2,3\}' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin

10、顯示/etc/grub2.cfg 文件中,至少以一個空白字符開頭的且后面存非空白字符的行

[root@localhost ~]# grep '^[[:space:]]\+[^[:space:]]*' /etc/grub2.cfg
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"

11、找出“netstat -tan” 命令 的結果 中以 以‘LISTEN’ 后跟任意多個空白字符結尾的行

[root@localhost ~]# netstat -ant |grep 'LISTEN[[:space:]]*$'
tcp        0      0 0.0.0.0:57160               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN

12、添加用戶bash 、testbash 、basher 以及nologin( shell為 為/sbin/nologin), 而后找出/etc/passwd 文件中用戶名同shell的行

[root@localhost ~]# grep '^\([^:]\+\>\).*/\1$' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:507:514::/home/bash:/bin/bash
nologin:x:510:517::/home/nologin:/sbin/nologin

13、顯示三個用戶root 、mage 、wang UID 和默認shell

[root@localhost ~]# grep -E '^(root|mage|wang)\>' /etc/passwd |cut -d: -f1,3,7
root:0:/bin/bash
mage:511:/bin/bash
wang:512:/bin/bash

14、找出/etc/rc.d/init.d/functions 文件中行首為某單詞(包括下劃線) 后面跟一個小括號的行

[root@localhost ~]# grep -E '^[_[:alpha:]]+\(\)' /etc/rc.d/init.d/functions 
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {

15、使用egrep 取出/etc/rc.d/init.d/functions 中其基名

[root@localhost ~]# echo "etc/rc.d/init.d/functions" |egrep -o '[^/]+/?$'
functions

16、使用egrep 取出上面路徑的目錄名

[root@localhost ~]# echo "/etc/rc.d/init.d/functions" |egrep -o '^/.*/\b'
/etc/rc.d/init.d/
[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" |egrep -o '^/.*/\<'
/etc/rc.d/init.d/

17、統計以root 身份登錄的每個遠程主機IP 地址的登錄次數

[root@localhost ~]# last |grep '^root\>' |tr -s ' ' |cut -d' ' -f3 |grep '^[[:digit:]]' |sort |uniq -c
     20 10.10.10.1
      2 10.1.69.10
      1 10.1.69.200
      1 10.1.69.70

18、利用擴展正則表達式分別表示0-9 、10-99 、100-199200-249 、250-255

0-9:     [0-9]
10-99:    [1-9][0-9]
100-199:  1[0-9][0-9]
200-249:  2[0-4][0-9]
250-255:  25[0-5]

9、顯示ifconfig 命令結果中所有IPv4 地址

[root@localhost ~]# ifconfig |grep '\<inet\>' |tr -s ' ' ':' |cut -d: -f4
10.10.10.10
127.0.0.1

20、取本機ip地址

[root@localhost ~]# ifconfig |grep '\<inet\>' |tr -s ' ' ':' |cut -d: -f4
10.10.10.10
10.1.253.35
127.0.0.1

21、取各分區利用率的數值

[root@localhost ~]# df |grep '^/dev/sd' |tr -s ' ' ':' |cut -d: -f1,5 |tr -d %
/dev/sda2:2
/dev/sda5:1
/dev/sda1:53

22、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示

[root@localhost ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' '\n' |sort |uniq -c | sort -n -r
     67 pid
     55 if
     54 file
     51 echo
     47 then
     45 return
     45 fi
     33 n

23、/etc/rc.d/init.d/functions/" 取目錄名

[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" |egrep -o '^/.*/+\b'
/etc/rc.d/init.d/

24、正則表達式表示身份證號

[1-9][0-9]{5}:前6位數字

(19[0-9][0-9]|200[0-9]|201[0-6]:4位代表年份

(0[0-9]|1[0-2]):2位代表月份

([0-2][0-9]|3[0-1]:2位代表日期

[0-9]{3}([0-9]|X):后4位隨機數(包含出現尾數為X的情況)

[root@localhost ~]# echo "ID:44162219810327471X" |egrep -o '\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>'
44162219810327471X
[root@localhost ~]# echo "ID:441622198103274710" |egrep -o '\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>'
441622198103274710

25、正則表達式表示手機號

[root@localhost ~]# echo "phone number 18925619340" |egrep -o "\<1[3|5|7|8][0-9]{9}\>"
18925619340

26、正則表達式表示郵箱

[root@localhost ~]# cat mail |egrep '^[[:alnum:]]+@[[:alnum:]]+.com\>'
wangwu@163.com
123456@qq.com
li23si@sohu.com

27、正則表達式表示QQ號

  QQ號5-10位:

[1-9]:第一個數字不能為0
[0-9]{4,9}:后面跟4-9位數字
[root@localhost ~]# echo "My QQ is 9087654321" |egrep -o "\<[1-9][0-9]{4,9}\>"
9087654321
[root@localhost ~]# echo "My QQ is 54321" |egrep -o "\<[1-9][0-9]{4,9}\>"
54321

 


原創文章,作者:pingsky,如若轉載,請注明出處:http://www.www58058.com/30149

(0)
pingskypingsky
上一篇 2016-08-07
下一篇 2016-08-07

相關推薦

  • 可伸縮的邏輯卷

    什么是邏輯卷? 邏輯卷簡稱LVM, LVM是Linux環境中對磁盤分區進行管理的一種機制,是建立在硬盤和分區之上、文件系統之下的一個邏輯層,可提高磁盤分區管理的靈活性.   為什么要使用邏輯卷? 邏輯卷相比于一般的磁盤分區, 具有更高的靈活性??呻S時伸縮空間的大小.   構建邏輯分區圖:   邏輯卷概念: PV(物理卷): 是在…

    Linux干貨 2016-09-01
  • Javascript 裝載和執行

    一兩個月前在淘寶內網里看到一個優化Javascript代碼的競賽,發現有不少的人對Javascript的執行和裝載的基礎并不懂,所以,從那天起我就想寫一篇文章,但一直耽擱了。自上篇《瀏覽器渲染原理簡介》,正好也可以承前啟后。 首先,我想說一下Javascript的裝載和執行。通常來說,瀏覽器對于Javascript的運行有兩大特性:1)載入后馬上執行,2)執…

    Linux干貨 2016-07-10
  • Shell腳本編程—數組、字符串處理

    數組 變量:存儲單個元素的內存空間 數組:存儲多個元素的連續的內存空間     數組名:整個數組只有一個名字;     數組索引:編號從0開始;          [數組名索引]  &nbs…

    Linux干貨 2016-08-30
  • nginx及I/O介紹原理

    上課筆記

    2018-03-12
  • Shell腳本編程基礎中() (()) [ ] [[ ]] 的使用

    Shell腳本編程基礎中() (()) [ ] [[ ]] 的使用 () 生成子進程,括號內的命令將會在子進程中運行,父進程不能夠讀取在子進程中創建的變量 例: 新建個腳本文件,寫入 則執行結果為 $()相當于 ` `? ,返回括號內命令執行結果 (( )) 用作四則運算和邏輯運算,并且支持多個表達式 例: 當 (( )) 加$,則是將獲得表達式值,賦值給左…

    2017-11-26
欧美性久久久久