1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
grep -c -v ".*/sbin/nologin$" /etc/passwd
grep -v ".*/sbin/nologin$ /etc/passwd" | cut -d: -f1
2、查出用户UID最大值的用户名、UID及shell类型
sort -t: -k3 -nr /etc/passwd | cut -d: -f 1,3,7 | head -1
3、统计当前连接本机的每个远程主机IP的连接数,并按照从大到小排序
netstat -nt | grep [[:digit:]] | tr -s " " : | cut -d: -f6 | sort | uniq -c | sort -nr
4、编写脚本createuser.sh, 实现如下功能: 使用一个用户名作为参数,如果指定参数的用户存在,就显示其存在,否则添加之,显示添加的用户的id号等信息
脚本内容:
1 #!/bin/bash
2 #
3 #*****************************************************
4 # Author: Peter Liang
5 # Date: 03-07-2020
6 # FileName: createuser.sh
7 # Description: Test Script
8 # Copyright (C) 2020 All right reserved
9 #*****************************************************
10 COLOUR1="\e[1;31m"
11 COLOUR2="\e[1;32m"
12 COLOUR3="\e[1;34m"
13 COLOUREND="\e[0m"
14
15
16 [ $# -eq 0 ] && { echo -e "$COLOUR1 Usage:$COLOUREND$COLOUR3 `basename $0`$COLOUREND$COLOUR1 Missing Username.$COLOUREND"; exit 10; }
17
18 id $1 &>/dev/null && { echo -e "$COLOUR1 User:$COLOUREND$COLOUR3 $1$COLOUREND$COLOUR1 already exist.$COLOUREND"; exit 20; }
19
20 useradd $1 &>/dev/null && echo -e "$COLOUR2 User:$COLOUREND$COLOUR3 $1$COLOUREND$COLOUR2 has created. ID related info:$COLOUREND$COLOUR3 `id $1`$COLOUREND" || { echo -e "$COLOUR1 Error occurred!$COLOUREND"; exit 30; }
5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
vim ~/.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: Peter Liang")
call setline(5,"#Date: ".strftime("%d-%m-%Y"))
call setline(6,"#FileName: ".expand("%"))
call setline(7,"#Description: Test Script")
call setline(8,"#Copyright (C) ".strftime("%Y"). " All rights reserved")
call setline(9,"#********************************************************")
call setline(10,"")
endif
endfunc
autocmd BufNewFile * normal G
vim test.sh 创建新shell脚本