马哥Linux

马哥Linux第四周

2020-03-09  本文已影响0人  Liang_JC

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

[root@Centos7 ~]# grep -v "/sbin/nologin" /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
LiangJC:x:1000:1000:LiangJC:/home/LiangJC:/bin/bash

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

[root@Centos7 ~]# sort -nr -t ":" -k 3 /etc/passwd | cut -d: -f1,3,7 | head -1
nfsnobody:65534:/sbin/nologin

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

[root@Centos7 ~]# ss -nt | grep "^ESTAB" | tr -s " " : | cut -d: -f6 | sort | uniq -c | sort -nr | head
      1 192.168.139.1

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

vim createuser.sh
#!/bin/bash
if [ -z "$1" ];then
    echo "Usage: $0 username [username2 username3 ...]"            
    exit 2
else
    while [ "$1" ];do
        id $1 &> /dev/null
        if [ $? -eq 0 ];then
            echo "$1 is already exists"
        else
            useradd $1
            echo "$1 is created , UID:`id $1 | cut -d= -f2 | grep -Eo "[0-9]+"`"
        fi
        shift
    done
fi

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

vim ~/.vimrc
set tabstop=4
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:           LiangJC")
    call setline(5,"# QQ:               184116857")
    call setline(6,"# Date:             ".strftime("%Y-%m-%d"))
    call setline(7,"# FileName:        ".expand("%"))
    call setline(8,"# URL:              https://www.jianshu.com/nb/42151888")
    call setline(9,"# Description:     The test script")
    call setline(10,"# Copyright (C):   ".strftime("%Y")." All rights reserved")
    call setline(11,"#********************************************************************")
    call setline(12,"")
    endif
endfunc
autocmd BufNewFile * normal G

#测试
vim test.sh
#!/bin/bash
#
#********************************************************************
# Author:           LiangJC
# QQ:               184116857
# Date:             2020-03-09
# FileName:        test.sh
# URL:              https://www.jianshu.com/nb/42151888
# Description:     The test script
# Copyright (C):    2020 All rights reserved
#********************************************************************

上一篇下一篇

猜你喜欢

热点阅读