The fifth assignment of the N51 period

1. Write the script createuser.sh to achieve the following functions: use a user name as a parameter, if the user with the specified parameter exists, display its existence, otherwise add it; display the id number of the added user and other information

[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. Write the script of the basic format of the generated script, including the author, contact information, version, time, description, etc.

[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. Find all files in the /etc directory that are larger than 1M and whose types are ordinary files

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

4. Pack all the files ending with conf under the /etc/ directory, the compressed package name is the time of the day, and copy to the /usr/local/src directory for backup.

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

5. Find files or directories that have no owner or group on the current system and have been visited in the last week

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

6. Find at least one type of file in the /etc directory that the user does not have execution permissions

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

Guess you like

Origin blog.51cto.com/3958535/2575404