N51期第五次作业

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

[root@centos8mini ~]# cat createuser.sh 
#!/bin/bash
set -u
echo -e "\e[1;33m检查用户名是否存在\e[0m"
echo -e "\e[1;33m说明:当用户存在,则提示用户已存在;当用户不存在,则自动创建用户.\e[0m"
read -p "请输入用户名: " ACCOUNT
USERNAME=`echo $ACCOUNT | tr 'A-Z' 'a-z'`
if [[ $USERNAME =~ ^[[:alnum:]]+$ ]];then
`id $USERNAME &> /dev/null`
    if [ $? -eq 0 ];then
        echo "用户已存在."
    elif [ $? -ne 0 ];then
        echo "用户不存在,下面开始自动创建用户."
        read -p "请输入初始密码位数: " NUMBER
            if [[ $NUMBER =~ ^[[:digit:]]+$ ]] && [ "$NUMBER" != 0 ];then
                `useradd $USERNAME`
                PASSWORD=`cat /dev/urandom | tr -dc '[[:alnum:]]' | { head -c $NUMBER;echo; }`
                echo `echo $PASSWORD | passwd --stdin $USERNAME`
            else
                echo "您的输入有误,请重新输入正确的数字."
                exit 3
            fi
        USER_UID=`grep "$USERNAME" /etc/passwd | sed -n -E 's@^(.*):x:([[:digit:]]+):([[:digit:]]+):(.*):(.*):(.*)$@\2@p'`
        USER_HOME_DIR=`grep "$USERNAME" /etc/passwd | sed -n -E 's@^(.*):x:([[:digit:]]+):([[:digit:]]+):(.*):(.*):(.*)$@\5@p'`
        USER_SHELL=`grep "$USERNAME" /etc/passwd | sed -n -E 's@^(.*):x:([[:digit:]]+):([[:digit:]]+):(.*):(.*):(.*)$@\6@p'`

        echo -e "\e[1;35m************用户$USERNAME************\e[0m"
        echo -e "\e[1;36m用户名: $USERNAME\n密码:   $PASSWORD\nUID:    ${USER_UID}\n家目录: ${USER_HOME_DIR}\nshell:  ${USER_SHELL}\e[0m"
    else
        exit 2
    fi
else
    echo "您的输入有误,请重新输入字母与数字的组合或字母或数字."
    exit 1
fi

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

[root@centos8mini ~]# 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:             xyj")
    call setline(5,"#QQ:                 90*****31")
    call setline(6,"#Date:               ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName:           ".expand("%"))
    call setline(8,"#URL:                https://blog.51cto.com/3958535")
    call setline(9,"#Description:        The is test script")
    call setline(10,"#Copyright ?:        ".strftime("%Y")." All rights reserved")
    call setline(11,"#********************************************************************")
    call setline(12,"")                                                                                 
    endif
endfunc
autocmd BufNewFile * normal G

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

[root@centos8mini ~]#  find /etc \( -size +1M -type f \) -ls

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

[root@centos8mini ~]# find /etc -name "*.conf" | xargs tar -jcvf /usr/local/src/`date +%F`.tar.bz2 

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

[root@centos8mini ~]# find / \( -nouser -o -nogroup \) -a -atime -7

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

[root@centos8mini ~]# find /etc ! -perm -111 -ls

猜你喜欢

转载自blog.51cto.com/3958535/2575404