SHELL练习脚本100例
批量设置账号并设置随机密码
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-27
#FileName: userfor.sh
#URL: www.yhtzjy.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 '[:alnum:]' 'head -c 12`
echo $PASS ' passwd --stdin user$i & > /dev/null
echo user$i:$PASS >> /data/user.log
echo "user$i is create"
done
将目录YYYY-MM-DD/中所有文件,移动到YYYY-MM/DD/下
#先创建YYYY-MM-DD格式的目录,当前日期一年前365天到目前共365个目录,里面有10个文件.log后缀的文件
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-27
#FileName: for_dir.sh
#URL: www.yhtzjy.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: Mr.yang
#QQ: 1419946323
#Date: 2021-06-27
#FileName: dir.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
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/*-*-*
将指定目录下的文件所有文件的后缀改名为 bak 后缀
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-27
#FileName: for_rename.sh
#URL: www.yhtzjy.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'
# PRE=`echo $FILE ' sed -nr 's/(.*)\.([^.]+)$/\2/p'`
# mv $FILE $PRE.bak
done
九九乘法表
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-28
#FileName: 9x9.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for i in {1..9};do
for (( j=1;j<=i;j++ ));do
printf "${j}x${i}=$[i*j]\t"
done
printf "\n"
done
九九彩色乘法表
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-28
#FileName: 9x9_for.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for i in {1..9};do
for j in `seq $i`;do
printf "\E[1;$[RANDOM%7+31]m${j}x${i}=$[i*j]\t"
done
printf "\n"
done
倒装九九彩色乘法表
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-28
#FileName: 9x9_for.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for ((i=1;i<=9;i++));do
for j in $(seq `echo $[10-$i]`);do
printf "\E[1;$[RANDOM%7+31]m${j}x`echo $[10-i]`=$(((10-i)*j))\E[0m\t"
done
printf "\n"
done
批量创建用户
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-29
#FileName: user.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
[ $# -eq 0 ] && { echo "usage:createuser.sh USERNAME ..." ; exit 1 ; }
for user ;do
id $user &> /dev/null && echo $user is exist '' { useradd $user ; echo $user is created; }
done
计算1+2+3+…+100 的结果
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-29
#FileName: user.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
sum=0;for i in {1..100};do
let sum+=i;
done
echo sum=$sum
打印三角形闪亮的小星星
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-29
#FileName: for_star.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for j in {1..6};do
for i in `seq $j`;do
echo -e "\E[5;1;$[RANDOM%7+31]m*\E[0m\c"
done
echo
done
删库跑路之命令rm的安全实现
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-30
#FileName: rm.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
WARNING_COLOR="echo -e \E[1;31m"
END="\E[0m"
DIR=/tmp/`date +%F_%H-%M-%S`
mkdir $DIR
mv $* $DIR
${WARNING_COLOR}Move $* to $DIR $END
[root@centos8 ~]#chmod a+x /data/scripts/rm.sh
[root@centos8 ~]#alias rm='/data/scripts/rm.sh'
[root@centos8 ~]#touch {1..10}.txt
[root@centos8 ~]#rm *.txt
Move 10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt to /tmp/2020-
04-01_15-15-28
运维菜单实现版本2
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-06-30
#FileName: work_menu.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
echo -en "\E[$RANDOM%7+31];1m"
cat <<EOF
请选择:
1) 备份数据库
2) 清理日志
3) 软件升级
4) 软件回滚
5) 删库跑路
EOF
echo -en '\E[0m'
read -p "请输入上面的数字1-5:" MENU
case $MENU in
1)
echo "执行备份数据库"
#./backup.sh
;;
2)
echo "清理日志"
;;
3)
echo "软件升级"
;;
4)
echo "软件回滚"
;;
5)
echo "删库跑路"
;;
*)
echo "INPUT FALSE!"
esac
显示当前主机系统信息
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-01
#FileName: system_info.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
RED="\E[1;31m"
GREEN="\E[1;32m"
END="\E[0m"
echo -e "$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"
echo -e "$GREEN---------------------------------------------------------$END"
每日将/etc/目录备份到/backup/etcYYYY-mm-dd中
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-02
#FileName: backup.sh
#URL: www.yhtzjy.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`
${COLOR}Staring backup ...$END
sleep 2
cp -av $SRC ${BACKUP}/${SRC}_$DATE
${COLOR}------Backup is finished-----$END
编写脚本 disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-05
#FileName: disk.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
CL="\033[1;33m"
END="\033[0m"
TLN=`df 'tr -s ' ' %'cut -d% -f5 'sort -nr'head -n1`
echo -e "The largest rate used of disk partition is :${CL}${TLN}${END}"
编写脚本 links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-05
#FileName: links.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
BEGIN="\e[1;35m"
END="\e[0m"
echo -e "${BEGIN}`netstat -nat'grep 'ESTAB' 'tr -s ' ' ':' 'cut -d: -f6'sort'uniq -c'sort -nr`${END}"
探测网络IP是否正常
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-05
#FileName: Network_Testing.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "please input the adress :" ADDRESS
if
ping -c2 -w2 ${ADDRESS} &> /dev/null
then
echo "${ADDRESS} is up"
elif
grep -q "${ADDRESS}" ~/maintenance.txt
then
echo "${ADDRESS} is undergoing maintenance"
else
echo "station is unexpecedly DOWN!"
fi
if,else嵌套性试验(检测用户是否存在,存在即显示信息,否则询问是否创建此用户)
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-07
#FileName: if_user.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "Please input the username :" NAME
INFO="echo `id ${NAME}`"
if
id ${NAME} &> /dev/null
then
echo -e "\033[1;32mThe user ${NAME} is exist,and ${NAME}'s information:\n${INFO}\033[0m"
else
echo -e "The user ${NAME} dont exist "
echo -e "\033[1;31mI will crate the account ${NAME},please choice "yes" or "no" \033[0m"
read -p "Please choice :" YN
if
[[ ${YN} =~ ^([Yy]'[Ee]'[Ss])$ ]];
then
useradd ${NAME};
echo "${NAME}:123456" 'chpasswd
chage -d1 ${NAME}
echo "The default password is 123456 ,you must change your passwd next login (force)"
else
echo -e "\033[1;33mwill leave and exit!!!\033[0m"
fi
fi
磁盘邮件预警
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-07
#FileName: Disk_alarm.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
WARNINFO="`df 'grep -E '^/dev/sd*' 'tr -s ' ' 'cut -d ' ' -f1,5,6 'sort -nr -k2 'head -n3`"
#LARSET="`df 'grep -E '^/dev/sd*' 'tr -s ' ' 'cut -d ' ' -f1,5,6 'sort -nr -k2 'head -n1`"
#LARSET="`echo ${WARNINF} 'tr -s ' ' '%' 'cut -d% -f2 'sort -nr'head -n1`"
VALURE="`echo $WARNINFO 'tr -s '%' ' ''sort -nr 'head -n1'cut -d ' ' -f2 `"
if
[[ $VALURE -ge 10 ]]
then
echo -e "Your host's disk rate is more than 10%\n${WARNINFO}" 'mail -v -s "disk_rate " 949786521@qq.com
echo "The rate is ${VAlURE} "
else
echo "正常"
fi
在/etcdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母
#!/bin/bash
[ -d /etcdir ] '' mkdir /etcdir
cd /etcdir
for i in {1..10};do
touch ${i}`cat /dev/urandom 'tr -dc '[[:alnum:]]''head -c8`.html
done
通过位置变量创建 Linux 系统账户及密码
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-08
#FileName: user.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
useradd "$1"
echo "$1" 'passwd --stdin "$2"
每周5凌晨3点使用 tar 命令备份/var/log 下的所有日志文件
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-08
#FileName: logbak.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
tar -czf log-`date +%F_%H_%M_%S`.tar.gz /var/log/
#crontab -e 编写计划任务,执行备份脚本
#00 02 * * 5 /root/logbak.sh
实时监控本机内存和硬盘剩余空间,剩余内存小于 500M、根分区剩余空间小于 1000M时,发送报警邮件给 root 管理员
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-08
#FileName: waring_disk.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#提取根分区剩余空间
disk_size=`df -m / 'awk '/\//{print $4}'`
mem_size=`free -m 'awk '/Mem/{print $4}'`
#注意内存和磁盘提取空间的大小都是以kb为单位
if [ $disk_size -le 512000 -a $mem_size -le 1024000 ];then
mail -s Waring root << EOF
Insufficient resources ,资源不足
EOF
fi
检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-10
#FileName: vsftpd.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
if [ $UID -eq 0 ];then
yum -y install vsftpd
else
echo "Your are not an adminstartator,No Permissions to install software"
fi
编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默认的 123456 作为默认密码。
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-10
#FileName: user.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "请输入用户名: " user
#使用‐z 可以判断一个变量是否为空,如果为空,提示用户必须输入账户名,并退出脚本,退出码为 2
#没有输入用户名脚本退出后,使用$?查看的返回码为 2
if [ -z $user ];then
echo "您不需输入账户名"
exit 2
fi
#使用 stty ‐echo 关闭 shell 的回显功能
#使用 stty echo 打开 shell 的回显功能
stty -echo
read -p "请输入密码:" pass
stty echo
pass=${pass:-123456}
useradd "$user"
echo "$pass" 'passwd --stdin "$user"
编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(for 版本)
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-11
#FileName: ping.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for i in {1..254};
do
ping -f254 -c2 -i0.3 -w1 192.168.4.$i &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.0.$i is up"
else
echo "192.168.0.$i is down"
fi
done
编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(while 版本)
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-11
#FileName: while.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
i=1
while [ $i -le 254 ]
do
ping -f254 -c2 -i0.3 -w1 192.168.4.$i &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.4.$i is up"
else
echo "192.168.4.$i is down"
fi
let i++
done
统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个
#URL: www.yhtzjy.com
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-11
#FileName: apache.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#awk 使用‐F 选项指定文件内容的分隔符是/或者:
#条件判断$7:$8 大于等于 13:30,并且要求,$7:$8 小于等于 14:30
#最后使用 wc ‐l 统计这样的数据有多少行,即多少个
#日志文档内容里面,第 1 列是远程主机的 IP 地址,使用 awk 单独显示第 1 列即可
awk -F "[ /:]" '$7":"$8>="13:30" && $7":"$8<="14:30"{print $1}' /var/log/httpd/access_log
. 统计当前 Linux 系统中可以登录计算机的账户有多少个
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-11
#FileName: user.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#方法 1:
grep "bash$" /etc/passwd 'wc -l
curl检测网站的健康性
[17:16:10 root@aliyun script]#bash curl.sh www.baidu.com
www.baidu.com (Website It’s ok)
[17:16:13 root@aliyun script]#vim curl.sh
#!/bin/bash
usage(){
echo "Usage:$0 url"
exit 1
}
checkurl(){
local num=`curl -I -m 5 -s -w "%{http_code}\n" -o /dev/null $1 'egrep "(200'301'302)"'wc -l`
if [ $num -eq 1 ]
then
echo "$1 (Website It's ok)"
else
echo "$1i (Website It's failed)"
fi
}
main(){
if [ $# -ne 1 ]
then
usage
fi
checkurl $1
}
main $*
mysql linux定时杀掉sleep进程
echo "`date` killing mysql sleep process..." >> /tmp/crontab.log
for id in `mysql -u root -pYourPassword, -e "show processlist"'grep -i -E 'slee
p'locked' ' awk '{if($6>100){print $1}}'`
do
echo "killing pid $id" >> /tmp/crontab.log
echo `mysql -u root -pYourPassword, -e "kill $id"`
done
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Mr.yang 个人博客!
评论