练习题
1. 统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来。
[root@localhost ~]# cat /etc/passwd | grep -v "/sbin/nologin" | cut -d: -f1
root
sync
shutdown
halt
2.查出用户UID最大值的用户名,UID及shell类型
[root@localhost ~]# cat /etc/passwd | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7
polkitd:999:/sbin/nologin
3. 统计当前连接本机的每个远程主机IP领导连接数,并从大到小排列
[root@localhost ~]# netstat -t | grep ':ssh'|tr -s ' '|cut -d ' ' -f5|cut -d: -f1 | uniq -c|sort -r
4 10.0.0.1
4. 编写脚本createuser.sh,实现如下功能:使用一个用户名作为参数,如果指定参数的用户存在,就显示其存在,否则添加之,显示添加的用户id号等信息。
read -p "hello, please add a user:" USERNAME
if `id $USERNAME &>/dev/null`
then echo "username already used!"
else `useradd $USERNAME &>/dev/null`
echo "successful added!"
echo `grep $USERNAME /etc/passwd`
fi
执行结果
[root@localhost ~]# bash createuser.sh
hello, please add a user:abc
username already used!
[root@localhost ~]# bash createuser.sh
hello, please add a user:def
successful added!
def:x:1001:1001::/home/def:/bin/bash
5. 编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
et 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:------------------------------------------------- ABYSS")
call setline(5,"#QQ:------------------------------------------------------359463614")
call setline(6,"#Date:--------------------------".strftime("%Y-%m-%d"))
call setline(7,"#FileName:----------------------------------- ".expand("%"))
call setline(8,"#Description:----------------------------------- hello hello hello")
call setline(9,"#Copyright (C-----".strftime("%Y")." All rights reserved")
call setline(10,"#********************************************************************")
call setline(11,"")
endif
endfunc
autocmd BufNewFile * normal G