四、Linux 文件截取与shell脚本

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

[root@localhost ~]#cat /etc/passwd |cut -d: -f1,7|grep -v sbin/nologin|cat -n
     1	root:/bin/bash
     2	sync:/bin/sync
     3	shutdown:/sbin/shutdown
     4	halt:/sbin/halt
     5	wensijia:/bin/bash
     6	rpc:/bin/bash
     7	mage:/bin/bash
     8	wang:/bin/bash
     9	gentoo:/bin/csh
    10	nginx:/bin/bash
    11	varnish:/bin/bash
    12	docker:/bin/bash
    13	zabbix:/bin/bash
    14	tomcat:/bin/bash
    15	git:/bin/bash
    16	abd:/bin/bash
    17	abc:/bin/bash
    18	mageia:/bin/bash
    19	slackware:/bin/tcsh

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

[root@localhost ~]#cat /etc/passwd|cut -d: -f1,3,7|sort -t: -k2 -nr|head -1
slackware:2002:/bin/tcsh

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

[root@localhost ~]#netstat -tun|grep ESTAB|tr -s " " :|cut -d: -f6|sort -nr|uniq -c
      1 10.0.0.110

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

[root@localhost data]# bash createuesr.sh 
Please input UserID: linux
linux is exist
[root@localhost data]# bash createuesr.sh 
Please input UserID: abd
abd is create,passwd is user
[root@localhost data]# id abd
uid=1020(abd) gid=1024(abd) groups=1024(abd)
[root@localhost data]# cat createuesr.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: ???
#Date: 2020-08-12
#FileName: createuesr.sh
#********************************************************************
set -e
set -u

read -p "Please input UserID: " user
if id $user &> /dev/null;then
	echo "$user is exist"
	exit 3
else
	useradd $user > /dev/null;
	echo "user" | passwd --stdin $user > /dev/null && echo "$user is create,passwd is user"
fi

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

[root@localhost ~]#cat .vimrc 
set ignorecase
set cursorline
set ts=4
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,"#Author: Kevin.Wen")
 call setline(4,"#Revision: 1.0")
 call setline(5,"#QQ: ???")
 call setline(6,"#Date: ".strftime("%Y-%m-%d"))
 call setline(7,"#FileName: ".expand("%")) 
 call setline(8,"#********************************************************************")
 call setline(9,"")
endif
endfunc
autocmd BufNewFile * normal G
posted @ 2020-08-31 11:20  人生值得  阅读(456)  评论(0)    收藏  举报