Linux文本處理和腳本實踐

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

grep -v /sbin/nologin /etc/passwd | wc -l
grep -v /sbin/nologin /etc/passwd | cut -d: -f1

二、查出用戶UID最大值的用戶名、UID及shell類型bash

sort -t: -n  -k3  /etc/passwd | cut -d: -f 1,3,7 | tail -1 
cut -d: -f1,3,7 /etc/passwd |sort -t: -k2 -n | tail -1

三、統計當前鏈接本機的每一個遠程主機IP的鏈接數,並按從大到小排序ide

ss -nt | awk -F'[: ]+' '/ESTAB/{ip[$(NF-2)]++}END{for(i in ip){print i,ip[i]}}' | sort -k2 -nr

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

#!/bin/bash
if [ $# -ne 1 ]; then
    echo "Usage: $0 USERNAME "
    exit
fi

if [ $UID -ne 0 ]; then
    echo "Permission denied,please run this script with root"
    exit
fi

grep -w $1 /etc/passwd > /dev/null

if [ $? -ne 0 ];then
        useradd $1
        echo "Add new user $1 succeeful"
        echo "The infomation of new user is `id $1`"
else
        echo "User $1 is already exist"
fi

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

autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
    if expand("%:e") == 'sh'
    call setline(1,"#!/bin/bash")
    call setline(2,"#")
    call setline(3,"#********************************************************************")
    call setline(4,"#Author:         Example")
    call setline(5,"#Mail:           example@qq.com")
    call setline(6,"#Version:        1.0")
    call setline(7,"#Date:           ".strftime("%Y-%m-%d"))
    call setline(8,"#FileName:       ".expand("%"))
    call setline(9,"#Description:    It's is test script.")
    call setline(10,"#Copyright (C):  ".strftime("%Y")." All rights reserved")
    call setline(11,"#********************************************************************")
    call setline(12,"")
    endif
endfunc
相關文章
相關標籤/搜索