Shell脚本编程和find文件查找

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

[root@localhost ~]# vim createuser.sh 
#!/bin/bash
#
#********************************************************************
#Author:        ye
#QQ:            1787183478
#Date:          2020-12-26
#FileName:      createuser.sh
#URL:           https://www.jianshu.com/u/8d17cce24779
#Description:       The test script
#Copyright (C):     2020 All rights reserved
#********************************************************************
read -p "input the user who you want:" user
if id $user &>/dev/null;
then 
    echo "$user is existed"
else
    useradd $user &>/dev/null && echo "$user is created!"
    echo "$user userinfo: `grep $user /etc/passwd |cut -d: -f1,3,7`"
fi

[root@localhost ~]# chmod +x createuser.sh 
[root@localhost ~]# ./createuser.sh 
input the user who you want:ye
ye is created!
ye userinfo: ye:1000:/bin/bash

[root@localhost ~]# ./createuser.sh 
input the user who you want:ye
ye is existed

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

[root@localhost ~]# vim .vimrc 
set 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:        ye") 
    call setline(5,"#QQ:            1787183478") 
    call setline(6,"#Date:          ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName:      ".expand("%"))
    call setline(8,"#URL:           https://www.jianshu.com/u/8d17cce24779")
    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

[root@localhost ~]# source .vimrc 

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

[root@localhost ~]# find /etc/ -size +1M -a -type f -exec ls -lhS {} +
-r--r--r--. 1 root root 8.8M Dec  9 04:41 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 8.4M Dec  9 04:36 /etc/selinux/targeted/policy/policy.31

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

[root@localhost ~]# find /etc/ -name "*.conf" |xargs tar zcvf /usr/local/src/`date +%F`.tar.gz
[root@localhost ~]# ls /usr/local/src/
2020-12-26.tar.gz

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

[root@localhost ~]# find / -nouser -o -nogroup -a -atime -7|xargs ls -l
find: ‘/proc/1999/task/1999/fd/9’: No such file or directory
find: ‘/proc/1999/task/1999/fdinfo/9’: No such file or directory
find: ‘/proc/1999/fd/8’: No such file or directory
find: ‘/proc/1999/fdinfo/8’: No such file or directory
-rw-r--r--. 1 1002 1002  18 Nov  9  2019 /home/test1/.bash_logout
-rw-r--r--. 1 1002 1002 141 Nov  9  2019 /home/test1/.bash_profile
-rw-r--r--. 1 1002 1002 312 Nov  9  2019 /home/test1/.bashrc
-rw-r--r--. 1 1001 1001  18 Nov  9  2019 /home/test/.bash_logout
-rw-r--r--. 1 1001 1001 141 Nov  9  2019 /home/test/.bash_profile
-rw-r--r--. 1 1001 1001 312 Nov  9  2019 /home/test/.bashrc
-rw-rw----. 1 1001 mail   0 Dec 26 04:16 /var/spool/mail/test
-rw-rw----. 1 1002 mail   0 Dec 26 04:18 /var/spool/mail/test1

/home/test:
total 0

/home/test1:
total 0

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

[root@localhost ~]# find /etc/ -not -perm /111|xargs ls -l
-rw-r--r--. 1 root root          16 Dec  9 04:40 /etc/adjtime
-rw-r--r--. 1 root root        1529 Apr  7  2020 /etc/aliases
-rw-r--r--. 1 root root         541 Nov  9  2019 /etc/anacrontab
-rw-r--r--. 1 root root           1 May 11  2019 /etc/at.deny
......

你可能感兴趣的:(Shell脚本编程和find文件查找)