1.編寫腳本 createuser.sh,實現如下功能:使用一個用戶名做為參數,如果 指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息
[root@centos7 ~]#
[root@centos7 ~]# cd /data/script/
[root@centos7 script]# ls
createuser.sh
[root@centos7 script]# cat createuser.sh
#!/bin/bash
#使用一個用戶名做為參數,如果指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息
#日期2020-12-22
#/data/scipts/createuser.sh
read -p "請輸入一個用戶名:" USERNAME
if `id $USERNAME &> /dev/null`;then
echo "用戶存在,用戶的ID信息為:`id $USERNAME`"
else
PASSWORD=`cat /dev/urandom | tr -cd A-Za-z0-9| head -c6`
`useradd $USERNAME &> /dev/null`
`echo "$PASSWORD" | passwd --stdin $USERNAME &> /dev/null`
echo "用戶名:$USERNAME 密碼:$PASSWORD" >> /data/user.txt
`chage -d 0 $USERNAME`
echo "用戶已添加,用戶的ID信息為:`id $USERNAME` 密碼為:$PASSWORD"
fi
[root@centos7 script]# ll /home
total 0
drwx------ 2 mageia mageia 62 Dec 10 19:08 linux
drwx------ 2 1005 distro 62 Dec 10 19:07 mandriva
drwx------ 2 slackware distro 62 Dec 10 19:14 slackware
drwx------ 2 user1 user1 83 Dec 10 19:51 user1
drwx------ 2 user2 user2 62 Dec 10 19:35 user2
drwx------ 2 user3 user3 62 Dec 10 19:35 user3
drwx------ 2 wang wang 62 Dec 22 16:23 wang
drwx------. 2 wukui wukui 83 Nov 25 10:16 wukui
[root@centos7 script]# bash createuser.sh
請輸入一個用戶名:wang
用戶存在,用戶的ID信息為:uid=2007(wang) gid=2007(wang) groups=2007(wang)
[root@centos7 script]# bash createuser.sh
請輸入一個用戶名:zhang
用戶已添加,用戶的ID信息為:uid=2008(zhang) gid=2008(zhang) groups=2008(zhang) 密碼為:6VZxTs
[root@centos7 script]# bash createuser.sh
請輸入一個用戶名:li
用戶已添加,用戶的ID信息為:uid=2009(li) gid=2009(li) groups=2009(li) 密碼為:lmdgH0
[root@centos7 script]#
2.編寫生成腳本基本格式的腳本,包括作者,聯系方式,版本,時間,描述等
# 編輯~/.vimrc配置文件
[root@centos7 ~]# cat ~/.vimrc
set tabstop=4
[root@centos7 ~]# vim ~/.vimrc
[root@centos7 ~]# vim ~/.vimrc
[root@centos7 ~]# vim
[root@centos7 ~]# cat ~/.vimrc
set tabstop=4
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: N51045")
call setline(5,"#QQ: 1467551834")
call setline(6,"#version: 1.0")
call setline(7,"#Date : ".strftime("%Y-%m-%d"))
call setline(8,"#FileName: ".expand("%"))
call setline(9,"#URL: http://www.magedu.com")
call setline(10,"#Description: The test script")
call setline(11,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(12,"#************************************************************************************")
call setline(13,"")
endif
endfunc
autocmd BufNewFile * normal G
[root@centos7 ~]#
# 新建測試腳本文件test.sh
[root@centos7 ~]# ls
anaconda-ks.cfg f1.txt
[root@centos7 ~]# vim test.sh
[root@centos7 ~]# ls
anaconda-ks.cfg f1.txt test.sh
[root@centos7 ~]# cat test.sh
#!/bin/bash
#
#*************************************************************************************
#Author: N51045
#QQ: 1467551834
#version: 1.0
#Date : 2020-12-22
#FileName: test.sh
#URL: http://www.magedu.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#************************************************************************************
[root@centos7 ~]#
3.查找/etc目錄下大于1M且類型為普通文件的所有文件
# 方法一
[root@centos7 ~]# find /etc/ -size +1M -a -type f -exec ls -lhS {} \+;
-r--r--r--. 1 root root 8.0M Nov 25 06:40 /etc/udev/hwdb.bin
-rw-------. 1 root root 3.8M Apr 1 2020 /etc/selinux/targeted/active/policy.kern
-rw-r--r--. 1 root root 3.8M Apr 1 2020 /etc/selinux/targeted/policy/policy.31
-rw-r--r--. 1 root root 1.4M Apr 1 2020 /etc/selinux/targeted/contexts/files/file_contexts.bin
[root@centos7 ~]#
# 方法二
[root@centos7 ~]# find /etc/ -size +1M -a -type f | xargs ls -lhS
-r--r--r--. 1 root root 8.0M Nov 25 06:40 /etc/udev/hwdb.bin
-rw-------. 1 root root 3.8M Apr 1 2020 /etc/selinux/targeted/active/policy.kern
-rw-r--r--. 1 root root 3.8M Apr 1 2020 /etc/selinux/targeted/policy/policy.31
-rw-r--r--. 1 root root 1.4M Apr 1 2020 /etc/selinux/targeted/contexts/files/file_contexts.bin
[root@centos7 ~]#
4.打包/etc/目錄下面所有conf結尾的文件,壓縮包名稱為當天的時間,并拷貝到/usr/local/src目錄備份。
[root@centos7 ~]#
[root@centos7 ~]# ll /usr/local/src/
total 0
[root@centos7 ~]# find /etc/ -name "*.conf" | xargs tar -cpf `date +%F`.tar && cp -a `date +%F`.tar /usr/local/src/
tar: Removing leading `/' from member names
[root@centos7 ~]# date
Tue Dec 22 20:17:28 CST 2020
[root@centos7 ~]# ll -h 2020-12-22.tar /usr/local/src/2020-12-22.tar
-rw-r--r-- 1 root root 190K Dec 22 20:16 2020-12-22.tar
-rw-r--r-- 1 root root 190K Dec 22 20:16 /usr/local/src/2020-12-22.tar
[root@centos7 ~]#
5.查找當前系統上沒有屬主或屬組,且最近一個周內曾被訪問過的文件或目錄
[root@centos7 ~]# find / \( -nouser -o -nogroup \) -atime +7 \( -type f -o -type d \) | xargs ls -lh
find: ‘/proc/2113/task/2113/fdinfo/6’: No such file or directory
find: ‘/proc/2113/fdinfo/5’: No such file or directory
-rw-r--r-- 1 1005 distro 18 Apr 1 2020 /home/mandriva/.bash_logout
-rw-r--r-- 1 1005 distro 193 Apr 1 2020 /home/mandriva/.bash_profile
-rw-r--r-- 1 1005 distro 231 Apr 1 2020 /home/mandriva/.bashrc
-rw-rw---- 1 1005 mail 0 Dec 10 19:07 /var/spool/mail/mandriva
[root@centos7 ~]#
6.查找/etc目錄下至少有一類用戶沒有執行權限的文件
[root@centos7 ~]# find /etc -not -perm -111 | wc -l
2040
[root@centos7 ~]# find /etc ! -perm -111 | wc -l
2040
[root@centos7 ~]#