极客进化岛
技术自由路

【shell案例】学员管理系统

前言

学员管理系统涉及到学员的增删改查,这是一个综合性比较强的项目,在所有的编程语言里都会有不同版本的学员信息管理系统,难度适中

效果截图

在这里插入图片描述

在这里插入图片描述

学员管理系统源码

#!/bin/bash

## 随机点名册

while true
do
    echo "学生随机点名系统"
    echo "1. 添加学员名单"
    echo "2. 遍历学员名单"
    echo "3. 随机点名"
    echo "4. 删除某个学员的信息"
    echo "5. 退出"
    read -p "请输入你要选择的序号: " num
    case $num in
    1)
    name(){
    while true
    do
        read -p "填写要添加的名字: " num1
        if [ -f namelist.txt ];then
                    name1=`grep $num1 namelist.txt`
                else
                        echo "正在键入信息请稍后..."
                        sleep 2
                        touch namelist.txt
                        name1=`grep $num1 namelist.txt`
                fi

        if [[ $num1 == $name1 ]];then
            echo "该学员已经存在!"
        else
                        echo $num1 >> namelist.txt
            read -p "是否继续添加Y|N: " num2
            if [ $num2 == "Y" ] || [ $num2 == "y" ];then
                name
                #echo "学员添加成功!"
            elif [ $num2 == "N" ] || [ $num == "n" ];then
                                echo "请重新确认学员姓名"
                break
                        else
                                 echo "你输入的字符有误,请输入Y|N"
                                 break
            fi
        fi
    done 
    }
    name
    ;;

    2)
        echo "你选择的是遍历学员名单,正在为您遍历请稍后..."
    for i in `cat namelist.txt`
    do
        echo $i
                sleep 1
    done
        echo "遍历完成" 
        total=`cat namelist.txt |wc -l`
        echo "已为您遍历 $total 个学员"
    ;;
    3)
        while(true)
        do
    line=`cat namelist.txt |wc -l`
    num3=$[RANDOM%line+1]
    sed -n "${num3}p" namelist.txt
    sleep 0.3
        read -p "是否要返回学员管理系统y/n: " input
        if [ $input  == "Y" ] || [ $input  == "y" ];then
            echo "正在返回请稍后"
            echo ''
            break
        elif [ $input == "N" ] || [ $input == "n" ];then
            echo "暂时没想到你不回学员管理系统还能干啥,要不搭建一套架构?"

        else
             echo "你输入的字符有误,请输入Y|N"
             break
        fi
        done
        clear
        echo "" 
        echo "===============学员管理系统============"
        echo "==========欢迎来到学员管理系统========="

    ;;
    4)
        while(true)
        do
    cat namelist.txt
    read -p "请输入要删除的学员: " num4
        while read line
        do
            #echo $line
            if [ $line == $num4 ];then
                sed -i "/${num4}/d" namelist.txt
    #name=`grep $num4 namelist.txt`
    #if [[ $num4 == $name ]];then
     #      sed -i "/${num4}/d" namelist.txt
        echo "删除成功!"

    #else
    #   echo " "
       fi
    done < namelist.txt
        read -p "是否返回学生系统管理首页Y|N: " back
        if [ $back == "y" ] || [ $back == "Y" ];then
            echo "正在返回请稍后"
            break
        else
            echo ""
        fi

        done

        clear
        echo "" 
        echo "===============学员管理系统============"
        echo "==========欢迎来到学员管理系统========="
        ;;
    5)
        echo "感谢您使用学员管理系统,请给五星好评哦"
    exit
    ;;
    esac

done

总结

这个脚本还可以在完善,让他更加符合我们的使用习惯,此脚本学员的第一版脚本和第二版脚本的结合,如果使用函数会更完美一些。

赞(0)
未经允许不得转载:极客进化岛 » 【shell案例】学员管理系统