第五周作业

1、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

脚本内容:

#!/bin/bash

read -p "请输入用户名:" name

if id $name &> /dev/null;then
    echo "$name 存在"
    else
        useradd $name && echo "$name 用户添加成功:$(id $name)"
        fi

运行结果:

[10:06:27 root@centos8 ~]#bash createuser.sh 
请输入用户名:linux
linux 用户添加成功:uid=1000(linux) gid=1000(linux) groups=1000(linux)
[10:06:36 root@centos8 ~]#bash createuser.sh 
请输入用户名:linux
linux 存在

2、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

[11:29:52 root@centos8 ~]#cat .vimrc
set expandtab
set ignorecase
set cursorline
set nu
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:		kaixin") 
	call setline(5,"#QQ: 			270035801") 
	call setline(6,"#Date: 			".strftime("%Y-%m-%d"))
	call setline(7,"#FileName:		".expand("%"))
	call setline(8,"#URL: 			http://www.magedu.com")
	call setline(9,"#Description:		The test script") 
	call setline(10,"#Copyright (C): 	".strftime("%Y")." All rights reserved")
	call setline(11,"#********************************************************************") 
	call setline(12,"") 
	endif
endfunc
autocmd BufNewFile * normal G

运行一个脚本:

[11:30:01 root@centos8 ~]#vim test.sh

  1 #!/bin/bash
  2 #
  3 #********************************************************************
  4 #Author:                kaixin
  5 #QQ:                    270035801
  6 #Date:                  2021-03-14
  7 #FileName:             test.sh
  8 #URL:                   http://www.magedu.com
  9 #Description:          The test script
 10 #Copyright (C):         2021 All rights reserved
 11 #********************************************************************
 12                                                                                                                            

3、查找/etc目录下大于1M且类型为普通文件的所有文件

命令

[11:37:32 root@centos8 ~]#find /etc -type f  -size +1M

运行

[11:39:09 root@centos8 etc]#find /etc -size +1M -type f
/etc/selinux/targeted/policy/policy.31
/etc/udev/hwdb.bin

4、打包/etc/目录下面所有conf结尾的文件,压缩包名称为当天的时间,并拷贝到/usr/local/src目录备份。

命令

[11:42:34 root@centos8 ~]#find /etc/ -name "*.conf" |xargs tar zcvf /usr/local/src/`date +%F`.tar.gz

运行

[11:45:27 root@centos8 ~]#ll /usr/local/src/
total 44
-rw-r--r--. 1 root root 44673 Mar 14 11:45 2021-03-14.tar.gz

5、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件或目录

命令:

find / ( -nouser -o -nogroup ) -atime +7 ( -type f -o -type d ) -ls

6、查找/etc目录下至少有一类用户没有执行权限的文件

find /etc -not -perm -111
原文地址:https://www.cnblogs.com/qiaokaixin/p/14526449.html