2020-03-03 第四周作业

▲就业班和全程班的小伙伴看这里:(学习老王视频的作业第7-8节)

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

[root@centos7 ~]#getent passwd|grep -v "/sbin/nologin$" | sed -nr 's@^([^:]+):.*$@\1@p' | cat -n

    1 root

    2 sync

    3 shutdown

    4 halt

    5 chen

2、查出用户UID最大值的用户名、UID及shell类型

[root@centos7 ~]#getent passwd|sort -nr -t":" -k3|head -1|cut -d":" -f1,3,7

nfsnobody:65534:/sbin/nologin

3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@centos7 ~]#netstat -net|egrep [0-9]|tr -s " " :|cut -d: -f6|sort |uniq -c|sort -nr

4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等 信息

[root@centos7 /data/scripts]#vim useradd.sh

#!/bin/bash

set -u

set -e

read -p "Please input you want add username:" NAME

if [ -z $NAME ] ; then                 

        echo  "Usage:Need USERNAME"

        exit 10

    elif id $NAME &> /dev/null; then

        echo "User $NAME is exist"

        exit 20

    elif useradd $NAME &>/dev/null; then

        echo $NAME is created ; echo abc.123 | passwd --stdin $NAME &> /dev/null ; passwd -e $NAME &>/dev/null

        echo $NAME uid is `getent passwd|grep "$NAME"|sed -nr 's/.*:([0-9]+):.*/\1/p'`                                     

    else

        echo " Error! "

fi

:wq

[root@centos7 /data/scripts]#useradd.sh

Please input you want add username:rick

rick is created

rick uid is 1001

5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

[root@centos7 ~]#vim .vimrc
autocmd BufNewFile *.sh exec ":call SetTitle()"

func SetTitle()

if expand("%:e")=='sh'

call setline(1,"#!/bin/bash")

set tabstop=4

set softtabstop=4

set shiftwidth=4

set expandtab

set ignorecase

set cursorline

set autoindent

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:                chenyb")

call setline(5,"#Date:                  ".strftime("%Y-%m-%d"))

call setline(6,"#FileName:              ".expand("%"))

call setline(7,"#Description:            The test script")

call setline(8,"#Copyright (C):          ".strftime("%Y")."All rights reserved")

call setline(9,"#*****************************************************************")

call setline(10,"")

endif

endfunc

autocmd BufNewFile * normal G

:wq

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容