Linux相关知识的第五回合

shell相关知识的过招

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

cat createuser.sh
######################################################################
#!/bin/bash
# 此脚本用于添加用户
# 脚本执行格式:
# /bin/sh createuser.sh <用户名>
# 示例:
# /bin/sh createuser.sh hooper

alias echo='echo -e'

### 设置变量
UserName=$1

### 脚本开始

### 要求root运行脚本
if [[ "$(whoami)" != "root" ]]; then
	echo "\e[1;33m \n Please run this script as root.\n \e[0m" >&2
	exit 1
fi

### 判断是否传参
if [[ $# -ne 1 ]]; then
  echo "\e[1;33m \n Custom parameters delivery error, please check...\n For example:/bin/sh createuser.sh hooper\n \e[0m"
  exit 2
fi

### 判断用户名是否存在
UserCounts=$(grep ${UserName} /etc/passwd|wc -l)

if [[ ${UserCounts} == 1 ]]; then
  echo "\e[1;35m \n ${UserName} already exists, please check!!!\n \e[0m"
  exit 3
else
  useradd ${UserName}
  userid=$(grep ${UserName} /etc/passwd|awk -F ":" '{print $3}')
  echo "\e[1;36m \n The ${UserName} is created, and the UserID is ${userid}! \n \e[0m"
fi
######################################################################

# 执行结果
[root@magedu-demo sh]#
[root@magedu-demo sh]# /bin/sh createuser.sh

 Custom parameters delivery error, please check...
 For example:/bin/sh createuser.sh hooper

[root@magedu-demo sh]# /bin/sh createuser.sh hooper

 hooper already exists, please check!!!

[root@magedu-demo sh]# /bin/sh createuser.sh snake

 The snake is created, and the UserID is 2006!

[root@magedu-demo sh]#

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

# 设置全局的基本格式脚本;如果只想针对某个用户添加的话,切换到对应的用户下,编辑vim ~/.vimrc
cp -rp /etc/vimrc{,.`datebak`}
# 在配置文件的最后添加,保存退出
vim /etc/vimrc
######################################################################
" 设置自动添加文件信息

autocmd BufNewFile *.py,*.sh, exec ":call SetTitle()"
let $author_name = "hooper"
let $author_email = "hujing0228@gmail.com"

func SetTitle()
if &filetype == 'sh'
call setline(1,"\###################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: ".$author_name)
call append(line(".")+2, "\# Mail: ".$author_email)
call append(line(".")+3, "\# Created Time: ".strftime("%Y-%m-%d %H:%M"))
call append(line(".")+4, "\#==================================================")
call append(line(".")+5, "\#!/bin/bash")
call append(line(".")+6, "")
else
call setline(1,"\###################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: ".$author_name)
call append(line(".")+2, "\# Mail: ".$author_email)
call append(line(".")+3, "\# Created Time: ".strftime("%Y-%m-%d %H:%M"))
call append(line(".")+4, "\#==================================================")
call append(line(".")+5, "\#!/usr/bin/python")
call append(line(".")+6, "")
endif
endfunc
######################################################################

# 创建shell脚本
[hooper@magedu-demo sh]$ vim test.sh
###################################################
# File Name: test.sh
# Author: hooper
# Mail: hujing0228@gmail.com
# Created Time: 2021-04-06 19:17
#==================================================
#!/bin/bash

查找及打包相关知识的过招

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

find /etc/ -type f -size +1M
/etc/udev/hwdb.bin
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31

find /etc/ -type f -size +1M|xargs ls -lh
-rw-------. 1 root root 3.8M Nov  3  2018 /etc/selinux/targeted/active/policy.kern
-rw-r--r--. 1 root root 1.4M Nov  3  2018 /etc/selinux/targeted/contexts/files/file_contexts.bin
-rw-r--r--. 1 root root 3.8M Nov  3  2018 /etc/selinux/targeted/policy/policy.31
-r--r--r--. 1 root root 7.6M Mar  9 10:29 /etc/udev/hwdb.bin

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

find /etc/ -name "*.conf"|xargs tar -zcvf /usr/local/src/`date +%F`.tar.gz
tar: Removing leading `/' from member names
/etc/resolv.conf
/etc/depmod.d/dist.conf
/etc/dracut.conf
/etc/prelink.conf.d/nss-softokn-prelink.conf
/etc/prelink.conf.d/fipscheck.conf
# ....忽略
/etc/fonts/conf.d/65-fonts-persian.conf
/etc/fonts/conf.d/65-nonlatin.conf
/etc/fonts/conf.d/69-unifont.conf
/etc/fonts/conf.d/80-delicious.conf
/etc/fonts/conf.d/90-synthetic.conf
/etc/fonts/fonts.conf
/etc/rabbitmq/rabbitmq-env.conf
/etc/3hooper.conf

ll /usr/local/src/
total 68K
-rw-r--r-- 1 root root 65K Apr  6 20:01 2021-04-06.tar.gz

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

find /  \( -nouser -o -nogroup -a -atime -7 \)|xargs ls -lh
-rwxr-xr-x  1 10 143 7.8K Jun 22  2016 /data/jdk/jdk1.8.0_101/bin/appletviewer
lrwxrwxrwx  1 10 143    8 Jun 22  2016 /data/jdk/jdk1.8.0_101/bin/ControlPanel -> jcontrol
-rwxr-xr-x  1 10 143 7.8K Jun 22  2016 /data/jdk/jdk1.8.0_101/bin/extcheck
-rwxr-xr-x  1 10 143 7.8K Jun 22  2016 /data/jdk/jdk1.8.0_101/bin/idlj
# ....忽略
/data/jdk/jdk1.8.0_101/jre/plugin:
total 0
drwxr-xr-x 2 10 143 50 Jun 22  2016 desktop

/data/jdk/jdk1.8.0_101/jre/plugin/desktop:
total 12K
-rw-r--r-- 1 10 143  624 Jun 22  2016 sun_java.desktop
-rw-r--r-- 1 10 143 4.3K Jun 22  2016 sun_java.png

/data/jdk/jdk1.8.0_101/man:
total 4.0K
lrwxrwxrwx 1 10 143   11 Jun 22  2016 ja -> ja_JP.UTF-8
drwxr-xr-x 3 10 143   18 Jun 22  2016 ja_JP.UTF-8
drwxr-xr-x 2 10 143 4.0K Jun 22  2016 man1

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

find /etc/ -not -perm /111 -ls
find /etc/ -not -perm /111|xargs ls -lh
-rw-r--r--  1 root root       19 Mar 23 09:34 /etc/3hooper.conf
-rw-r--r--. 1 root root       51 Feb 28  2028 /etc/adjtime
-rw-r--r--. 1 root root     1.5K Jun  7  2013 /etc/aliases
-rw-r--r--. 1 root root      12K Mar  9 10:29 /etc/aliases.db
-rw-------. 1 root root      541 Apr 11  2018 /etc/anacrontab
-rw-r--r--. 1 root root       55 Oct 30  2018 /etc/asound.conf
-rw-r-----. 1 root root      246 Oct 31  2018 /etc/audisp/audispd.conf
-rw-r-----. 1 root root      358 Oct 31  2018 /etc/audisp/plugins.d/af_unix.conf
-rw-r-----. 1 root root      517 Oct 31  2018 /etc/audisp/plugins.d/syslog.conf
-rw-r-----. 1 root root      805 Oct 31  2018 /etc/audit/auditd.conf
-rw-r-----. 1 root root       81 Mar  9 10:29 /etc/audit/audit.rules
-rw-r-----. 1 root root      127 Oct 31  2018 /etc/audit/audit-stop.rules
# ......忽略
-rw-r--r--. 1 root root     2.0K Oct 31  2018 /etc/virc
-rw-r--r--. 1 root root     4.4K May 16  2019 /etc/wgetrc
-rw-------. 1 root root       67 Oct 31  2018 /etc/wpa_supplicant/wpa_supplicant.conf
-rw-r--r--. 1 root root      232 Mar  9 10:24 /etc/X11/xorg.conf.d/00-keyboard.conf
-rw-r--r--. 1 root root      970 Nov  5  2018 /etc/yum.conf
-rw-r--r--. 1 root root      279 Oct 31  2018 /etc/yum/pluginconf.d/fastestmirror.conf
-rw-r--r--. 1 root root      372 Mar  9 10:19 /etc/yum/pluginconf.d/langpacks.conf
-rw-r--r--. 1 root root        8 Oct 31  2018 /etc/yum/protected.d/systemd.conf
-rw-r--r--. 1 root root     1.7K Nov 23  2018 /etc/yum.repos.d/CentOS-Base.repo
-rw-r--r--. 1 root root     1.3K Nov 23  2018 /etc/yum.repos.d/CentOS-CR.repo
-rw-r--r--. 1 root root      649 Nov 23  2018 /etc/yum.repos.d/CentOS-Debuginfo.repo
-rw-r--r--. 1 root root      314 Nov 23  2018 /etc/yum.repos.d/CentOS-fasttrack.repo
-rw-r--r--. 1 root root      630 Nov 23  2018 /etc/yum.repos.d/CentOS-Media.repo
-rw-r--r--. 1 root root     1.3K Nov 23  2018 /etc/yum.repos.d/CentOS-Sources.repo
-rw-r--r--. 1 root root     5.6K Nov 23  2018 /etc/yum.repos.d/CentOS-Vault.repo
-rw-r--r--  1 root root     1.1K Nov  1 04:33 /etc/yum.repos.d/epel.repo
-rw-r--r--  1 root root     1.2K Nov  1 04:33 /etc/yum.repos.d/epel-testing.repo
-rw-r--r--. 1 root root        7 Mar  9 10:21 /etc/yum/vars/contentdir
-rw-r--r--. 1 root root        6 Nov 23  2018 /etc/yum/vars/infra
-rw-r--r--. 1 root root      444 Nov  5  2018 /etc/yum/version-groups.conf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值