标签:判断 脚本 pass com else tle uniq mamicode auth
1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来[root@centos7 scripts]# grep -v ‘/sbin/nologin$‘ /etc/passwd |cut -d: -f1
root
sync
shutdown
halt
kyle
test
[root@centos7 ~]# awk -F: ‘{if($NF!="/sbin/nologin"){print $1}}‘ /etc/passwd
root
sync
shutdown
halt
kyle
test
[root@centos7 scripts]# sed -n ‘/nologin/!p‘ /etc/passwd |sed -r ‘s@^([^:]+).*$@\1@‘
root
sync
shutdown
halt
kyle
test
2、查出用户UID最大值得用户名、UID及shell类型
[root@centos7 scripts]# sort -n -r -t : -k 3 /etc/passwd |head -1 |cut -d: -f1,3,7
test:1006:/bin/bash
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
(ss.log是附加的)
[root@centos7 scripts]# grep -v ‘State‘ ss.log |tr -s ‘ ‘|cut -d‘ ‘ -f5|awk -F: ‘{i=$1;count[i]++}END{for(i in count){print count[i],"\t",i}}‘|sort -nr -k1
44 127.0.0.1
10 113.234.28.244
8 124.64.18.135
6 45.62.217.224
6 210.21.36.228
6 106.38.128.110
5 116.227.232.42
4 59.46.213.114
3 144.76.4.41
2 5.9.70.117
1 91.191.250.142
1 66.249.66.158
1 66.249.66.129
1 61.151.178.165
1 61.149.193.234
1 61.136.204.138
1 5.188.210.39
1 40.77.189.81
1 218.2.216.30
1 202.100.205.245
1 183.226.12.140
1 180.174.58.237
1 120.194.3.98
1 117.136.38.134
1 116.255.176.86
1 112.33.255.134
1 101.200.188.230
1 100.100.110.61
[root@centos7 scripts]# grep -v "State" ss.log|awk ‘{print $5}‘|cut -d: -f1 |sort|uniq -c |sort -nr -k1
44 127.0.0.1
10 113.234.28.244
8 124.64.18.135
6 45.62.217.224
6 210.21.36.228
6 106.38.128.110
5 116.227.232.42
4 59.46.213.114
3 144.76.4.41
2 5.9.70.117
1 91.191.250.142
1 66.249.66.158
1 66.249.66.129
1 61.151.178.165
1 61.149.193.234
1 61.136.204.138
1 5.188.210.39
1 40.77.189.81
1 218.2.216.30
1 202.100.205.245
1 183.226.12.140
1 180.174.58.237
1 120.194.3.98
1 117.136.38.134
1 116.255.176.86
1 112.33.255.134
1 101.200.188.230
1 100.100.110.61
4、编写脚本createuser.sh,实现如下功能:使用一个用户名作为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
[root@centos7 scripts]# vim createuser.sh
[root@centos7 scripts]# cat createuser.sh
#!/bin/bash
#
#**********************************************#
#Author: kyle
#QQ: 1923803824
#Date: 2020-07-05
#FileName: createuser.sh
#Description: The test script
#Copyright (C) 2020 All rights reserved
#**********************************************#
[ $# -gt 1 -o $# -eq 0 ] && { echo "Usge: `basename $0` USERNAME" && exit 1; }
id $1 &> /dev/null
if [ $? -eq 0 ];then
echo "$1 is existed"
else
useradd $1 &>/dev/null && echo "$1 is created! "
echo "$1 userinfo: `grep $1 /etc/passwd | cut -d: -f1,3,7`"
fi
5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,描述等
[root@Centos7 ~]# vim .vimrc
[root@Centos7 ~]# cat .vimrc
set tabstop=4 #指定tab缩进的4个字符
set softtabstop=4 #定义tab所等同的空格长度
set shiftwidth=4 #自动缩进所使用的空白长度;建议以上三项设置数值一样
set expandtab #tab,默认是8个空格,这里与上面三项组合,描述为以4个空格代替tab
set ignorecase #搜索忽略大小写
set cursorline #下划线突出显示当前行
set autoindent #自动缩进
autocmd BufNewFile *.sh exec ":call SetTitle()" #当vim .sh结尾的文件,就会调用SetTitle函数
func SetTitle() #SetTitle函数
if expand("%:e") == ‘sh‘ #判断后缀是否为sh
call setline(1,"#!/bin/bash")
call setline(2,"#")
call setline(3,"#**********************************************#")
call setline(4,"#Author: kyle")
call setline(5,"#QQ: 1923803824")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#Description: The test script")
call setline(9,"#Copyright (C) ".strftime("%Y")." All rights reserved")
call setline(10,"#**********************************************#")
call setline(11,"")
endif
endfunc #函数结束
autocmd BufNewFile * normal G #自动将光标定位到末尾
显示效果如下:
标签:判断 脚本 pass com else tle uniq mamicode auth
原文地址:https://blog.51cto.com/14812296/2508788