1、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
[root@centos6 ~]#cat createuser.sh
#!/bin/bash
#使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
#定义变量
read -p "请输入一个用户名:" USERNAME
#判断用户名是否存在
if `id $USERNAME &> /dev/null`;then
# 若存在,则显示其存在,输出ID等信息
echo "用户存在,用户的ID信息为:`id $USERNAME`"
else
# 若不存在,则添加用户,设置密码为随机8位,下次登录时提示修改密码,同时显示ID等信息
PASSWORD=`cat /dev/urandom |tr -cd [:alpha:] |head -c8`
`useradd $USERNAME &> /dev/null`
`echo "$PASSWORD" | passwd --stdin $USERNAME &> /dev/null`
echo "用户名:$USERNAME 密码: $PASSWORD" >> user.txt
`chage -d 0 $USERNAME`
echo "用户已添加,用户的ID信息为:`id $USERNAME` 密码为:$PASSWORD"
fi
#删除变量
unset name passwd
[root@centos7 ~]# source createuser.sh
请输入一个用户名:duwenshuo
用户存在,用户的ID信息为:uid=1001(duwenshuo) gid=1001(duwenshuo) groups=1001(duwenshuo)
[root@centos7 ~]# source createuser.sh
请输入一个用户名:wang
用户已添加,用户的ID信息为:uid=1002(wang) gid=1002(wang) groups=1002(wang) 密码为:ajcMfkwx
2、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
[root@centos7 ~]# cat ~/.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: dws")
call setline(5,"#QQ: 2421297991")
call setline(6,"#email: 2421297991@qq.com")
call setline(7,"#version: 1.0")
call setline(8,"#date: ".strftime("%Y-%m-%d"))
call setline(9,"#description: test")
call setline(10,"#*************************************")
endif
endfunc
[root@centos7 ~]# vim test.sh
#!/bin/bash
#
#*************************************
#author: dws
#QQ: 2421297991
#email: 2421297991@qq.com
#version: 1.0
#date: 2021-04-11
#description: test
#*************************************
3、查找/etc目录下大于1M且类型为普通文件的所有文件
[root@centos7 ~]# find /etc -size +1M -type f
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
/etc/udev/hwdb.bin
4、打包/etc/目录下面所有conf结尾的文件,压缩包名称为当天的时间,并拷贝到/usr/local/src目录备份。
[root@centos7 ~]# find /etc/ -name "*.conf" |xargs tar zcvf /usr/local/src/`date +%F`.tar.gz
[root@centos7 ~]# ls /usr/local/src/
2021-04-11.tar.gz
[root@centos7 ~]# tar tvf /usr/local/src/2021-04-11.tar.gz
-rw-r--r-- root/root 60 2021-04-11 20:38 etc/resolv.conf
-rw-r----- root/root 191 2017-10-12 23:53 etc/libaudit.conf
-rw-r--r-- root/root 662 2013-07-31 19:46 etc/logrotate.conf
-rw-r--r-- root/root 842 2016-11-06 04:29 etc/GeoIP.conf
-rw-r--r-- root/root 184 2018-04-11 14:01 etc/prelink.conf.d/nss-softokn-prelink.conf
-rw-r--r-- root/root 57 2017-08-02 20:47 etc/prelink.conf.d/fipscheck.conf
-rw-r--r-- root/root 220 2017-10-21 16:12 etc/prelink.conf.d/grub2.conf
-rw-r--r-- root/root 970 2018-04-13 20:58 etc/yum.conf
.......
5、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件或目录
[root@centos7 ~]# find / \( -nouser -o -nogroup \) -a -atime -7
find: ‘/proc/1633/task/1633/fd/6’: No such file or directory
find: ‘/proc/1633/task/1633/fdinfo/6’: No such file or directory
find: ‘/proc/1633/fd/5’: No such file or directory
find: ‘/proc/1633/fdinfo/5’: No such file or directory
6、查找/etc目录下至少有一类用户没有执行权限的文件
[root@centos7 ~]# find /etc ! -perm -111 |wc -l
2131