shell脚本实战
一、系统性能监控脚本
#!/bin/bash
# 系统性能监控脚本
# 时间
Time=$(date +"%Y-%m-%d %H:%M:%S")
# ip
ip=$(ifconfig ens32 | grep -w inet | awk '{print $2}')
# 内存剩余空间
a=$(cat /proc/meminfo | grep -i avai | awk '{print $2}')
free_mem=$(($a / 1024))
# 磁盘剩余空间
b=$(df | grep "/$" | awk '{print $4}')
free_disk=$(($b / 1024 / 1024))
# 已登录用户
users=$(who | wc -l)
# 当前进程数
procs=$(ps aux | wc -l)
warn=/root/warning
mkdir -p $warn
if [ $free_mem -lt 1024 ];then
echo -e "$Time,内存剩余不足1G!\n当前内存剩余:
$free_mem\n主机IP:$ip">>$warn/mem_warning
else
echo "$Time,内存剩余容量正常" >> $warn/mem_warning
fi
if [ $free_disk -lt 10 ];then
echo -e "$Time,磁盘剩余不足10G!\n当前磁盘剩余:
$free_disk\n主机IP:$ip">>$warn/disk_warning
else
echo "$Time,磁盘剩余容量正常" >> $warn/disk_warning
fi
if [ $users -gt 3 ];then
echo -e "$Time,当前用户登录过多!\n当前用户数:$users\n
主机IP:$ip">>$warn/user_warning
else
echo "$Time,登录用户正常" >> $warn/user_warning
fi
if [ $procs -gt 500 ];then
echo -e "$Time,进程数量过多!\n当前进程数:$procs\n主机
IP:$ip">>$warn/procs_warning
else
echo "$Time,进程数正常" >> $warn/procs_warning
fi
#!/bin/bash
g='\e[1;32m'
e='\e[0m'
prin(){
echo -e "$1"
}
cpu_red(){
#内核数[即线程]
cpu_processor=`grep processor /proc/cpuinfo | wc -
l`
#CPU数量[即多路]
cpu_number=`cat /proc/cpuinfo| grep "physical id"|
sort| uniq| wc -l`
#CPU物理内核
cpu_core=`cat /proc/cpuinfo | grep "cpu cores" |
sed -n '1p' | awk '{print $4}'`
#CPU型号
cpu_info=`cat /proc/cpuinfo | grep name | cut -f2
-d: | uniq -c`
}
mem_red(){
#总内存[单位MB]
total=`free -m | grep -v "Swap" | awk '{print $2}'
| sed -n "2p"`
used=`free -m | grep -v "Swap" | sed -n '2p' | awk
'{print $3}'`
free=`free -m | grep -v "Swap" | sed -n '2p'| awk
'{print $4}'`
shared=`free -m | grep -v "Swap" | sed -n '2p' |
awk '{print $5}'`
cache=`free -m | grep -v "Swap" | sed -n '2p' |
awk '{print $6}'`
}
print_cpu(){
cpu_red
echo -e "CPU物理数:${g}${cpu_number}${e}\nCPU内核
数:${g}${cpu_core}${e}\nCPU型号:${g}${cpu_info}${e}"
if [[ ${cpu_core} == ${cpu_processor} ]];then
echo -e "超线程:${g}支持${e}"
else
echo -e "超线程:${g}不支持${e}"
fi
}
print_mem(){
mem_red
d="当前主机内存信息 总内存:${g}${total}MB${e} 当前已
用:${g}${used}MB${e} 当前可用:${g}${free}MB${e} 共享占
用:${g}${shared}MB${e} 缓存占用:${g}${cache}MB${e}"
for i in $d;do
prin "$i"
done
}
print_cpu
print_mem
二、菜单脚本
#!/bin/bash
clear
echo -e "\033[33m—————————————————————————————————\033[0m"
echo -e "|\e[2;12H\033[43;30m这里是菜单\033[0m\t\t|"
echo -e "\033[33m—————————————————————————————————\033[0m"
echo -e "|\e[36m1.查看网卡信息\e[0m\t\t\t|"
echo -e "|\e[34m2.查看内存信息\e[0m\t\t\t|"
echo -e "|\e[35m3.查看磁盘信息\e[0m\t\t\t|"
echo -e "|\e[32m4.查看CPU信息\e[0m\t\t\t|"
echo -e "|\e[33m5.查看账户信息\e[0m\t\t\t|"
echo -e "\033[33m—————————————————————————————————\033[0m"
echo
read -p "请输入选项[1-5]:" key
case $key in
1)
ens=`ifconfig|head -1|awk -F':' '{print
$1}'`
ip=`ifconfig|head -2|tail -1|awk '{print
$2}'`
run=`ifconfig ens32|head -1|awk -F','
'{print $3}'`
echo -e "网卡名称:$ens\nIP地址:$ip\n启动状
态:$run"
;;
2)
a=$(free |grep Mem |tr -s " " | cut -d" "
-f7)
mem=$(echo "scale=2;$a / 1024"|bc -l)
echo "当前内存剩余容量:${mem}M"
;;
3)
b=$(df | grep /$ | tr -s " " | cut -d " "
-f4)
disk=$(echo "scale=2;$b / 1024"|bc -l)
echo "当前磁盘剩余容量:${disk}M"
;;
4)
cpu=$(uptime | tr -s " " | cut -d" " -f12)
echo "本机CPU的15分钟的平均负载为:$cpu"
;;
5)
login_num=$(who | wc -l)
total_num=$(cat /etc/passwd | wc -l)
echo "当前系统登录账户:$USER"
echo "当前登陆系统的账户数量为:$login_number"
echo "当前系统中总⽤户数量为:$total_number"
;;
*)
echo "您输入的序号有误,请重新输入!"
exit 1
;;
esac
三、nginx 启动脚本
#!/bin/bash
nginx="/usr/local/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
if [ -f $pid ];then
echo -e "\033[91mNginx运行
中...\033[0m"
exit 1
else
$nginx && echo -e "\033[32mNginx已
启动 ~\033[0m"
fi
;;
stop)
if [ ! -f $pid ];then
echo -e "\033[91mNginx未运行!
\033[0m"
exit 2
else
$nginx -s stop && echo -e
"\033[32mNginx已停止!\033[0m"
fi
;;
restart)
if [ ! -f $pid ];then
echo -e "\033[91mNginx未运行!
\033[0m"
echo -e "\033[91m请先启动Nginx!
\033[0m"
exit 3
else
$nginx -s stop && $nginx && echo -
e "\033[32mNginx已重启!\033[0m"
fi
;;
status)
if [ -f $pid ];then
echo -e "\033[32mNginx运行
中...\033[0m"
else
echo -e "\033[91mNginx未运行!
\033[0m"
fi
;;
reload)
if [ ! -f $pid ];then
echo -e "\033[91mNginx未运行!
\033[0m"
exit 4
else
$nginx -s reload && echo -e
"\033[32mNginx配置文件已重新载入...\033[0m"
fi
;;
*)
echo "输入错误!格式:$0
[start|stop|restart|status|reload]"
;;
esac
四、简易猜拳脚本
#!/bin/bash
computer=$[RANDOM%3+1]
g='\e[1;32m'
e='\e[0m'
clear
echo -e "${g}—————————————————————————————————${e}"
echo -e "${g}#\t 石头剪刀布游戏\t\t#${e}"
echo -e "${g}—————————————————————————————————${e}"
echo -e "${g}#${e} 请输入对应序号出拳 \t${g}#${e}"
echo -e "${g}—————————————————————————————————${e}"
echo -e "${g}#${e}\t\033[33m1.剪刀\033[0m\t\t\t${g}#${e}"
echo -e "${g}#${e}\t\033[35m2.石头\033[0m\t\t\t${g}#${e}"
echo -e "${g}#${e}\t\033[31m3.布\033[0m\t\t\t${g}#${e}"
echo -e "${g}—————————————————————————————————${e}"
echo
read -p "请输入序号[1-3]:" person
case $person in
1)
if [[ $computer == 1 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:剪刀
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:石头
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机赢!
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
elif [[ $computer == 2 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:剪刀
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:剪刀
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 平局
~\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
elif [[ $computer == 3 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:剪刀
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:布
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 恭喜,您获胜!
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
fi;;
2)
if [[ $computer == 1 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:石头
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:石头
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 平局
~\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
elif [[ $computer == 2 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:石头
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:剪刀
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 恭喜,您获胜!
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
elif [[ $computer == 3 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:石头
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:布
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机赢!
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
fi;;
3)
if [[ $computer == 1 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:布
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:石头
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 恭喜,您获胜!
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
elif [[ $computer == 2 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:布
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:剪刀
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机胜!
\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
elif [[ $computer == 3 ]];then
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 您出:布
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 计算机:布
\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
echo -e "${g}|\t 平局
~\t\t|${e}"
echo -e "${g}
—————————————————————————————————${e}"
fi;;
*)
echo "请正确输入出拳序号!"
exit 1
;;
esac
五、随机双色球脚本
#!/bin/bash
RED_COL='\033[91m'
BLUE_COL='\033[34m'
NONE_COL='\033[0m'
red_ball=""
while :
do
clear
echo "---机选双色球---"
tmp=$[RANDOM%33+1]
if echo "$red_ball" | grep -q -w $tmp; then
continue
fi
red_ball+=" $tmp"
echo -en "$RED_COL$red_ball$NONE_COL"
word=$(echo "$red_ball" | wc -w)
if [ $word -eq 6 ]; then
blue_ball=$[RANDOM%16+1]
echo -e "$BLUE_COL $blue_ball$NONE_COL"
break
fi
sleep 0.5
done
BLUE_COL='\033[34m'
NONE_COL='\033[0m'
# 使用关联数组来存储选中的红球号码,方便后续检查是否重复选中
declare -A red_balls
# 生成1到33的随机数,检查是否已经选中,如果没有,则添加到red_balls数
组并输出
function select_red_ball {
local num=$(( RANDOM % 33 + 1 ))
if [[ -z ${red_balls[$num]} ]]; then
red_balls[$num]="true"
echo -e "$RED_COL $num$NONE_COL"
else
# 如果已经选中,则重新选择
select_red_ball
fi
sleep 0.5
}
# 生成1到16的随机数,作为蓝球号码并输出
function select_blue_ball {
local num=$(( RANDOM % 16 + 1 ))
echo -e "$BLUE_COL $num$NONE_COL"
}
clear
echo "红球号码:"
# 主循环,持续选择红球,直到选够6个为止
while [[ ${#red_balls[@]} -lt 6 ]]; do
select_red_ball
done
echo "蓝球号码:"
sleep 0.5
# 选择蓝球号码并输出
select_blue_ball
原文地址:https://blog.csdn.net/qq_70752974/article/details/140697569
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!