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
相关文章
相关标签/搜索