第四周做业

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

[23:20:02 root@centos7_6 data]# getent passwd |grep -v nologin  |wc -l
6
[23:20:17 root@centos7_6 data]# 
[23:20:32 root@centos7_6 data]# 
[23:20:33 root@centos7_6 data]# getent passwd |grep -v nologin |cut -d: -f1
root
sync
shutdown
halt
jiangjingqun
jiang
[23:20:50 root@centos7_6 data]# 

思路:
1)统计非/sbin/nologin的用户个数,想到用grep -v参数,反向选择;而后配合wc -l参数来统计个数;
2)要显示用户,使用cut命令,取出第一列centos

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

[23:29:23 root@centos7_6 data]# getent passwd |sort -t: -k3 -n |cut -d: -f1,3,7  |tail -1
nfsnobody:65534:/sbin/nologin
[23:29:28 root@centos7_6 data]# 

思路:
1)要查有UID、用户名及shell,确定是passwd文件;
2)要找UID最大值,就先排个序,因而就用sort,以:号分列,-t:,UID在第3列,-k3,按数字排序-n
3)用cut按需分别把表明:用户名、UID、shell类型的一、三、7列找出来
4)UID最大值的会排到最后,因而tail -1;ide

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

[14:14:19 root@localhost ~]#netstat -nt |awk '{print $5}' |grep [0-9] |sort -t: -k5 -nr 
192.169.1.11:53277
192.169.1.102:40738
192.169.1.102:30700

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

#!/bin/bash
#########################################
#author:jiangjingqun
#Date:2020-07-01
#FileName:createuser
#Description:建立用户,建立前判断是否存在
########################################
set -e
set -u
if [ $# -eq 0 ];then
   echo "Usage:Please Input user name"
  exit
elif id $1 &>/dev/nul
    echo "user $1 exist!"
    exit
else
    echo "start creat user $1" 
    useradd $1
    echo "user $1 create successd!"
    id $1
fi

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

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:                       jiangjingqun")
         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(12,"")
         endif
endfunc
autocmd BufNewFile * normal G
相关文章
相关标签/搜索