linux学习笔记(四)

练习

1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

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、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

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,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的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、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容