第四周、文本处理工具及shell基础

1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
[root@iZbp19axymtpqhar62dycvZ data]# cat /etc/passwd |egrep -v '.*/sbin/nologin$' |cut -d: -f1
root
sync
shutdown
halt
wang
[root@iZbp19axymtpqhar62dycvZ data]# cat /etc/passwd |egrep -v '.*/sbin/nologin$' |cut -d: -f1 |wc -l
5

2、查出用户UID最大值的用户、UID及shell类型
[root@iZbp19axymtpqhar62dycvZ data]# cat passwd |sort -nr -t: -k3 |head -1 |cut -d: -f1,3,7
wang:1000:/bin/bash
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@iZbp19axymtpqhar62dycvZ data]# netstat |grep tcp |egrep -o '\<([0-9]+\.){3}[0-9]+\>' |sort -r |uniq -c
      2 118.114.15.34
      1 100.100.30.25
4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
[root@iZbp19axymtpqhar62dycvZ data]# cat createuser.sh 
#!/bin/bash
CLO="\e[1;$[RANDOM%7+31]m"
CLOEND="\e[0m"
[[ "$#" -eq 0 ]] && { echo -e "Usage: $CLO`basename $0` user$CLOEND" ;exit 10; }
id $1 &>/dev/null && { echo -e "$CLOuser $1 is exist$CLOEDN";id $1;exit 20; }
useradd $1 || { echo -e "$CLOError$CLOEND" ;exit 30; }
id $1
[root@iZbp19axymtpqhar62dycvZ data]# /data/createuser.sh
Usage: createuser.sh user
[root@iZbp19axymtpqhar62dycvZ data]# /data/createuser.sh li
uid=1001(li) gid=1001(li) groups=1001(li)
[root@iZbp19axymtpqhar62dycvZ data]# /data/createuser.sh li
 li is exist
uid=1001(li) gid=1001(li) groups=1001(li)

5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
[root@iZbp19axymtpqhar62dycvZ data]# cat vimrc.sh 
cat > ~/.vimrc <<EOF
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:		wangzhipeng") 
	call setline(5,"#QQ: 		        632917996") 
	call setline(6,"#Date: 			".strftime("%Y-%m-%d"))
	call setline(7,"#FileName:		".expand("%"))
	call setline(8,"#URL: 			http://www.magedu.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
EOF

猜你喜欢

转载自blog.csdn.net/wauzy/article/details/105893536
今日推荐