linux shell编写学生选课系统
时间: 2023-07-26 20:23:11 浏览: 33
好的,以下是一个简单的学生选课系统的shell脚本示例:
```bash
#!/bin/bash
# 定义课程列表
courses=("数学" "英语" "计算机" "物理")
# 定义学生列表
students=("张三" "李四" "王五" "赵六")
# 定义选课记录
declare -A records
# 选课函数
function select_course() {
echo "请选择您要选的课程编号:"
for i in "${!courses[@]}"; do
echo "$i. ${courses[$i]}"
done
read -r course_index
if [[ ! $course_index =~ ^[0-9]+$ ]] || [[ $course_index -lt 0 ]] || [[ $course_index -ge ${#courses[@]} ]]; then
echo "无效的课程编号!"
return
fi
echo "请选择您的学号:"
for i in "${!students[@]}"; do
echo "$i. ${students[$i]}"
done
read -r student_index
if [[ ! $student_index =~ ^[0-9]+$ ]] || [[ $student_index -lt 0 ]] || [[ $student_index -ge ${#students[@]} ]]; then
echo "无效的学号!"
return
fi
key="${courses[$course_index]}-${students[$student_index]}"
if [[ ${records[$key]+_} ]]; then
echo "您已经选过此课程!"
else
records[$key]="已选"
echo "选课成功!"
fi
}
# 查看选课记录函数
function view_records() {
echo "选课记录如下:"
for key in "${!records[@]}"; do
if [[ ${records[$key]} == "已选" ]]; then
echo "$key"
fi
done
}
# 主循环
while true; do
echo "欢迎使用学生选课系统!请选择您要进行的操作:"
echo "1. 选课"
echo "2. 查看选课记录"
echo "3. 退出"
read -r choice
case $choice in
1)
select_course
;;
2)
view_records
;;
3)
echo "再见!"
exit 0
;;
*)
echo "无效的选项!"
;;
esac
done
```
这个脚本定义了一个课程列表、一个学生列表和一个选课记录,然后提供了两个操作:选课和查看选课记录。当用户选择选课时,脚本会提示用户选择课程和学生的编号,并将选课记录保存到记录中。当用户选择查看选课记录时,脚本会遍历记录并输出已选的课程和学生的组合。