基于 Shell 命令的管理系统

学校这学期末,添加了一门《计算机操作系统》的课程。
课程需要完成项目《基于Shell命令的管理系统》

Main.sh 主控脚本

文件名:main.sh

#!/bin/bash

while true; 
do
    echo "----------------------------"
    echo "      欢迎使用本系统         "
    echo "----------------------------"
    echo -e "\e[2;37m The script : 0 ==>" "\e[0m applyMange.sh 应用管理"
    echo -e "\e[2;37m The script : 1 ==>" "\e[0m backupMange.sh 备份管理"
    echo -e "\e[2;37m The script : 2 ==>" "\e[0m fileMange.sh 文件管理"
    echo -e "\e[2;37m The script : exit ==>" "\e[0m 退出系统           "
    
    read -p "请输入操作选项: [ | 0 | 1 | 2 | exit | ]: " choice

    case $choice in
        0)
            /bin/bash ./applyManage_catchyun.sh
            ;;
        1)
            /bin/bash ./backupManage_catchyun.sh
            ;;
        2)
            /bin/bash ./fileManage_catchyun.sh
            ;;
        "exit")
            echo "退出系统成功"
            break
            ;;
        *)
            echo "输入不合法,请重新输入!"
            ;;
    esac
done

FileManage_CatchYun.sh 文件管理脚本

文件名:fileManage_catchyun.sh

#! /bin/bash
while true
do
    # 打印菜单
    echo "     文件管理              "
    echo "--------------------------"
    echo " 1.查看目录                "
    echo "--------------------------"
    echo " 2.查看文件内容            "
    echo "--------------------------"
    echo " 3.删除文件                "
    echo "--------------------------"
    echo " 4.检查文件是否存在         "
    echo "--------------------------"
    echo " 5.移除/重命名文件        "
    echo "--------------------------"
    echo " 6.文件搜索               "
    echo "--------------------------"
    echo " 7.修改文件权限           "
    echo "--------------------------"
    echo " 8.修改文件属主与属组       "
    echo "--------------------------"
    echo " 9.返回主菜单              "
    echo "--------------------------"
    echo " 0.退出                   "

    # 读入用户输入
    read -p "请输入操作选项: " chooice

    # 选择相应功能 / ;; 代表break / case 代表开始 反写 esac 代表结束 {}作用
    case $chooice in 
    1)
        read -p "请输入你要查看的目录: " dirname 
        ls -l $dirname ;;
    2)
        read -p "请输入要查看文件名称: " filename
        cat $filename ;;
    3)
        read -p "请输入要删除的文件名称: " delfilename
        rm -r $delfilename
        if [ $? = 0 ]  # 0表示真,1表示假
        then
            echo "文件删除成功"
        else
            echo "文件删除失败"
        fi;;
    4)
        read -p "请输出要检查的文件名称: " detfilename
        if test -e $detfilename
        then 
            echo "文件存在"
        else
            echo "文件不存在"
        fi;;
    5)
        read -p "请输入要移除的源文件名称: " oldfilename
        read -p "请输入目标文件: " newfilename
        sudo mv $oldfilename $newfilename
        if [ $? = 0 ]
        then
            echo "文件移动/重命名成功"
        else
            echo "文件移动/重命名失败"
        fi ;;
    6)
        read -p "请输入查找路径: " findroad
        read -p "请输入所要查找的文件名称: " findfilename
        find $findroad -name \*${findfilename}* -print # 也可用-ls
        echo "Key is: " $findfilename ;;
    7)
        read -p "输入所要修改权限的文件名称: " chfilename
        read -p "请输入所要修改的权限【数字设定法】: " chnum
        sudo chmod $chnum $chfilename
        if [ $? = 0 ]
        then 
            echo "文件权限修改成功"
        else
            echo "文件权限修改失败"
        fi;;
    8)
        read -p "请输入要修改的文件名称: " groupfilename
        read -p "请输入新的属主: " host
        read -p "请输入新的属组: " group 
        sudo chown $host:$group $groupfilename
        if [ $? = 0 ]
        then 
            echo "文件修改成功"
        else
            echo "文件修改失败"
        fi;;
    9)
        /bin/bash ./main.sh
        exit 0
        ;;
    0)
        exit 0
        ;;  #正常关闭为0 ,非正常为1
    *)
        echo "您的输入有误,请重新输入"
    esac
