文本處理與腳本的簡單編寫練習

一、統計出/etc/passwd文件中其默認shell爲非/sbin/nologin的用戶個數,並將用戶都顯示出來shell

[root@centos7 scripts]# grep -v '/sbin/nologin$' /etc/passwd |cut -d: -f1
root
sync
shutdown
halt
kyle
test

[root@centos7 ~]# awk -F: '{if($NF!="/sbin/nologin"){print $1}}' /etc/passwd
root
sync
shutdown
halt
kyle
test

[root@centos7 scripts]# sed -n  '/nologin/!p' /etc/passwd |sed -r 's@^([^:]+).*$@\1@'
root
sync
shutdown
halt
kyle
test

二、查出用戶UID最大值得用戶名、UID及shell類型vim

[root@centos7 scripts]# sort -n -r -t : -k 3 /etc/passwd |head -1 |cut -d: -f1,3,7
test:1006:/bin/bash

三、統計當前鏈接本機的每一個遠程主機IP的鏈接數,並按從大到小排序
(ss.log是附加的)centos

[root@centos7 scripts]# grep -v 'State' ss.log |tr -s ' '|cut -d' ' -f5|awk -F: '{i=$1;count[i]++}END{for(i in count){print count[i],"\t",i}}'|sort -nr -k1
44   127.0.0.1
10   113.234.28.244
8    124.64.18.135
6    45.62.217.224
6    210.21.36.228
6    106.38.128.110
5    116.227.232.42
4    59.46.213.114
3    144.76.4.41
2    5.9.70.117
1    91.191.250.142
1    66.249.66.158
1    66.249.66.129
1    61.151.178.165
1    61.149.193.234
1    61.136.204.138
1    5.188.210.39
1    40.77.189.81
1    218.2.216.30
1    202.100.205.245
1    183.226.12.140
1    180.174.58.237
1    120.194.3.98
1    117.136.38.134
1    116.255.176.86
1    112.33.255.134
1    101.200.188.230
1    100.100.110.61

[root@centos7 scripts]# grep -v "State" ss.log|awk '{print $5}'|cut -d: -f1 |sort|uniq -c |sort -nr -k1
     44 127.0.0.1
     10 113.234.28.244
      8 124.64.18.135
      6 45.62.217.224
      6 210.21.36.228
      6 106.38.128.110
      5 116.227.232.42
      4 59.46.213.114
      3 144.76.4.41
      2 5.9.70.117
      1 91.191.250.142
      1 66.249.66.158
      1 66.249.66.129
      1 61.151.178.165
      1 61.149.193.234
      1 61.136.204.138
      1 5.188.210.39
      1 40.77.189.81
      1 218.2.216.30
      1 202.100.205.245
      1 183.226.12.140
      1 180.174.58.237
      1 120.194.3.98
      1 117.136.38.134
      1 116.255.176.86
      1 112.33.255.134
      1 101.200.188.230
      1 100.100.110.61

四、編寫腳本createuser.sh,實現以下功能:使用一個用戶名做爲參數,若是指定參數的用戶存在,就顯示其存在,不然添加之;顯示添加的用戶的id號等信息bash

[root@centos7 scripts]# vim createuser.sh 
[root@centos7 scripts]# cat createuser.sh
#!/bin/bash
#
#**********************************************#
#Author:        kyle
#QQ:            1923803824
#Date:          2020-07-05
#FileName:        createuser.sh
#Description:    The test script
#Copyright (C)    2020 All rights reserved
#**********************************************#
[ $# -gt 1 -o $# -eq 0 ] && { echo "Usge: `basename $0` USERNAME" && exit 1; }
id $1 &> /dev/null
if [ $? -eq 0 ];then
    echo "$1 is existed"
else
    useradd $1 &>/dev/null && echo "$1 is created! "
    echo "$1 userinfo: `grep $1 /etc/passwd | cut -d: -f1,3,7`"
fi

五、編寫生成腳本基本格式的腳本,包括做者,聯繫方式,版本,描述等ide

[root@Centos7 ~]# vim .vimrc
[root@Centos7 ~]# cat .vimrc
set tabstop=4                    #指定tab縮進的4個字符
set softtabstop=4                #定義tab所等同的空格長度      
set shiftwidth=4                 #自動縮進所使用的空白長度;建議以上三項設置數值同樣
set expandtab                    #tab,默認是8個空格,這裏與上面三項組合,描述爲以4個空格代替tab
set ignorecase                   #搜索忽略大小寫
set cursorline                   #下劃線突出顯示當前行
set autoindent                   #自動縮進
autocmd BufNewFile *.sh exec ":call SetTitle()"   #當vim .sh結尾的文件,就會調用SetTitle函數
func SetTitle()                  #SetTitle函數
    if expand("%:e") == 'sh'     #判斷後綴是否爲sh
    call setline(1,"#!/bin/bash")
    call setline(2,"#")
    call setline(3,"#**********************************************#")
    call setline(4,"#Author:        kyle")
    call setline(5,"#QQ:            1923803824")
    call setline(6,"#Date:          ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName:        ".expand("%"))
    call setline(8,"#Description:    The test script")
    call setline(9,"#Copyright (C)    ".strftime("%Y")." All rights reserved")
    call setline(10,"#**********************************************#")
    call setline(11,"")
    endif
endfunc                           #函數結束
autocmd BufNewFile * normal G     #自動將光標定位到末尾

顯示效果以下:
文本處理與腳本的簡單編寫練習函數

相關文章
相關標籤/搜索