首页 > 解决方案 > 有没有更好的方法来读取和附加 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

任何人请帮帮我。我想也许我在没有正确附加或读取文件方面犯了错误,但我现在已经搜索了所有内容,但我什至不知道错误在哪里。

标签: linuxbashshell

解决方案


您可以使用文件名数组和名称数组。

这是一个例子。确保您的脚本使用bash而不是sh, 运行,以便支持数组。

请注意,代码在索引数组时减去 1,因为数组索引在 bash 中从零开始(与许多其他语言一样),但酒店编号从 1 开始。

顺便说一句,我-necho语句中添加了禁止换行符,以便您在与提示相同的行上键入响应。

在错误检查中,我使用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

推荐阅读