done

AppleManage_CatchYun.sh 应用管理脚本

文件名:appleManage_catchyun.sh

#!/bin/bash

while true; 
do
    echo "     应用管理              "
    echo "--------------------------"
    echo " 1. RPM安装管理"
    echo "--------------------------"
    echo " 2. YUM安装管理"
    echo "--------------------------"
    echo " 3. 返回主界面"
    echo "--------------------------"
    echo " 0. 结束整个系统"
    echo "--------------------------"

    read -p "请输入选项: " choice

    case $choice in
        1)
            # RPM安装管理菜单
            while true;
            do
                echo "     RPM安装管理           "
                echo "--------------------------"
                echo " 1. 安装软件"
                echo "--------------------------"
                echo " 2. 卸载软件"
                echo "--------------------------"
                echo " 3. 更新软件"
                echo "--------------------------"
                echo " 4. 查看已安装的软件"
                echo "--------------------------"
                echo " 5. 返回应用管理菜单"
                echo "--------------------------"
                read -p "请输入选项: " rpm_choice

                case $rpm_choice in
                    1)
                        read -p "请输入要安装的RPM包所在路径: " rpm_path
                        read -p "请输入要安装的软件名称: " rpm_package
                        sudo rpm -ivh "$rpm_path/$rpm_package.rpm"
                        if [ $? -eq 0 ]; then
                            echo -e "\e[32m安装成功\e[0m"
                        else
                            echo -e "\e[31m安装失败\e[0m"
                        fi
                        ;;
                    2)
                        read -p "请输入要卸载的软件名称: " rpm_package
                        sudo rpm -e "$rpm_package"
                        if [ $? -eq 0 ]; then
                            echo -e "\e[32m卸载成功\e[0m"
                        else
                            echo -e "\e[31m卸载失败\e[0m"
                        fi
                        ;;
                    3)
                        read -p "请输入要更新的RPM包所在路径: " rpm_path
                        read -p "请输入要更新的软件名称: " rpm_package
                        sudo rpm -Uvh "$rpm_path/$rpm_package.rpm"
                        if [ $? -eq 0 ]; then
                            echo -e "\e[32m更新成功\e[0m"
                        else
                            echo -e "\e[31m更新失败\e[0m"
                        fi
                        ;;
                    4)
                        read -p "请输入要查询的软件名称: " rpm_package
                        sudo rpm -q "$rpm_package"
                        if [ $? -eq 0 ]; then
                            echo -e "\e[32m查询成功\e[0m"
                        else
                            echo -e "\e[31m查询失败\e[0m"
                        fi
                        ;;
                    5)
                        break
                        ;;
                    *)
                        echo "无效选项,请重新输入。"
                        ;;
                esac
            done
            ;;
        2)
            # YUM安装管理菜单
            while true; do
                echo "       YUM安装管理         "
                echo "--------------------------"
                echo " 1. 安装软件"
                echo "--------------------------"
                echo " 2. 卸载软件"
                echo "--------------------------"
                echo " 3. 更新软件"
                echo "--------------------------"
                echo " 4. 查看已安装的软件"
                echo "--------------------------"
                echo " 5. 返回应用管理菜单"
                echo "--------------------------"
                read -p "请输入选项: " yum_choice

                case $yum_choice in
                    1)
                        read -p "请输入要安装的软件名称: " yum_package
                        sudo yum install "$yum_package" -y
                        ;;
                    2)
                        read -p "请输入要卸载的软件名称: " yum_package
                        sudo yum remove "$yum_package" -y
                        ;;
                    3)
                        read -p "请输入要更新的软件名称: " yum_package
                        sudo yum update "$yum_package" -y
                        ;;
                    4)
                        read -p "请输入要查询的软件名称: " yum_package
                        sudo yum list installed | grep "$yum_package" -y
                        ;;
                    5)
                        break
                        ;;
                    *)
                        echo "无效选项,请重新输入。"
                        ;;
                esac
            done
            ;;
        3)
            /bin/bash ./main.sh
            exit 0
            ;;
        0)
            exit 0
            ;;
        *)
            echo "无效选项,请重新输入。"
            ;;
    esac
