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
#********************************************************************