1、编写脚本createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之,显示添加的用户的id号等信息。
答:① 通过vim工具,在/data/createuser.sh文件中,输入以下内容:
read -p "请输入用户名:" USER_NAME
id $USER_NAME &> /dev/null && echo "$USER_NAME已存在。" || (useradd $USER_NAME ; echo "$USER_NAME已创建。" ; id $USER_NAME)
② bash /data/createuser.sh:运行该脚本,根据提示信息,回答问题。
[root@centos8 data]#vim /data/createuser.sh
[root@centos8 data]#bash /data/createuser.sh
请输入用户名:user
user已存在。
[root@centos8 data]#bash /data/createuser.sh
请输入用户名:user7
user7已创建。
uid=2007(user7) gid=2007(user7) groups=2007(user7)
[root@centos8 data]#
2、编写生成脚本基本格式的脚本,包括作者、联系方式、版本、时间、描述等。
答:① 通过vim工具,在~/.vimrc文件中,输入以下内容:
set et
set ts=4
set number
set cursorline
autocmd BufNewFile .sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
cal setline(1,"#!/bin/bash")
cal setline(2,"#")
cal setline(3,"#")
cal setline(4,"#Author: qc")
cal setline(5,"#QQ 285076228")
cal setline(6,"#Date: ".strftime("%Y-%m-%d"))
cal setline(7,"#FileName ".expand("%"))
cal setline(8,"#URL:")
cal setline(9,"#Description:")
cal setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
cal setline(11,"#*")
cal setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
② vim /data/hello.sh:通过vim工具,创建/data/hello.sh文件,并自动生成shell声明序列和注释。
3、查找/etc目录下大于1M且类型为普通文件的所有文件。
答:find /etc/ -size +1M -a -type f
[root@centos8 ~]#find /etc/ -size +1M -a -type f
/etc/selinux/targeted/policy/policy.31
/etc/udev/hwdb.bin
[root@centos8 ~]#
[root@centos8 ~]#ll -h /etc/selinux/targeted/policy/policy.31 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 8.5M Jul 19 2020 /etc/selinux/targeted/policy/policy.31
-r--r--r--. 1 root root 9.0M Jul 19 2020 /etc/udev/hwdb.bin
[root@centos8 ~]#
4、打包/etc/目录下面所有conf结尾的文件,压缩包名称为当天的时间,并拷贝到/usr/local/src目录备份。
答:find /etc/ -name "*.conf" | xargs tar zcvf /usr/local/src/date +%F
.tar.gz
注:``会自动隐藏。
[root@centos8 ~]#find /etc/ -name "*.conf" | xargs tar zcvf /usr/local/src/`date +%F`.tar.gz
tar: Removing leading `/' from member names
/etc/dnf/dnf.conf
tar: Removing leading `/' from hard link targets
/etc/dnf/plugins/copr.conf
/etc/dnf/plugins/debuginfo-install.conf
/etc/dnf/plugins/product-id.conf
/etc/dnf/plugins/subscription-manager.conf
/etc/dnf/plugins/spacewalk.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/yum.conf
/etc/dnf/protected.d/sudo.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/66-google-noto-sans-tai-viet.conf
...
[root@centos8 ~]#
[root@centos8 ~]#ll /usr/local/src
total 152
-rw-r--r-- 1 root root 153069 Aug 22 17:49 2021-08-22.tar.gz
[root@centos8 ~]#
5、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件或目录。
答:find / ( -nouser -o -nogroup ) -a -atime -7 -ls
注:\ 会自动隐藏。
[root@centos8 ~]#find / \( -nouser -o -nogroup \) -a -atime -7 -ls
find: ‘/proc/2443/task/2443/fd/5’: No such file or directory
find: ‘/proc/2443/task/2443/fdinfo/5’: No such file or directory
find: ‘/proc/2443/fd/6’: No such file or directory
find: ‘/proc/2443/fdinfo/6’: No such file or directory
135608783 0 drwxr-xr-x 3 qc 1111 46 Feb 13 2021 /root/backup2021-04-09/dir1
69674350 0 -rw-rw---- 1 1005 mail 0 May 9 23:03 /var/spool/mail/mandriva
659727 0 drwx------ 3 tomcat 1005 99 May 9 00:08 /home/tomcat
69674329 0 drwxr-xr-x 4 tomcat 1005 39 Jul 19 2020 /home/tomcat/.mozilla
135608784 0 drwxr-xr-x 2 tomcat 1005 6 May 14 2019 /home/tomcat/.mozilla/extensions
203891686 0 drwxr-xr-x 2 tomcat 1005 6 May 14 2019 /home/tomcat/.mozilla/plugins
1274389 4 -rw-r--r-- 1 tomcat 1005 18 Nov 9 2019 /home/tomcat/.bash_logout
1274390 4 -rw-r--r-- 1 tomcat 1005 141 Nov 9 2019 /home/tomcat/.bash_profile
1775049 4 -rw-r--r-- 1 tomcat 1005 312 Nov 9 2019 /home/tomcat/.bashrc
1775053 4 -rw------- 1 tomcat 1005 169 May 9 00:08 /home/tomcat/.bash_history
135608789 0 drwx------ 3 1005 distro 78 May 9 23:03 /home/mandriva
201328710 0 drwxr-xr-x 4 1005 distro 39 Jul 19 2020 /home/mandriva/.mozilla
1274371 0 drwxr-xr-x 2 1005 distro 6 May 14 2019 /home/mandriva/.mozilla/extensions
68644011 0 drwxr-xr-x 2 1005 distro 6 May 14 2019 /home/mandriva/.mozilla/plugins
135608792 4 -rw-r--r-- 1 1005 distro 18 Nov 9 2019 /home/mandriva/.bash_logout
135608793 4 -rw-r--r-- 1 1005 distro 141 Nov 9 2019 /home/mandriva/.bash_profile
135608794 4 -rw-r--r-- 1 1005 distro 312 Nov 9 2019 /home/mandriva/.bashrc
[root@centos8 ~]#
[root@centos8 ~]#stat /root/backup2021-04-09/dir1
File: /root/backup2021-04-09/dir1
Size: 46 Blocks: 0 IO Block: 4096 directory
Device: 802h/2050d Inode: 135608783 Links: 3
Access: (0755/drwxr-xr-x) Uid: ( 1000/ qc) Gid: ( 1111/ UNKNOWN)
Access: 2021-08-22 17:39:39.158840364 +0800
Modify: 2021-02-13 11:37:20.033301932 +0800
Change: 2021-04-09 22:06:09.797952050 +0800
Birth: -
[root@centos8 ~]#
6、查找/etc目录下至少有一类用户没有执行权限的文件。
答:find /etc/ -not ( -perm -111 ) -ls 或 find /etc/ ! ( -perm -111 ) -ls
注:\ 会自动隐藏。
[root@centos8 ~]#find /etc/ -not \( -perm -111 \) -ls
68625812 4 -r--r--r-- 1 root root 76 Feb 11 2020 /etc/lvm/profile/thin-generic.profile
68625813 4 -r--r--r-- 1 root root 80 Feb 11 2020 /etc/lvm/profile/thin-performance.profile
68625814 4 -r--r--r-- 1 root root 563 Feb 11 2020 /etc/lvm/profile/vdo-small.profile
134330171 4 -rw-r--r-- 1 root root 22 Jun 3 2020 /etc/issue.net
134425661 0 -rw-r--r-- 1 root root 0 Apr 14 2020 /etc/kernel/install.d/20-grubby.install
134425662 0 -rw-r--r-- 1 root root 0 Apr 14 2020 /etc/kernel/install.d/90-loaderentry.install
68208736 0 drwxr-x--- 3 root root 45 Jul 19 2020 /etc/dhcp
69202544 4 -rw-r--r-- 1 root root 513 Apr 24 2020 /etc/dhcp/dhclient.conf
67287990 0 drwx------ 2 root root 231 Jul 19 2020 /etc/grub.d
...
134778126 4 ---------- 1 root root 1171 May 10 01:00 /etc/gshadow
134609176 4 ---------- 1 root root 1166 May 10 00:16 /etc/gshadow-
...
134752676 12 -rwxr--r-- 1 root root 11521 Mar 19 2019 /etc/bash_completion.d/gluster
...