練習
1、統(tǒng)計出/etc/passwd文件中其默認shell為非/sbin/nologin的用戶個數(shù),并將用戶都顯示出來
cat /etc/passwd | grep -v '\/sbin\/nologin$' |wc -l && cat /etc/passwd | grep -v '\/sbin\/nologin$'| cut -d':' -f 1,7

1.png
2、查出用戶UID最大值的用戶名、UID及shell類型
cat /etc/passwd | sort -nr -t':' -k3 | head -1 | cut -d':' -f 1,3,7 | tr ':' ' '

2.png
3、統(tǒng)計當前連接本機的每個遠程主機IP的連接數(shù),并按從大到小排序
ss -an | tr -s ' ' | cut -d' ' -f6 | sed -rn '/[0-9]+\:[0-9]+$/p' | cut -d':' -f1 | sort | uniq -c | sort -rn

3.png
4、編寫腳本 createuser.sh,實現(xiàn)如下功能:使用一個用戶名做為參數(shù),如果 指定參數(shù)的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等 信息
#!/bin/bash
read -p "please input a username:" username
if id -u $username > /dev/null 2>&1; then
echo “${username} is alread exit!”
else
useradd ${username} &&
id ${username}
fi

4.png
5、編寫生成腳本基本格式的腳本,包括作者,聯(lián)系方式,版本,時間,描述等
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand ("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "#Author:ZHS")
call setline(3, "#Email:****@163.com")
call setline(4, "#Time:".strftime("%F %T"))
call setline(5, "#Name:".expand("%"))
call setline(6, "#Version:V1.0")
call setline(7, "#Description:This is a test description script.")
call setline(8, "")
endif
endfunc

5.png