shell脚本——系统工具箱(SystemToolbox)

一些想法

  1. 需要使用shell编写一个简单实用的系统工具箱脚本
  2. 一共想出了两套方案
  3. 本套综合了一些自己的想法不是很熟练大家看个乐 (^o ^ )

基本使用界面大概是这样
在这里插入图片描述

分析需要的功能

我的构思将其分成7个部分

  1. 磁盘挂载信息部分
  2. 内存使用部分
  3. CPU利用率和负载部分
  4. 网络端口信息部分
  5. 进程信息部分
  6. 磁盘每秒进程下的IO读、写请求数量部分
  7. 没有匹配项重新显示菜单部分

构建整体框架

1、定义了函数

#<-----------------------询问继续使用工具箱函数定义
whether () { read -p "Continue to use[y],sign out[n]:" play
        if [[ "$play" == "y" ]];then
        clear
        else
                echo "Thank you for your use. Have a nice day!";exit
        fi
}
#<-------------------------菜单显示函数定义
ToolboxMenu () {
echo -e "\033[5;34m ========This is a system detection toolbox============\033[0m"
echo -e "\033[33m |            1.查看磁盘挂载信息                           | \033[0m"
echo -e "\033[33m |            2.查看内存信息                              | \033[0m"
echo -e "\033[33m |            3.查看CPU信息                               | \033[0m"
echo -e "\033[33m |            4.查看网络端口信息                           | \033[0m"
echo -e "\033[33m |            5.查看进程信息                              | \033[0m"
echo -e "\033[33m |            6.磁盘每秒进程下的I0读写数量                  | \033[0m"
echo -e "\033[33m |            7.Exit Toolbox                            | \033[0m"
echo -e "\033[33m | Current Time:$(date "+%Y-%m-%d %H:%M:%S") welcome!!engineer! |\033[0m"
echo -e "\033[5;34m =============System detection toolbox=================\033[0m"
}

2、使用交互定义变量邀请用户输入

#<----------------邀请用户输入需要检测的信息
read -p "Please enter your choice![1..7]" number

3、使用while循环让工具箱一直使用
4、中间为case判断选项
5、最后一部分为没有匹配项的匹配

#<------------------没有匹配项重新显示菜单
                *)
                        echo -e "\033[31m  Error redirecting to menu for you ! \033[0m"
                ;;

着手完整代码

#!/bin/bash
#Author:Pakho
#Date:2021.5.21
#File:SystemToolbox.sh
#VersionNumber:v1.0.1
#https://blog.csdn.net/sixeleven611
#Demand:Write system toolbox
#——————————————————————————————————
#script start
#<-----------------------询问继续使用工具箱函数定义
whether () { read -p "Continue to use[y],sign out[n]:" play
        if [[ "$play" == "y" ]];then
        clear
        else
                echo "Thank you for your use. Have a nice day!";exit
        fi
}
#菜单显示函数定义
ToolboxMenu () {
echo -e "\033[5;34m ========This is a system detection toolbox============\033[0m"
echo -e "\033[33m |            1.查看磁盘挂载信息                          | \033[0m"
echo -e "\033[33m |            2.查看内存信息                             | \033[0m"
echo -e "\033[33m |            3.查看CPU信息                             | \033[0m"
echo -e "\033[33m |            4.查看网络端口信息                          | \033[0m"
echo -e "\033[33m |            5.查看进程信息                             | \033[0m"
echo -e "\033[33m |            6.磁盘每秒进程下的I0读写数量                 | \033[0m"
echo -e "\033[33m |            7.Exit Toolbox                           | \033[0m"
echo -e "\033[33m | Current Time:$(date "+%Y-%m-%d %H:%M:%S") welcome!!engineer! |\033[0m"
echo -e "\033[5;34m =============System detection toolbox=================\033[0m"
}
#<----------------邀请用户输入需要检测的信息
while :
do
        ToolboxMenu
        read -p "Please enter your choice![1..7]" number
                case $number in
#<-----------------------------------------------------------磁盘挂载信息
                1)
                                echo -e "\033[5;34m ============Partition information${z}============\033[0m"
                                df -hT
                                whether
                ;;
#<------------------------------------------------------内存使用情况
                2)
                        echo -e "\033[5;34m ============Memory usage============\033[0m"
                        free -h
                        n=1
                        for i in {1..3}
                        do
                                echo -e "\033[5;34m ============Memory usage${n}============\033[0m"
                                vmstat | awk '{if(NR==3){print "You have free memory left:" $4}}'
                                let ++n
                                sleep 1
                        done
                        whether
                ;;
#<-------------------------------------------------------CPU利用率和负载
                3)
                        v=1
                        for i in {1..3}
                        do
                                echo -e "\033[5;34m ============CPU usage${v}===========\033[0m"
                                uptime
                                vmstat | grep 2 |awk '{if (100-$15<=10){print 100-$15 "%","您的CPU很安全"}else{print 100-$15 "%","您的CPU即将满载请留意"}}'
                                let ++v
                                sleep 1
                        done
                                whether
                ;;
#<---------------------------------------------------------------网络端口信息
                4)
                        while :
                        do
                                echo -e "\033[5;34m ============Network port information============\033[0m"
                                read -p "Please enter the network port number you want to query:" Port
                                netstat -anpt | head -2
                                netstat -anpt | grep $Port
                                read -p "Do you want to check the port?[y/n]" see
                                if [[ ! $see == "y" ]];then
                                        echo "Thank you for your use. Have a nice day!";exit
                                else
                                        break
                                fi
                        done
                        whether
                ;;
#<----------------------------------------------------------------进程信息查询
                5)
                        echo -e "\033[5;34m ============Process information============\033[0m"
                        read -p "Please enter the process information you want to query:" Process
                        ps aux | grep -v grep |grep $Process
                        whether
                ;;
#<-------------------------------------------磁盘每秒进程下的IO读、写请求数量
                6)
                        b=1
                        for i in {1..3}
                        do
                                echo -e "\033[5;34m ============Number of IO read / write requests issued by the process${b}============\033[0m"
                                iostat | grep ^sd | awk '{print $1,$2}'
                                let ++b
                                sleep 1
                        done
                                whether
                ;;
#<---------------------------------退出
                7)
                        echo -e "\033[36 Thank you for your use. Have a nice day!\033[0m"
                        exit
                ;;
#<------------------没有匹配项重新显示菜单
                *)
                        echo -e "\033[31m  Error redirecting to menu for you ! \033[0m"
                ;;
        esac
done
#script end                       
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

611#

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值