linux - 有没有更好的方法来读取和附加 bash 脚本中的文件
问题描述
基本上,我正在做一个项目,我正在用 Bash 脚本制作酒店管理系统。一切正常,但我在将数据附加到文件然后读取(打印)它们时遇到问题。每个单独的酒店总共有 5 个文件。问题是无论我做什么,数据只会保存在同一个文件中,并且无论调用哪个文件都会打印同一个文件。 我已经将归档代码分开了,这样更容易专注于主要问题。
filesave(){
$name, $totalp, $id, $hnum;
#Input Details
echo "Enter your name: "
read name
echo "Enter the total number of people: "
read totalp
#Assigning Random ID
id=$RANDOM
#Selecting file
echo "Enter hotel number"
read hnum
if [[ hnum=="1" ]]; then
echo "Customer ID: $id " >> Ramada.txt
echo "Customer Name: $name " >> Ramada.txt
echo "Total Number of People: $totalp " >> Ramada.txt
echo
#
elif [[ hnum=="2" ]]; then
echo "Customer ID: $id " >> Tm.txt
echo "Customer Name: $name " >> Tm.txt
echo "Total Number of People: $totalp " >> Tm.txt
echo
#
elif [[ hnum=="3" ]]; then
echo "Customer ID: $id " >> Mehran.txt
echo "Customer Name: $name " >> Mehran.txt
echo "Total Number of People: $totalp " >> Mehran.txt
echo
#
elif [[ hnum=="4" ]]; then
echo "Customer ID: $id " >> Mp.txt
echo "Customer Name: $name " >> Mp.txt
echo "Total Number of People: $totalp " >> Mp.txt
echo
#
elif [[ hnum=="5" ]]; then
echo "Customer ID: $id " >> PC.txt
echo "Customer Name: $name " >> PC.txt
echo "Total Number of People: $totalp " >> PC.txt
echo
fi
echo "ROOM SUCCESSFULLY BOOKED!!! "
echo "Enjoy your stay"
}
showdata(){
$selecth;
echo "Enter Hotel Number: "
read selecth
if [[ selecth=="1" ]]; then
clear
echo "Processing request..."
echo "Hotel: Ramada"
cat Ramada.txt
#
#
elif [[ selecth=="2" ]]; then
clear
echo "Processing request..."
echo "Hotel: Taj Mahal"
cat Tm.txt
#
#
elif [[ selecth=="3" ]]; then
clear
echo "Processing request..."
echo "Hotel: MEHRAN"
cat Mehran.txt
#
#
elif [[ selecth=="4" ]]; then
clear
echo "Processing request..."
echo "Hotel: Move and Pick"
cat Mp.txt
#
#
elif [[ selecth=="5" ]]; then
clear
echo "Processing request..."
echo "Hotel: Pearl Continental"
cat PC.txt
#
#
fi
}
#Main
filesave
echo "Now u can view your data by entering the hotel number"
showdata
任何人请帮帮我。我想也许我在没有正确附加或读取文件方面犯了错误,但我现在已经搜索了所有内容,但我什至不知道错误在哪里。
解决方案
您可以使用文件名数组和名称数组。
这是一个例子。确保您的脚本使用bash
而不是sh
, 运行,以便支持数组。
请注意,代码在索引数组时减去 1,因为数组索引在 bash 中从零开始(与许多其他语言一样),但酒店编号从 1 开始。
顺便说一句,我-n
在echo
语句中添加了禁止换行符,以便您在与提示相同的行上键入响应。
在错误检查中,我使用exit 1
了如果您输入了无效的选择(脚本将终止)。或者,您可以拥有return 1
并且该函数将返回状态 1(指示错误),但脚本将继续。
#!/bin/bash
files=(Ramada.txt Tm.txt Mehran.txt Mp.txt PC.txt)
names=(Ramada "Taj Mahal" MEHRAN "Move and Pick" "Pearl Continental")
filesave(){
#Input Details
echo -n "Enter your name: "
read name
echo -n "Enter the total number of people: "
read totalp
#Assigning Random ID
id=$RANDOM
#Selecting file
echo -n "Enter hotel number: "
read hnum
filename=${files[$hnum-1]}
if [ -z "$filename" ]
then
echo "Hotel number not recognised" >&2
exit 1
fi
echo "Customer ID: $id " >> $filename
echo "Customer Name: $name " >> $filename
echo "Total Number of People: $totalp " >> $filename
echo "ROOM SUCCESSFULLY BOOKED!!! "
echo "Enjoy your stay"
}
showdata(){
echo -n "Enter Hotel Number: "
read selecth
hotel_name=${names[$selecth-1]}
filename=${files[$selecth-1]}
if [ -z "$filename" ]
then
echo "Hotel number not recognised" >&2
exit 1
fi
clear
echo "Processing request..."
echo "Hotel: $hotel_name"
cat $filename
}
#Main
filesave
echo "Now u can view your data by entering the hotel number"
showdata
推荐阅读
- google-apps-script - 如何在运行时删除谷歌表单的表单响应?
- python - 在 python 中使用递归来组合可变大小的列表
- excel - 数字的 QUOTIENT 返回无效值
- javascript - npm install - 为什么 node_modules 中的 javascript 代码在 es5 中
- java - JEE Restful 将 JSON 绑定到 JAVA
- java - 单击后退按钮时未选中上一页的单选按钮
- server - Window 服务器许可证的有效期仅为 52 天。52天后会发生什么?
- postgresql - 保险库 - AWS RDS Postgres 集成
- asp.net - asp webforms 文本框验证最多 5 个整数和 *
- r - 使用 R 将 GPX 文件转换为 shapefile