题1
找出shell不是/sbin/nologin的用户,并计算个数
[root@use10 ~]# grep -v '/sbin/nologin' /etc/passwd | cut -d ':' -f 1 | cat -n
1 root
2 sync
3 shutdown
4 halt
5 liuzhe
6 mageia
7 tom
8 slackware
题2
找出UID最大的账号,并显示账号名、UID、shell类型
[root@use10 ~]# sort -t ':' -k 3 -nr /etc/passwd | cut -d ':' -f 1,3,7 | head -n 1
slackware:2002:/bin/tcsh
题3
统计正在远程连接主机的IP数量
[root@use10 ~]# w -h | awk '{print $3}' | sort | uniq -c | sort -t ' ' -k 1 -nr
4 192.168.75.43
3 192.168.75.1
1 192.168.75.41
题4
通过脚本创建用户,要求判断用户是否存在再创建
[root@use09 ~]# cat createuser.sh
#!/bin/bash
read -p "Enter one user name: " UserName
id ${UserName} &> /dev/null
if [ $? -eq 0 ];then
echo "${UserName} is exsits."
else
useradd ${UserName} &> /dev/null && id ${UserName}
fi
[root@use09 ~]# bash createuser.sh
Enter one user name: tom
uid=1001(tom) gid=1001(tom) groups=1001(tom)
[root@use09 ~]# bash createuser.sh
Enter one user name: tom
tom is exsits.
题5
自定义shell脚本开头注释
[root@use09 ~]# cat .vimrc
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
call setline(1,"\###################################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: mage")
call append(line(".")+2, "\# mail: mage@123.com")
call append(line(".")+3, "\# Created Time: ".strftime("%c"))
call append(line(".")+4, "\########################################################")
call append(line(".")+5, "\#!/bin/bash")
call append(line(".")+6, "")
endfunc
效果:
###################################################################
# File Name: a.sh
# Author: mage
# mail: mage@123.com
# Created Time: Sat 09 May 2020 04:26:22 PM CST
########################################################
#!/bin/bash