第一次使用linux学一个shell脚本的菜单
前言
基于本课程所学要求设计一款程序包含是和系统有关,且不能做诸如计算器,小游戏之类的编程程序;同时代码应尽可能体现使用shell脚本编程优势。
本文依照课题老师所要求除了利用(顺序/选择/循环 三大结构)外,还可以运用所学的Linux命令、管道、重定向、命令序列、bash一些技巧、AWK/SED、正则表达式;以上所制定的一款程序,包括文件引用,邮箱通信,交互式应答,系统初始化,定时同步等程序。其中,邮箱验证系统软件的可行性和交互系统的设计与实现是本文的重难点和创新点。
本章首先阐述了菜单交互系统的核心模块构成,接着阐述了其多层次架构,分析了软件的需求,提出了基于shell的系统软件设计方案,然后介绍了Linux 技术的特点以及系统软件所用到的技术进行了介绍。
1、设计思路的流程图
csdn的图片插入太捞了,还好写的这个不会太难所以我就不插入图片了。
2、下载mailx 软件,配置发件人,这个就自行百度了(很简单)
3、下载ntp(时间)
二、源码解析
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
2.读入数据
作者注释
#!/bin/bash
# 小八嘎菜单系统
# 通过交互来执行命令
# 邮箱通信,文件调用
#
# @alexshen s1355069527@gmail.com or 1355069527@qq.com
# 版本更新记录
# 文件名 版本号 版本说明 更改日期 管理员 发行日期
#script.sh 1.1 密码登录 2022/10/30 alex -
#yan.sh 1.2 验证码系统 2022/10/30 alex 2022/10/30
#module.sh 1 mueu - zp -
#key.sh 2.1 服务器存储 2022/10/30 alex 2022/10/30
#t2.sh 2.1.2 验证码与密码结合 - alex 2022/10/31
#yan(3).sh 3.1 加入菜单 2022/10/31 alex
#pro1.sh 4.1 简单的进度条 2022/10/31 alex
#jiud1 4.2 2022/10/31 alex
#pro2.sh 4.3 2022/10/31 alex
#color.sh 4.4 改变颜色 2022/10/31 alex 2022/10/31
#mid.sh 5.1 综合版本 2022/11/1 alex 2022/11/1
#
#
#
#
#
# 版权声明@个人学习
#
#Date:2022-10-30
#version:5.1
对内存中的全局和静态变量进行赋值
pass=`cat /root/password.txt `
usemail=`cat /root/mail.txt `
2、登入前的验证系统
{
cat<<EOF
欢迎使用小八嘎系统
1.登录
2.设置
3.退出
EOF
}
2.2对输入的值进行判断选择下一步以及验证码部分
while true
do
read -ep "请选择你要执行的操作[1-3]:" num
if [ "$num" = "1" ];then
while true
do
read -s -ep "请输入您的密码: " passwd
let i++
echo "你是不是本人啊"
if [ $i -eq 3 ];then
echo "密码输入错误太多"
echo "warning someone try to break your system"|mail -s "warning" $usemail
i=0
read -ep "是否发送邮件修改密码(y/n):" x
if [ "$x" = "y" ];then
read -ep"输入邮箱修改验证:" mail
if [ `grep "$mail" /root/mail.txt|wc -l` -ne 0 ];then
echo "验证成功,正在发送"
yan=`echo $RANDOM |md5sum |cut -c 1-4`
echo "$yan" >/root/mails.txt
mail -s '验证码' $usemail </root/mails.txt
read -ep "请输入验证码:" shu
if [ "$shu" = "$yan" ];then
read -ep "验证码成功,请输入新密码:" newpass
pass=$newpass
echo $newpass > /root/password.txt
read -s -ep"请输入passwd:" passwd
else
echo "error"
break
fi
else
echo "找不到这个邮箱"
break
fi
else
break
fi
fi
登录菜单系统
if [ "$passwd" = "$pass" ];then
echo "欢迎使用"
HINT(){ #打印提示符
read -p "Press Enter tocontinue:"
}
CPU_INFO(){ #查看CPU信息
echo "begin to execute cpu_info"
echo -e "\033[4;31mPrint the CPU info:\033[0m"
cat /proc/cpuinfo |awk 'BEGIN {FS=":"} /model name/{print "CPU 型号:" $2}'
cat /proc/cpuinfo |awk 'BEGIN {FS=":"} /cpu MHz/{print "CPU 主频频率:" $2"MHz"}'
grep -Eq 'svm|vmx' /proc/cpuinfo && echo "Virtualization: Support" || echo "Virtualization: No support"
echo
}
LOAD_INFO(){ #查看系统负载
echo $(uptime)
}
MEM_INFO(){ #查看内存与交换分区信息
echo
echo -e "\033[4;31mPrint the Memory and Swap info:\033[0m"
free |grep buffers/cache|awk '{print "内存空间还剩余:"$4" Bytes"}'
free |grep Swap|awk '{print "交换分区还剩余:"$4" Bytes"}'
echo
}
DISK_INFO(){ #查看磁盘挂载信息
echo
echo -e "\033[4;31mPrint system disk space usage:\033[0m"
df -h
echo
}
while true
do
clear
echo "---------------------------------"
echo "1. 显示CPU运行:"
echo "2. 显示系统运载:"
echo "3. 显示系统内存和交换分区:"
echo "4. 显示文件系统挂载信息:"
echo "5. 退出程序 :"
echo "---------------------------------"
read -ep "请选择你要执行的操作(1-5):" U_SELECT
case $U_SELECT in
1)
CPU_INFO #通过函数名称调用函数
HINT
;;
2)
LOAD_INFO
HINT
;;
3)
MEM_INFO
HINT
;;
4)
DISK_INFO
HINT
;;
5)
exit
;;
*)
read -ep "请选择1-4, 按任意键继续:"
esac
done
fi
done
退出和设置菜单
elif [ "$num" = "3" ];then
exit
elif [ "$num" = "2" ];then
while true
do
clear
echo "-----------设置----------------------"
echo "1. 设置时区并同步时间:"
echo "2. 禁用selinux: "
echo "3. 历史命令显示操作时间:"
echo "4. 更改账号邮箱绑定:"
echo "5. 重新设置密码:"
echo "6. 退出系统:"
echo "---------------------------------"
read -ep "请选择要执行的项目(1-6):" U_SELECT
case $U_SELECT in
1)
trap 'onCtrlC' INT
function onCtrlC () {
#捕获CTRL+C,当脚本被ctrl+c的形式终止时同时终止程序的后台进程
kill -9 ${do_sth_pid} ${progress_pid}
echo
echo 'Ctrl+C is captured'
exit 1
}
do_sth() {
#运行的主程序
echo "设置完成!"
ntpdate time.windows.com
if ! crontab -l |grep ntpdate &>/dev/null ; then
(echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l)|crontab
fi
echo "现在的时间日期是:⬇"
date
}
progress() {
#进度条程序
local main_pid=$1
local length=20
local ratio=1
while [ "$(ps -p ${main_pid} | wc -l)" -ne "1" ] ; do
mark='>'
progress_bar=
for i in $(seq 1 "${length}"); do
if [ "$i" -gt "${ratio}" ] ; then
mark='-'
fi
progress_bar="${progress_bar}${mark}"
done
printf "Progress: ${progress_bar}\r"
ratio=$((ratio+1))
#ratio=`expr ${ratio} + 1`
if [ "${ratio}" -gt "${length}" ] ; then
ratio=1
fi
sleep 0.1
done
}
do_sth &
do_sth_pid=$(jobs -p | tail -1)
progress "${do_sth_pid}" &
progress_pid=$(jobs -p | tail -1)
wait "${do_sth_pid}"
printf "Progress: done \n"
read -sp "按Enter键继续:"
;;
2)
i=0
str=""
label=('|' '/' '-' '\\')
index=0
while [ $i -le 20 ]
do
let index=i%4
let jinshu=$i*5
printf "\e[47m\e[31m[%-20s]\e[0m\e[47;32m[%c]\e[1;0m\e[47;35m[%-3d%%]\e[1;0m\r" $str ${label[$index]} $jinshu
let i++
str+="#"
sleep 0.1
done
echo
sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config
echo "已禁用"
read -sp "按Enter键继续:"
;;
3)
read -ep "是否显示history时间戳(y/n):" U_SELECT1
case $U_SELECT1 in
y)
export HISTTIMEFORMAT='%F %T '
i=0
str=""
arr=("|" "/" "-" "\\")
while [ $i -le 20 ]
do
let index=i%4
let indexcolor=i%8
let color=30+indexcolor
let NUmbER=$i*5
printf "\e[0;$color;1m[%-20s][%d%%]%c\r" "$str" "$NUmbER" "${arr[$index]}"
sleep 0.1
let i++
str+='+'
done
printf "\n"
echo "已显示时间戳:"
read -ep "输入你想要多少条最近历史命令:" n
HISTFILE=~/.bash_history
set -o history
history $n
;;
n)
export HISTTIMEFORMAT=' '
i=0
str=""
arr=("|" "/" "-" "\\")
while [ $i -le 20 ]
do
let index=i%4
let indexcolor=i%4
let color=30+indexcolor
let NUmbER=$i*5
printf "\e[0;$color;1m[%-20s][%d%%]%c\r" "$str" "$NUmbER" "${arr[$index]}"
sleep 0.1
let i++
str+='+'
done
printf "\n"
echo "已隐藏时间戳"
read -ep "输入你想要多少条最近历史命令:" n
HISTFILE=~/.bash_history
set -o history
history $n
;;
*)
read -ep "请选择y or n"
;;
esac
read -sp "按Enter键继续:"
;;
4)
read -ep "是否发送邮件修改邮箱绑定 (y/n)" x
if [ "$x" = "y" ];then
read -ep"输入邮箱修改验证:" mail
if [ `grep "$mail" /root/mail.txt|wc -l` -ne 0 ];then
echo "验证成功,正在发送"
yan=`echo $RANDOM |md5sum |cut -c 1-4`
echo "$yan" >/root/mails.txt
mail -s '验证码' $usemail </root/mails.txt
read -ep "请输入验证码:" shu
if [ "$shu" = "$yan" ];then
read -ep "验证码成功,请输入新邮箱:" newmail
usemail=$newmail
echo $newmail > /root/mail.txt
read -ep"请再确认新邮箱:" mail
else
echo "验证码错误"
break
fi
else
echo "找不到这个邮箱"
break
fi
else
read -sp "按Enter键继续:"
fi
;;
5)
while true
do
read -s -ep "请输入您的密码: " passwd
let i++
if [ "$i" = "2" ];then
echo "密码输入错误超过两次"
echo "warning someone try to break your password"|mail -s "warning" $usemail
i=0
read -ep "是否发送邮件修改密码(y/n)" x
if [ "$x" = "y" ];then
read -ep"输入邮箱修改验证:" mail
if [ `grep "$mail" /root/mail.txt|wc -l` -ne 0 ];then
echo "验证成功,正在发送"
yan=`echo $RANDOM |md5sum |cut -c 1-4`
echo "$yan" >/root/mails.txt
mail -s '验证码' $usemail </root/mails.txt
read -ep "请输入验证码:" shu
if [ "$shu" = "$yan" ];then
read -ep "验证码成功,请输入新密码:" newpass
pass=$newpass
echo $newpass > /root/password.txt
read -s -ep"请输入passwd:" passwd
else
echo"验证码错误"
break
fi
else
echo "找不到这个邮箱"
break
fi
else
break
fi
fi
if [ "$passwd" = "$pass" ];then
i=0
read -ep "密码正确,请输入新密码:" newpass
pass=$newpass
echo $newpass > /root/password.txt
read -ep "修改成功,按任意键进行"
break
fi
done
;;
6)
exit
;;
*)
read -ep "请按1-6:"
;;
esac
done
fi
done
总结
后续操作将更新其他一些更加基础的系统操作指令,优化进度条等,下个版本写一个文本编辑并存储。目前,还有一些bug,正在优化,例如 -eq 这个等于问题会倒置中文配置错误等问题。我把文件到时候上传到资源里面,csdn实在写博客好累