1、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-17
#FileName: system_info_1.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
RED="\E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
$GREEN-----------------------Host systeminfo----------------------------------$END
echo -e "HOSTNAME: $RED`hostname`$END"
echo -e "IPADDR: $RED`ifconfig eth0|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1`$END"
echo -e "OSVERSION: $RED`cat /etc/redhat-release`$END"
echo -e "KERNEL: $RED`uname -r`$END"
echo -e "CPU: $RED`lscpu|grep 'Model name'|tr -s ' '|cut -d : -f2`$END"
echo -e "MEMORY: $RED`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`$END"
echo -e "DISK: $RED`lsblk|grep '^sd'|tr -s ' '|cut -d " " -f4`$END"
$GREEN------------------------------------------------------------------------$END
2、编写脚本 backup.sh,可实现每日将 /etc/ 目录备份到 /backup/etcYYYY-mm-dd中
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-17
#FileName: backup2.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
COLOR='echo -e \E[1;35m'
END='\E[0m'
BACKUP=/backup
SRC=/etc
DATE=`date +%F`
mkdir -p ${BACKUP}
${COLOR}Starting backup...$END
sleep 2
cp -av $SRC ${BACKUP}${SRC}_$DATE
${COLOR}Backup is finished$END
3、编写脚本 disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-17
#FileName: disk.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
GREEN="echo -e \E[1;33m"
END="\E[0m"
${GREEN}`df | grep "^/dev/sd" |grep -Eo [0-9]+%|cut -d% -f1|sort -nr|head -1`${END}
4、编写脚本 links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排
序
BEGIN="\e[1;32m"
END="\e[0m"
echo -e ${BEGIN}`ss -tan |grep "ESTAB"|tr -s " " :|cut -d: -f6|sort|uniq -c |sort -nr`${END}
5、编写脚本 argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: argsnum.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
if [ $# -lt 1 ];then
echo "至少应该给一个参数"
exit
elif [ -f $1 ];then
grep "^$" $1 |wc -l
else
echo "请输入有效文件路径"
fi
6、编写脚本 hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: hostping.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "Please input IP address: " IP
ping -c1 -W1 $IP &> /dev/null && echo "该$IP地址可访问" || echo "该$IP地址不可访问"
7、编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: checkdisk.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
WARNING=1
SPACE_USE=`df|sed -nr '/^\/dev\/sd/ s@^.* ([0-9]+)%.*@\1@p'|sort -nr |head -1`
INODE_USE=`df -i|sed -nr '/^\/dev\/sd/ s@^.* ([0-9]+)%.*@\1@p'|sort -nr |head -1` [ $SPACE_USE -gt $WARNING -o $INODE_USE -gt $WARNING ] && wall disk_space is fulled
8、编写脚本 per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: per.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
[ ! -r $1 -a ! -w $1 ] && echo "不可读且不可写"
read -p "请输入文件: " file
if [ ! -r $file -a ! -w $file ] ; then
echo "不可读且不可写"
fi
9、编写脚本 excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: excute.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "请输入参数文件:" FILE
[ -f $FILE ] && [[ $FILE == *.sh ]] && chmod +x $FILE; echo 已经添加执行权限 || echo "这不是脚本文件"
read -p "Please input a filename: " file
if [[ $file =~ .*sh$ ]] ; then
if [ -f $file ] ; then
chmod a+x $file
echo "success"
fi
else
echo '非脚本文件'
fi
10、编写脚本 nologin.sh和 login.sh,实现禁止和允许普通用户登录系统
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: nologin.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
[ -f /etc/nologin ] && echo "已禁止普通用户登录" || { touch /etc/nologin;echo "已开启禁止普通用户登录"; }
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: login.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
[ -f /etc/nologin ] && { rm -rf /etc/nologin;echo "已开启普通用户登录"; } || echo "已允许普通用户登录"
11、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin 用户 root 登录时,将命令指示符变成红色,并自动启用如下别名: rm=‘rm -i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系统是
CentOS7)
echo "PATH=$PATH:/usr/local/apache/bin" >> /etc/profile.d/path.sh
vim /root/.bashrc
PS1="\[\e[0;31m[\u@\h \W]\\$\e[0m\]"
alias rm='rm –i'
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-ens33'
12、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
[root@CentOS8-minal ~]# echo 'echo -e "\e[1;31mHi,dangerous! \e[0m"' > /etc/prodfile.d/start.sh
13、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
vim .vimrc
set ignorecase
set cursorline
set autoindent
set expandtab
set ts=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: wangmingwang")
call setline(5,"#QQ: 2380821650")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: http://www.wangmingwang.com")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
14 计算身体质量指数 (BMI)脚本:
#********************************************************************
read -p "请输入身高(m为单位): " HIGH
if [[ ! $HIGH =~ ^[0-2](\.[0-9]{,2})?$ ]];then
echo "输入错误的身高"
exit 1
fi
read -p "请输入体重(kg为单位) " WEIGHT
if [[ ! "$WEIGHT" =~ ^[0-9]{1,3}$ ]];then
echo "输入错误的身高"
exit 1
fi
BMI=`echo $WEIGHT/$HIGH^2|bc`
if [ $BMI -le 18 ];then
echo "太瘦了,多吃点"
elif [ $BMI -lt 24 ];then
echo "身材很棒!"
else
echo "太胖了,注意节食,加强运动"
fi
15、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之。并设置初始密码为123456,显示添加的用户的id号等信息,在此新用户第一次登录时,会提示用户立即改密码,如果没有参数,就提示:请输入用户名
版本1:
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-21
#FileName: createuser.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#定义变量
read -p "Please input Username: " username
#判断用户名是否存在
if [ -z $username ];then
echo "请输入用户名: "
exit 2
elif `id $username &> /dev/null`;then
echo "$username is exited"
else
#如果不存在,添加用户 设置密码 下次登陆时修改密码 同时显示ID等信息
PASSWORD=`cat /dev/urandom |tr -dc [:alpha:] |head -c8`
`useradd $username &> /dev/null`
`echo $PASSWORD |passwd --stdin $username &> /dev/null`
echo "用户名:$username 密码: $PASSWORD" >> user.txt
`chage -d 0 $username`
echo "用户已添加,用户的ID信息为:`id $username` 密码为: $PASSWORD"
版本2:
#!/bin/bash
read -p "Please input a username: " NAME
if [ -z $NAME ];then
echo -e "\e[1;37mInput a username.\e[0m"
exit 2
fi
if getent passwd $NAME;then
echo -e "\e[1;32m$NAME is exist.\e[0m"
else
useradd $NAME
echo $NAME:123456 | chpasswd $NAME
id $NAME
passwd -e $NAME &> /dev/null
fi
16、编写脚本 yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
版本1 :
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: yesorno.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "Please input yes or no : " answer
answer=`echo $answer |tr 'A-Z' 'a-z'`
case $answer in
y|yes)
echo "your input is YES"
;;
n|no)
echo "your input is NO"
;;
*)
echo "input fales,please input yes or no!"
esac
版本2 :
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: case_yesorno2.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "Please input yes or no : " answer
case $answer in
[yY]|[yY][Ee][sS])
echo "You input is YES"
;;
[Nn]|[Nn][Oo])
echo "You insput is NO"
;;
*)
echo "You input false,Please input yes or no !"
esac
17、编写脚本 filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
read -p "Please input a filename: " FILE
TYPE=`ls -ld $FILE|grep -Eo '^.'`
case $TYPE in
-)
echo -e "\E[1;31m$FILE is a nomal file\E[0m"
;;
d)
echo -e "\E[1;32m$FILE is a directory file\E[0m"
;;
l)
echo -e "\E[1;33m$FILE is a link file\E[0m"
;;
b)
echo -e "\E[1;34m$FILE is a block file\E[0m"
;;
c)
echo -e "\E[1;35m$FILE is a character file\E[0m"
;;
s)
echo -e "\E[1;36m$FILE is a socket file\E[0m"
;;
p)
echo -e "\E[1;37m$FILE is a pipe file\E[0m"
;;
*)
echo -e "\E[1;31m$FILE is other type file\E[0m"
esac
18、编写脚本 checkint.sh,判断用户输入的参数是否为正整数
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: checkint.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "Please input a digit: " NUM
[[ $NUM =~ ^[0-9]+$ ]] && echo -e "\E[1;32m$NUM is a positive integer\E[0m" || echo -e "\E[1;31m$NUM is not a positive integer\E[0m"
19、编写脚本 reset.sh,实现系统安装后的初始化环境,包括:1、别名 2、环境变量,如PS1等 3、安装常用软件包,如:tree 5、网卡名修改和实现固定的IP的设置,6、vim的设置,7、关闭selinux和iptables 8 配置yum源等
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: reset_centos.sh
#URL: www.wangmingwang.com
#Description: The Centos reset script
#Copyright (C): 2021 All rights reserved
#********************************************************************
. /etc/init.d/functions
centos_version() {
sed -rn 's#^.* ([0-9]+)\..*#\1#p' /etc/redhat-release
}
disable_selinux(){
sed -ri.bak 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config
action "CentOS`centos_version`SELinux已禁用,请重新启动系统后才能生效!"
}
set_alias(){
cat >>~/.bashrc <<EOF
alias cdnet="cd /etc/sysconfig/network-scripts"
alias editeth0="vim /etc/sysconfig/network-scripts/ifcfg-eth0"
alias scandisk="echo '- - -' > /sys/class/scsi_host/host0/scan;echo '- - -' > /sys/class/scsi_host/host1/scan;echo '- - -' > /sys/class/scsi_host/host2/scan"
EOF
action "CentOS`centos_version`系统别名已设置成功,请重新登陆后生效!"
}
set_vimrc(){
cat >~/.vimrc <<EOF
set ts=4
set expandtab
set ignorecase
set cursorline
set autoindent
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: wangmingwang")
call setline(5,"#QQ: 2380821650")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: www.wangmingwang.com")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
EOF
action "CentOS`centos_version`vimrc设置完成,请重新系统启动才能生效!"
}
set_mailrc(){
cat >~/.mailrc <<EOF
set from=2380821650@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=2380821650@qq.com
set smtp-auth-password=hrlnpctmxkpqbjdd
set smtp-auth=login
set ssl-verify=ignore
EOF
action "CentOS`centos_version`mailrc设置完成,请重新登录后才能生效!"
}
disable_firewalld_centos78(){
systemctl disable --now firewalld &> /dev/null
action "CentOS`centos_version`防火墙已关闭!"
}
disable_firewalld_centos6(){
chkconfig iptables off
action "CentOS`centos_version`防火墙已关闭!"
}
disable_firewalld(){
centos_version | while read ov ;do
if [ $ov -eq 6 ];then
disable_firewalld_centos6
else
disable_firewalld_centos78
fi
done
}
set_yum_centos8(){
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/BaseOS/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/BaseOS/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/BaseOS/\$basearch/os/
http://mirrors.163.com//centos/\$releasever/BaseOS/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/AppStream/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/AppStream/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/AppStream/\$basearch/os/
http://mirrors.163.com/centos/\$releasever/AppStream/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/\$basearch/
https://mirrors.huaweicloud.com/epel/\$releasever/Everything/\$basearch/
https://mirrors.cloud.tencent.com/epel/\$releasever/Everything/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/Everything/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/os/
http://mirrors.163.com/centos/\$releasever/extras/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
enabled=1
[centosplus]
name=centosplus
baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/centosplus/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/centosplus/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/os/
http://mirrors.163.com/centos/\$releasever/centosplus/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/centosplus/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
dnf clean all
dnf repolist
action "CentOS`centos_version`YUM源设置完成!"
}
set_yum_centos7(){
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/os/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/
http://mirrors.163.com/centos/\$releasever/os/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/\$basearch/
https://mirrors.huaweicloud.com/epel/\$releasever/\$basearch/
https://mirrors.cloud.tencent.com/epel/\$releasever/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/
http://mirrors.163.com/centos/\$releasever/extras/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[updates]
name=updates
baseurl=https://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/updates/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/updates/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/updates/\$basearch/
http://mirrors.163.com/centos/\$releasever/updates/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[centosplus]
name=centosplus
baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/centosplus/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/centosplus/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/
http://mirrors.163.com/centos/\$releasever/centosplus/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/centosplus/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
EOF
yum clean all
yum repolist
action "CentOS`centos_version`YUM源设置完成!"
}
set_yum_centos6(){
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/os/\$basearch/
https://mirrors.aliyun.com/centos-vault/\$releasever.10/os/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[epel]
name=epel
baseurl=https://mirrors.cloud.tencent.com/epel/\$releasever/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.cloud.tencent.com/epel/RPM-GPG-KEY-EPEL-\$releasever
[extras]
name=extras
baseurl=https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/extras/\$basearch/
https://mirrors.aliyun.com/centos-vault/\$releasever.10/extras/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[updates]
name=updates
baseurl=https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/updates/\$basearch/
https://mirrors.aliyun.com/centos-vault/\$releasever.10/updates/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[centosplus]
name=centosplus
baseurl=https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/centosplus/\$basearch/
https://mirrors.aliyun.com/centos-vault/\$releasever.10/centosplus/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
EOF
yum clean all
yum repolist
action "CentOS`centos_version`YUM源设置完成!"
}
set_yum(){
centos_version | while read ov2 ;do
if [ $ov2 -eq 8 ];then
set_yum_centos8
elif [ $ov2 -eq 7 ];then
set_yum_centos7
else
set_yum_centos6
fi
done
}
centos_minimal_install(){
yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages
action "CentOS`centos_version`最小化安装建议安装软件已安装完成!"
}
set_eth(){
ETHNAME=`ip addr | sed -nr '/^2/s#^.* ([a-z]+[0-9]+).*#\1#p'`
#修改网卡名称配置文件
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/null
#修改网卡文件名
mv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0
action "CentOS`centos_version`网卡名已修改成功,请重新启动系统后才能生效!"
}
check_ip(){
local IP=$1
VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
if [ $VALID_CHECK == "yes" ]; then
echo "IP $IP available!"
return 0
else
echo "IP $IP not available!"
return 1
fi
else
echo "IP format error!"
return 1
fi
}
set_ip(){
while true; do
read -p "请输入IP地址:" IP
check_ip $IP
[ $? -eq 0 ] && break
done
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE=eth0
NAME=eth0
BOOTPROTO=none
ONBOOT=yes
IPADDR=$IP
PREFIX=24
GATEWAY=10.0.0.2
DNS1=223.5.5.5
DNS2=223.6.6.6
EOF
action "CentOS`centos_version`IP地址已修改成功,请重新启动系统后才能生效!"
}
set_eth_ip(){ if [ $(centos_version) -eq 6 ];then
set_ip
else
set_eth
set_ip
fi
}
set_hostname78(){
read -p "请输入主机名:" HOST
hostnamectl set-hostname $HOST
action "CentOS`centos_version`主机名设置成功,请重新登录生效!"
}
set_hostname6(){
read -p "请输入主机名:" HOST
sed -i.bak -r '/^HOSTNAME/s#^(HOSTNAME=).*#\1'$HOST'#' /etc/sysconfig/network
action "CentOS`centos_version`主机名设置成功,请重新登录生效!"
}
set_hostname(){
if [ $(centos_version) -eq 6 ];then
set_hostname6
else
set_hostname78
fi
}
set_centosps1(){
TIPS="action CentOS`centos_version`PS1已设置完成,请重新登录生效!"
while true;do
echo -e "\E[$[RANDOM%7+31];1m"
cat <<EOF
1)31 红色
2)32 绿色
3)33 黄色
4)34 蓝色
5)35 紫色
6)36 青色
7)随机颜色
8)退出
EOF
echo -e '\E[0m'
read -p "请输入颜色编号(1-8)" NUM
case $NUM in
1)
echo "PS1='\[\e[1;31m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
2)
echo "PS1='\[\e[1;32m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
3)
echo "PS1='\[\e[1;33m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
4)
echo "PS1='\[\e[1;34m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
5)
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
6)
echo "PS1='\[\e[1;36m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
7)
echo "PS1='\[\e[1;"$[RANDOM%7+31]"m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
8)
break
;;
*)
echo -e "\e[1;31m输入错误,请输入正确的数字(1-8)!\e[0m"
;;
esac
done
}
PS3="请选择相应的编号(1-13):"
MENU="
CentOS禁用SELinux
CentOS设置系统别名
CentOS设置vimrc配置文件
CentOS设置mailrc配置文件
CentOS关闭防火墙
CentOS设置YUM源
CentOS_Minimal安装建议安装软件
CentOS1-7全执行
CentOS修改网卡名和IP地址
CentOS设置主机名
CentOS设置PS1(请进入选择颜色)
重启系统
退出
"
select menu in $MENU;do
case $REPLY in
1)
disable_selinux
;;
2)
set_alias
;;
3)
set_vimrc
;;
4)
set_mailrc
;;
5)
disable_firewalld
;;
6)
set_yum
;;
7)
centos_minimal_install
;;
8)
disable_selinux
set_alias
set_vimrc
set_mailrc
disable_firewalld
set_yum
centos_minimal_install
;;
9)
set_eth_ip
;;
10)
set_hostname
;;
11)
set_centosps1
;;
12)
reboot
;;
13)
break
;;
*)
echo -e "\e[1;31m输入错误,请输入正确的数字(1-13)!\e[0m"
;;
esac
done
20 打印九九乘法表
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: 9x9.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#版本1:
for j in {1..9}; do
for i in `seq $j` ;do
echo -e "\E[1;$[RANDOM%7+31]m${i}x${j}=$[i*j]\E[0m\t\c"
done
echo
done
echo
#版本2:
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: 9x9_for.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
语句1:
for i in {1..9};do
for j in `seq $i`; do
echo -e "${j}x${i}=$[j*i]\t\c"
done
echo
done
echo
语句2:
for ((i=1;i<10;i++));do
for ((j=1;j<=i;j++));do
echo -e "${j}x${i}=$((j*i))\t\c"
done
echo
done
for ((i=1;i<=9;i++));do
for ((j=1;j<=i;j++));do
printf "\E[1;$[RANDOM%7+31]m${i}x${j}=$[i*j]\E[0m\t"
done
printf "\n"
done
21 倒置的九九乘法表:
[root@WMW-CentOS8 scripts]# cat 9x9_2.sh
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: 9x9_2.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for i in {1..9}; do
for j in $(seq `echo $[10-$i]`);do
echo -ne "${j}x`echo $[10-i]`=$(((10-i)*j))\t"
done
echo
done
22 生产案例:将指定目录下的文件所有文件的后缀改名为 bak 后缀
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_rename.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
DIR=/data/test
cd $DIR || { echo 无法进入 $DIR ;exit 1; }
for FILE in * ;do
# PRE=`echo $FILE|grep -Eo ".*\."`
# mv $FILE ${PRE}bak
# PRE=`echo $FILE|rev |cut -d. -f 2-|rev`
PRE=`echo $FILE|sed -nr 's/(.*)\.([^.]+)$/\1/p'`
SUFFIX=`echo $FILE|sed -nr 's/(.*)\.([^.]+)$/\2/p'`
mv $FILE $PRE.bak
done
23 面试题:要求将目录YYYY-MM-DD/中所有文件,移动到YYYY-MM/DD/下
1、先创建Y-M-D 格式365个目录并在每个目录下创建10个文件
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_dir.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
PDIR=/data/test
for i in {1..365};do
DIR=`date -d "-$i day" +%F`
mkdir -pv $PDIR/$DIR
cd $PDIR/$DIR
for j in {1..10};do
touch $RANDOM.log
done
done
2、将上面的目录移动到YYYY-MM/DD/下
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_mv.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#脚本安全
set -eu
DIR=/data/test
cd $DIR || { echo 无法进入 $DIR;exit 1; }
for subdir in * ;do
YYYY_MM=`echo $subdir |cut -d"-" -f1,2`
DD=`echo $subdir |cut -d"-" -f3`
[ -d $YYYY_MM/$DD ] || mkdir -p $YYYY_MM/$DD &> /dev/null
mv $subdir/* $YYYY_MM/$DD
done
rm -rf $DIR/*-*-*
24 面试题:扫描一个网段:10.0.0.0/24,判断此网段中主机在线状态,将在线的主机的IP打印出来
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_scan_host.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
NET=10.0.0
for ID in {1..254};do
{
ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID is up |tee -a host_list.log || echo $NET.$ID is down
}&
done
wait
25 计算1+2+3…100总和
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_sum2.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for ((sum=0;i<=100;i++));do
let sum+=i
done
echo sum=$sum
for ((sum=0,i=1;i<=100;sum+=i,i++));do
true
done
echo sum=$sum
26 打印等腰三角形
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_triangle.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "请输入三角形的行数: " line
for ((i=1;i<=line;i++));do
for ((k=0;k<=line-i;k++));do
echo -e ' \c'
done
for ((j=1;j<=2*i-1;j++));do
echo -e '&\c'
done
echo
done
27 生成进度条
[root@WMW-CentOS8 scripts]# for ((i=0;i<=100;i++));do printf "\e[4D%3d%%" $i;sleep 0.05s;done
100%[root@WMW-CentOS8 scripts]#
[root@WMW-CentOS8 scripts]# for ((;;));do echo for;sleep 1;done
for
for
for
for
for
28、判断/var/目录下所有文件的类型
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_filetype.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for file in `ls -a /var`;do
if [ -f $file ];then
echo "$file is normal file"
elif [ -h $file ];then
echo "$file is link file"
elif [ -d "$file" ];then
echo "$file is directory file"
elif [ -p "$file" ];then
echo "$file is pipe file"
elif [ -S "$file" ];then
echo "$file is socket file"
elif [ -c "$file" ];then
echo "$file is character file"
elif [ -b "$file" ];then
echo "$file is block file"
fi
done
版本二:
#!/bin/bash
for FILE in $(ls /var);do
TYPE=$(ls -ld /var/$FILE|egrep -o '^.')
case $TYPE in
-)
echo -e "\e[1;31m$FILE is normal file.\e[0m"
;;
d)
echo -e "\e[1;32m$FILE is directory file.\e[0m"
;;
b)
echo -e "\e[1;33m$FILE is block file.\e[0m"
;;
c)
echo -e "\e[1;34m$FILE is character file.\e[0m"
;;
l)
echo -e "\e[1;35m$FILE is link file.\e[0m"
;;
p)
echo -e "\e[1;36m$FILE is pipe file.\e[0m"
;;
S)
echo -e "\e[1;37m$FILE is socket file.\e[0m"
;;
*)
echo -e "\e[1;31m$FILE is other file.\e[0m"
esac
done
29、添加10个用户user1-user10,密码为8位随机字符
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: user_for.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for i in {1..10};do
useradd user$i
PASS=`cat /dev/urandom|tr -dc '[:alpha:]' |head -c8`
echo $PASS |passwd --stdin user$i &> /dev/null
echo user$i:$PASS >> /data/user.log
echo "user$i is created"
done
30、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: rc_ks.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
FILE=`ls /etc/rc.d/rc3.d/`
for i in FILE;do
find /etc/rc.d/rc3.d/S* -exec mv {} "{} start" \; &> /dev/null
find /etc/rc.d/rc3.d/K* -exec mv {} "{} stop " \; &> /dev/null
done
31、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: sum_triangle.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p " Please input a positive interger: " INT
for ((i=1,sum=0;i<=$INT;i++));do
let sum+=i
done
echo -e "\E[1;$[RANDOM%7+31]mThe sum is $sum.\E[0m"
32、计算100以内所有能被3整除的整数之和
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: zhengchu3_sum.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for ((i=1,sum=0;i<=100;i++));do
YU=$[i%3]
[ $YU -eq 0 ] && let sum+=i
done
echo -e "\E[1;$[RANDOM%7+31]mThe sum is $sum\E[0m"
33、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: for_hostcheck.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
IP=10.0.0
RED='\E[1;31m'
GREEN='\E[1;32m'
END='\E[0m'
for i in {1..254};do
{
ping -c1 -W1 ${IP}.${i} &> /dev/null && echo -e "${GREEN}${IP}.${i} is up${END}"|tee -a hostlist2.log || echo -e "${RED}${IP}.${i} is down${END}"
}&
done
wait
for ((i=1;i<=254;i++));do
ping -c1 -W1 ${IP}.${i} &> /dev/null && echo -e "${GREEN}${IP}.${i} is up.${END}" || echo -e "${RED}${IP}.${i} is down.${END}"
done
34、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:
1AbCdeFgH.html
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: mkdir_html.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#!/bin/bash
NAME=`cat /dev/urandom|tr -dc '[:alpha:]'|head -c8`
DIR=/data/testdir
for i in `seq 10`;do
if [ -d $DIR ];then
touch ${DIR}/${i}${NAME}.html
else
mkdir -p $DIR
touch ${DIR}/${i}${NAME}.html
fi
done
35、猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: monkey.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
sum=1
for ((n=1;n<10;n++));do
sum=$[2*$[$sum+1]]
done
echo "猴子所摘桃子数: $sum"
36 防止DOS 攻击脚本
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: check_link.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
WARNING=10
touch deny_hosts.txt
while true;do
ss -nt |sed -nr '1!s#.* ([0-9.]+):[0-9]+ *#\1#p'|sort |uniq -c |sort |
while read count ip ;do
if [ $count -gt $WARNING ];then
echo $ip is deny
grep -q "$ip" deny_hosts.txt || { echo $ip >> deny_hosts.txt;
iptables -A INPUT -s $ip -j REJECT; }
fi
done
sleep 10
done
37、编写脚本,求100以内所有正奇数之和
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: while_sum.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
i=1
sum=0
while ((i<=100));do
let sum+=i
let i+=2
done
echo -e "\e[1;31mThe sum is $sum\e[0m"
38、编写脚本,提示请输入网络地址,如:192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: while_hostcheck.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
IP=10.0.0
i=1
UP=0
DOWN=0
while [ $i -le 10 ];do
if ping -c1 -W1 ${IP}.$i &> /dev/null;then
echo -e "\E[1;32m${IP}.${i} is UP\E[0m"
let UP++
else
echo -e "\E[1;31m${IP}.${i} is down\E[0m"
let DOWN++
fi
let i++
done
echo -e "\E[1;32mUP is $UP\E[0m"
echo -e "\E[1;31mDOWN is $DOWN\E[0m"
wait
39、编写脚本,打印九九乘法表
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-22
#FileName: while_9x9.sh
#URL: www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
echo -e "\E[1;$[RANDOM%7+31]m${j}x${i}=$[i*j]\t\c\E[0m"
let j++
done
let i++
echo
done
40、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值
#!/bin/bash
#
#********************************************************************
#Author: wangmingwang
#QQ: 2380821650
#Date: 2021-04-20
#FileName: while_max_min.sh
#URL: http://www.wangmingwang.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
i=1
MAX=$RANDOM
MIN=$MAX
echo $MAX
while [ $i -lt 10 ];do
N=$RANDOM
echo $N
if [ $N -gt $MAX ];then
MAX=$N
elif [ $N -lt $MIN ];then
MIN=$N
fi
let i++
done
echo MAX=$MAX,MIN=$MIN
41、编写脚本,实现打印国际象棋棋盘
#!/bin/bash
for i in {1…8};do
temp1=$[ $i % 2 ]
for j in {1..8};do
temp2=$[ $j % 2 ]
if [ $temp1 -eq $temp2 ];then
echo -e -n "\033[47m \033[0m"
else
echo -e -n "\033[41m \033[0m"
fi
done
echo
done
42、文件host_list.log 如下格式,请提取”.magedu.com”前面的主机名部分并写入到回到该文件中
1 www.magedu.com
2 blog.magedu.com
3 study.magedu.com
4 linux.magedu.com
5 python.magedu.com
…
999 study.magedu.com
awk -F"[ .]" '{print $2}' host_list.log >> host_list.log
43、统计/etc/fstab文件中每个文件系统类型出现的次数
[root@CentOS8 ~]# awk '/^UUID/{diskfile[$3]++}END{for (i in diskfile){print i,diskfile[i]}}' /etc/fstab
swap 1
ext4 1
xfs 2
44、统计/etc/fstab文件中每个单词出现的次数
[root@CentOS8 ~]# awk '{for(i=1;i<NF;i++){count[$i]++}} END{for(i in count) {print i,count[i]}}' /etc/fstab
45、提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有数字
[root@CentOS8 ~]# echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw" |awk '{gsub(/[^0-9]/,"",$0);print $0}'
05973
46、有一文件记录了1-100000之间随机的整数共5000个,存储的格式100,50,35,89…请取出其中最大和最小的整数
[root@CentOS8 ~]# for i in {1..5000};do echo -n $((($RANDOM+90000)%100000)),>>random.txt;done
[root@CentOS8 ~]# awk -F, '{max=$1;min=$1;for(i=1;i<NF;i++){if($i>max){max=$i}else{if($i<min){min=$i}}}}END{print "最大值:"max,"最小值:"min}' random.txt
最大值:99999 最小值:1
47、解决Dos攻击生产案例:
根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔5分钟。防火墙命令为iptables -A INPUT -s IP -j REJECT
[root@centos8 ~]#cat ss.sh
#!/bin/bash
#Version: 1.1
#**********************************************************************
ss -nt|awk -F' +|:' 'NR!=1{ip[$(NF-2)]++}END{for(i in ip){if(ip[$i]>=100){system("iptables -A INPUT -s "i" -j REJECT")}}}'
[root@centos8 ~]#chmod +x ss.sh
[root@centos8 ~]#crontab -e
*/5 * * * * root ss.sh
48、将以下文件内容中FQDN取出并根据其进行计数从高到低排序
http://mail.magedu.com/index.html
http://www.magedu.com/test.html
http://study.magedu.com/index.html
http://blog.magedu.com/index.html
http://www.magedu.com/images/logo.jpg
http://blog.magedu.com/20080102.html
http://www.magedu.com/images/magedu.jpg
[root@CentOS8 ~]# awk -F'/' '{FQDN[$3]++}END{for(i in FQDN){print FQDN[i],i}}' fqdn.txt |sort -nr
3 www.magedu.com
2 blog.magedu.com
1 study.magedu.com
1 mail.magedu.com
49、将以下文本文件awktest.txt中 以inode列为标记,对inode列相同的counts列进行累加,并且统计出同一inode中,beginnumber列中的最小值和endnumber列中的最大值
inode|beginnumber|endnumber|counts|
106|3363120000|3363129999|10000|
106|3368560000|3368579999|20000|
310|3337000000|3337000100|101|
310|3342950000|3342959999|10000|
310|3362120960|3362120961|2|
311|3313460102|3313469999|9898|
311|3313470000|3313499999|30000|
311|3362120962|3362120963|2|
[root@CentOS8 ~]# awk -F '|' '!/^inode/{sum[$1]+=$4;
if(!begin[$1])begin[$1]=$2;else if(begin[$1]>$2)begin[$1]=$2;
if(!end[$1])end[$1]=$3;else if(end[$1]<$3)end[$1]=$3}
END{for(i in sum)print i"|"begin[i]"|"end[i]"|"sum[i]}' awktest.txt
106|3363120000|3368579999|30000
310|3337000000|3362120961|10103
311|3313460102|3362120963|39900
#!/usr/bin/awk -f
NR!=1{
if( $1 in inode){
if(inode[$1]["beginMinNum"]>$2){
inode[$1]["beginMinNum"]=$2
}
if(inode[$1]["endMaxNum"]<$3){
inode[$1]["endMaxNum"]=$3
}
inode[$1]["counts"]+=$4
}else{
inode[$1]["beginMinNum"]=$2;
inode[$1]["endMaxNum"]=$3;
inode[$1]["counts"]=$4;
}
}
END{
\# for (i in inode){
\# for(j in inode[i]){
\# print inode[i][j];
\# }
\# }
OFS="|";
for(i in inode){
print i,inode[i]["beginMinNum"],inode[i]["endMaxNum"],inode[i]["counts"]
}
}