图书管理系统shell
时间: 2023-11-30 18:42:33 浏览: 88
以下是一个简单的图书管理系统的Shell脚本示例,它包括图书的添加、查找、编辑、借阅和归还等功能:
```shell
#!/bin/bash
# 定义图书结构体
declare -A book
# 添加图书
function add_book() {
echo "请输入图书编号:"
read id
echo "请输入图书名称:"
read name
echo "请输入图书作者:"
read author
echo "请输入图书价格:"
read price
book[$id]=$name,$author,$price
echo "添加成功!"
}
# 查找图书
function search_book() {
echo "请输入要查找的图书编号:"
read id
if [ ${book[$id]} ]; then
echo "编号:$id,名称:${book[$id]%,*},作者:${book[$id]#*,},价格:${book[$id]##*,}"
else
echo "未找到该图书!"
fi
}
# 编辑图书
function edit_book() {
echo "请输入要编辑的图书编号:"
read id
if [ ${book[$id]} ]; then
echo "请输入新的图书名称:"
read name
echo "请输入新的图书作者:"
read author
echo "请输入新的图书价格:"
read price
book[$id]=$name,author,$price
echo "编辑成功!"
else
echo "未找到该图书!"
fi
}
# 借阅图书
function borrow_book() {
echo "请输入要借阅的图书编号:"
read id
if [ ${book[$id]} ]; then
echo "请输入借阅人姓名:"
read borrower
book[$id]=${book[$id]},$borrower
echo "借阅成功!"
else
echo "未找到该图书!"
fi
}
# 归还图书
function return_book() {
echo "请输入要归还的图书编号:"
read id
if [ ${book[$id]} ]; then
echo "请输入归还人姓名:"
read borrower
if [[ ${book[$id]} == *$borrower* ]]; then
book[$id]=${book[$id]/,$borrower/}
echo "归还成功!"
else
echo "该图书未被借阅给该人!"
fi
else
echo "未找到该图书!"
fi
}
# 删除图书
function delete_book() {
echo "请输入要删除的图书编号:"
read id
if [ ${book[$id]} ]; then
unset book[$id]
echo "删除成功!"
else
echo "未找到该图书!"
fi
}
# 主菜单
while true
do
echo "欢迎使用图书管理系统!"
echo "1.添加图书"
echo "2.查找图书"
echo "3.编辑图书"
echo "4.借阅图书"
echo "5.归还图书"
echo "6.删除图书"
echo "0.退出系统"
read choice
case $choice in
1) add_book;;
2) search_book;;
3) edit_book;;
4) borrow_book;;
5) return_book;;
6) delete_book;;
0) exit;;
*) echo "输入错误,请重新输入!";;
esac
done
```
阅读全文