done

BackUpManage_CatchYun.sh 备份管理脚本

文件名:backupManage_catchyun.sh

#!/bin/bash

while true; 
do
    echo "     备份管理              "
    echo "--------------------------"
    echo " 1.备份文件                "
    echo "--------------------------"
    echo " 2.备份目录                "
    echo "--------------------------"
    echo " 3.制定计划任务            "
    echo "--------------------------"
    echo " 4.查看计划任务            "
    echo "--------------------------"
    echo " 5.返回主菜单              "
    echo "--------------------------"
    echo " 0.退出                   "

    read -p "请输入选项: " choice

    case $choice in
        1)
            # 备份文件菜单
            while true; do
                echo "     备份文件              "
                echo "--------------------------"
                echo " 1.压缩文件                "
                echo "--------------------------"
                echo " 2.解压文件                "
                echo "--------------------------"
                echo " 3.返回备份管理菜单         "
                echo "--------------------------"
                read -p "请输入选项: " file_choice

                case $file_choice in
                    1)
                         read -p "请输入要压缩的文件路径: " file_path
                        if [ -e "$file_path" ]; 
                        then
                            read -p "请输入要压缩的文件名称: " file_name
                            tar -czvf "${file_path}/${file_name}.tar.gz" "${file_path}/${file_name}"
                            echo "压缩成功!"
                        else
                            echo "文件不存在,请重新输入。"
                        fi
                        ;;
                    2)
                         read -p "请输入要解压的文件路径: " compressed_file_path
                        if [ -e "$compressed_file_path" ]; then
                            read -p "请输入要解压的文件名称: " compressed_file_name
                            tar -xzvf "${compressed_file_path}/${compressed_file_name}.tar.gz"
                            echo "解压成功!"
                        else
                            echo "压缩文件不存在,请重新输入。"
                        fi
                       
                        ;;
                    3)
                        break
                        ;;
                    *)
                        echo "无效选项,请重新输入。"
                        ;;
                esac
            done
            ;;
        2)
            # 备份目录菜单
            while true; do
                echo "     备份目录              "
                echo "--------------------------"
                echo " 1.压缩目录                "
                echo "--------------------------"
                echo " 2.解压目录                "
                echo "--------------------------"
                echo " 3.返回备份管理菜单         "
                echo "--------------------------"
                read -p "请输入选项: " dir_choice

                case $dir_choice in
                    1)
                        read -p "请输入要压缩的目录路径: " dir_path
                        if [ -e "$dir_path" ]; 
                        then
                            read -p "请输入要压缩的文件名称: " dir_name
                            tar -czvf "${dir_path}/${dir_name}.tar.gz" "$dir_path"
                            echo "压缩成功!"
                        else
                            echo "目录不存在,请重新输入。"
                        fi
                        ;;
                    2)
                        read -p "请输入要解压的目录路径: " compressed_dir_path
                        if [ -e "$compressed_dir_path" ]; 
                        then
                            read -p "请输入要解压的文件名称: " compressed_dir_name
                            tar -xzvf "${compressed_dir_path}/${compressed_dir_name}.tar.gz"
                            echo "解压成功!"
                        else
                            echo "压缩目录不存在,请重新输入。"
                        fi
                        ;;
                    3)
                        break
                        ;;
                    *)
                        echo "无效选项,请重新输入。"
                        ;;
                esac
            done
            ;;
        3)
            # 制定计划任务
            echo "进入crontab编辑界面,请添加计划任务。"
            crontab -e
            ;;
        4)
            # 查看当前计划任务
            echo "当前计划任务:"
            crontab -l
            ;;
        5)
            /bin/bash ./main.sh
            exit 0
            ;;
        0)
            # 退出系统
            echo "退出系统。"
            exit 0
            ;;
        *)
            echo "无效选项,请重新输入。"
            ;;
    esac
done

就是一个基于Shell命令的简单脚本,此博文仅用于学习记录。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共11条

请登录后发表评论

    • 头像王光卫博客0
    • 头像Echo0
    • 头像0
    • 头像团团0
    • 头像obaby0