【发布时间】:2012-01-25 13:42:30
【问题描述】:
我的程序应该可以这样工作。
以下是名为 BookDB.txt 的文本文件的内容 个人用冒号(:)分隔,文本文件中的每一行都应作为一组信息,并按如下所述的顺序排列。
标题:作者:价格:QtyAvailable:QtySold
Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
我实际上打算 1)逐行读取文件并将其存储在数组中 2) 显示它
但是我什至不知道如何开始第一个。 从网上研究,下面是我写到现在的代码。
#!/bin/bash
function fnReadFile()
{
while read inputline
do
bTitle="$(echo $inputline | cut -d: -f1)"
bAuthor="$(echo $inputline | cut -d: -f2)"
bPrice="$(echo $inputline | cut -d: -f3)"
bQtyAvail="$(echo $inputline | cut -d: -f4)"
bQtySold="$(echo $inputline | cut -d: -f5)"
bookArray[Count]=('$bTitle', '$bAuthor', '$bPrice', '$bQtyAvail', '$bQtySold')
Count = Count + 1
done
}
function fnInventorySummaryReport()
{
fnReadFile
echo "Title Author Price Qty Avail. Qty Sold Total Sales"
for t in "${bookArray[@]}"
do
echo $t
done
echo "Done!"
}
if ! [ -f BookDB.txt ] ; then #check existance of bookdb file, create the file if not exist else continue
touch BookDB.txt
fi
"HERE IT WILL THEN BE THE MENU AND CALLING OF THE FUNCTION"
感谢那些提前提供帮助的人!
【问题讨论】:
-
我刚刚发布的这段代码有错误。
-
什么错误?如果您将它们添加到问题中,我们将更容易为您提供帮助。
-
第 12 行:bookArray[Count]:无法将列表分配给数组成员
-
这一行出现错误 > bookArray[Count]=('$bTitle', '$bAuthor', '$bPrice', '$bQtyAvail', '$bQtySold')
-
bash 没有多维数组。如果这是您真正想要的,请使用具有更灵活数据结构的语言,例如 Perl。
标签: linux arrays shell unix ubuntu