2020-03-28第四周作业

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

[root@centos7localdomain ~]# cat /etc/passwd |grep -v /sbin/nologin|cut -d":" -f1|wc -l
8
[root@centos7localdomain ~]# cat /etc/passwd |grep -v /sbin/nologin|cut -d":" -f1
root
sync
shutdown
halt
yang
help
mageia
slackware

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

[root@centos7localdomain etc]# grep `cat /etc/passwd |cut -d":" -f3|sort -n|tail -n1` /etc/passwd|cut -d":" -f1,3,7
nfsnobody:65534:/sbin/nologin
[root@centos7localdomain etc]#cat /etc/passwd|sort -t: -k3 -nr|cut -d: -f1,3,7|head -n1
nfsnobody:65534:/sbin/nologin
sort -t: -k3 -nr    -t: 分割符为: -k3  第三列   -n  数字排序 -r 倒序排
类似于cut -d":" -f3 

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

[root@centos7localdomain etc]# netstat -tun|grep "ESTABLISHED"|tr -s " " :|cut -d: -f6|sort -nr|uniq -c
      2 192.168.8.7
      1 192.168.8.104

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

脚本 如下:
#!/bin/bash
#
#********************************************************************
#Author:                yangwei
#QQ:                    1293472700
#Date:                  2020-03-31
#FileName:             createuser.sh
#email:                 1293472700@qq.com
#Description:          The test script
#Copyright (C):         2020 All rights reserved
#********************************************************************
read -p "please input a username:" username

if id -u $username >/dev/null 2>&1; then
        echo "User exists.The parameters are as follows"                                      
        id $username
        else
                echo "User creating.The parameters are as follows"
                useradd $username
                id $username
                fi

[root@localhost shell]#createuser.sh 
please input a username:yang
User exists.The parameters are as follows
uid=1000(yang) gid=1000(yang) groups=1000(yang),1008(renyue)
[root@localhost shell]#createuser.sh 
please input a username:22
User creating.The parameters are as follows
uid=1007(22) gid=1012(22) groups=1012(22)

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

在root家目录下创建vimrc文件,编写如下内容

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:                yangwei")
        call setline(5,"#QQ:                    1293472700")                                  
        call setline(6,"#Date:                  ".strftime("%Y-%m-%d"))
        call setline(7,"#FileName:             ".expand("%"))
        call setline(8,"#email:                   1293472700@qq.com")
        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
创建一个.sh的脚本,显示如下
[root@localhost shell]#vim test.sh

#!/bin/bash
#
#********************************************************************
#Author:                yangwei
#QQ:                    1293472700
#Date:                  2020-03-31
#FileName:             test.sh
#email:                 1293472700@qq.com
#Description:          The test script
#Copyright (C):         2020 All rights reserved
#********************************************************************
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ▲就业班和全程班的小伙伴看这里:(学习老王视频的作业第7-8节) 1、统计出/etc/passwd文件中其默认sh...
    一心1977阅读 153评论 0 0
  • 1.统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来...
    dongyading阅读 227评论 0 0
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,467评论 0 5
  • 1,统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数并将用户都显示出来 ...
    快乐人丶老朱阅读 136评论 0 0
  • 1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来...
    alone_0cd6阅读 164评论 0 0