第五周作业

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

 id $1 &> /dev/null && echo "$1 is exist"
 id $1 &> /dev/null || (useradd $1 && id $1)

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

  1 set nu
  2 set paste
  3 set ts=4
  4 
  5 autocmd BufNewFile *.sh exec ":call SetTitle()"
  6 func SetTitle()
  7         if expand("%:e") == 'sh'
  8         call setline(1, "#!/bin/bash")
  9         call setline(2, "#")
 10         call setline(3, "#************************************************************************************")
 11         call setline(4, "#Author:               owen")
 12         call setline(5, "#QQ:                   xxxxxx")
 13         call setline(6, "#Date:                 ".strftime("%Y-%m-%d"))
 14         call setline(7, "#FileName:                 ".expand("%"))
 15         call setline(8, "#URL:                  https://google.com")
 16         call setline(9, "#Descrition:           The test script")
 17         call setline(10, "#Copyright (C):       ".strftime("%Y")." All rights reserved")
 18         call setline(11, "#************************************************************************************")
 19         call setline(12, "")
 20         endif
 21 endfunc
 22 autocmd BufNewFile * normal G

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

find /etc -size +1M -type f

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

tar -cf /usr/local/src/backup`date +%F` /etc/*.conf

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

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

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

find / ! -perm /555

注意事项

  • echo 命令是一个子进程,配合管道符的时候要加括号
echo 1 2 | (read x y; echo $x $y)
  • 使用bash和使用source执行命令的区别, bash开启子进程执行,source在当前进程执行,这也是为什么配置文件要使用.或者source运行来使之立刻生效, 一般使用bash运行脚本,使用source配置环境

  • 用于判断ip地址的

[[ "$ip" =~ ^([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]
posted @ 2021-02-01 14:07  无名小卒戊  阅读(24)  评论(1)    收藏  举报