第四周作业—N42-虚怀若谷

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

[root@centos7 ~]# grep -v "/sbin/nologin" /etc/passwd|cut -d: -f1
root
sync
shutdown
halt
hovin
[root@centos7 ~]# grep -v "/sbin/nologin" /etc/passwd|cut -d: -f1|wc -l
5

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

[root@centos7 ~]# cut -d : -f 1,3,7 /etc/passwd|sort -t : -k 2 -nr|head -n 1
nfsnobody:65534:/sbin/nologin

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

[root@centos7 ~]# w -h    #查询连接数
root     pts/0    192.168.214.1    09:08    5.00s  0.12s  0.00s w -h
root     pts/1    192.168.214.1    09:22    3:01   0.03s  0.00s less -s
root     pts/2    172.16.236.130   09:34    4:00   0.02s  0.02s -bash
[root@centos7 ~]# w -h|tr -s " "      #压缩空格方便进一步处理  
root pts/0 192.168.214.1 09:08 3.00s 0.12s 0.00s w -h
root pts/1 192.168.214.1 09:22 3:31 0.02s 0.02s -bash
root pts/2 172.16.236.130 09:34 4:30 0.02s 0.02s -bash
[root@centos7 ~]# w -h|tr -s " "|cut -d " " -f 3    #取出IP地址
192.168.214.1
192.168.214.1
172.16.236.130
[root@centos7 ~]# w -h|tr -s " "|cut -d " " -f 3|sort   #排序
192.168.214.1
192.168.214.1
172.16.236.130
[root@centos7 ~]# w -h|tr -s " "|cut -d " " -f 3|sort|uniq -c     #去重并计数
      1 172.16.236.130
      2 192.168.214.1
[root@centos7 ~]# w -h|tr -s " "|cut -d " " -f 3|sort|uniq -c|sort -nr  #按计数逆序
      2 192.168.214.1
      1 172.16.236.130

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

[root@centos7 scripts]# cat createuser.sh 
#!/bin/bash

#read -p "please input a username: " USER
USER=$1

if [ -z "$USER" ];then 
    echo "Usage: `basename $0` username"
    exit
fi

if id $USER &> /dev/null ;then
    echo "user: $USER already exists"
    exit
else 
    useradd $USER
    echo "user: $USER create success"
    id $USER
fi
[root@centos7 scripts]# bash createuser.sh 
Usage: createuser.sh username
[root@centos7 scripts]# bash createuser.sh hovin
user: hovin already exists
[root@centos7 scripts]# bash createuser.sh alice
user: alice create success
uid=1001(alice) gid=1001(alice) groups=1001(alice)

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

  在.vimrc中定义,对创建的以.sh结尾的脚本应用,相关代码如下: 

[root@centos7 ~]# cat .vimrc 
set tabstop=4
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:                hovin")
    call setline(5,"QQ:                    405001597")
    call setline(6,"Date:                ".strftime("%Y-%m-%d"))
    call setline(7,"FileName:            ".expand("%"))
    call setline(8,"Description:        The test script")
    call setline(9,"Copyright (C):        ".strftime("%Y")." ALL rights reserved)
    call setline(10,"#**************************************************")
    call setline(11,"")
    endif
endfunc
autocmd BufNewFile * normal G

[root@centos7 ~]# vim test.sh   #创建脚本

#/bin/bash

#

#***************************************************

Author:             hovin

QQ:                 405001597

Date:               2019-11-23

FileName:           test.sh

Description:        The test script

Copyright (C):      2019 ALL rights reserved

#**************************************************

猜你喜欢

转载自www.cnblogs.com/hovin/p/11887041.html