Shellscript数组问题

时间:2018-01-26 17:02:57

标签: arrays bash shell sh

基于我对shell脚本的有限理解,我决定使用数组来更容易排序。但是,每当我调用该函数时,它都会打印出同一本书的多个副本。例如,

在BookDB.txt内

Title1:Author1:Price1:X1:Y1
Title2:Author2:Price2:X2:Y2

在调用任一功能后在我的终端内

Title1:Author1:Price1:X1:Y1
Title1:Author1:Price1:X1:Y1
Title1:Author1:Price1:X1:Y1
Title2:Author2:Price2:X2:Y2
Title2:Author2:Price2:X2:Y2
Title2:Author2:Price2:X2:Y2

我在这里做错了什么?它不断打印掉它的多个“实例”,偶尔会保存在我的文本文件中,导致我的文本文件也包含多个“实例”(我还没有确认是什么原因造成的,因为它有时只会发生)。

我附上了功能代码的主要部分。 $FILENAME的值为 ./ BookDB.txt 。提前感谢您的专业知识和帮助。

功能inventory_summary_report()

inventory_summary_report()
{
access_bookdb_data
echo "--------------------------------------------"
echo "<INVENTORY SUMMARY REPORT>"   
echo ""

printf "%-35s\t%-15s\t%-10s\t%-5s\t%-5s\t%-10s\n" \
   "Title" "Author" "Price" "Qty Avail" "Qty Sold" "Total Sales"
echo ""

#Prints all data from database
for ((i=0; $i<${#TITLE[@]}; i++)); do
    printf "%-35s\t%-15s\t$%-0.2f\t\t%-3d\t\t%-3d\t\t$%-0.2f\n" \
           "${TITLE[$i]}" "${AUTHOR[$i]}" "${PRICE[$i]}" "${AVAIL[$i]}" \ 
           "${SOLD[$i]}" "$(echo ${PRICE[$i]}*${SOLD[$i]} | bc)"
done

echo "--------------------------------------------"
}

功能report_books_statistics()

report_books_statistics()
{
access_bookdb_data
echo "--------------------------------------------"
echo "<BOOK STATISTICS REPORT>"
echo ""

tempSoldArr=("${SOLD[@]}");
tempTitleArr=("${TITLE[@]}");
tempAuthorArr=("${AUTHOR[@]}");
tempAvailArr=("${AVAIL[@]}");

#Sorts numbers in descending array
for ((last=numRec-1; last>0; last--)); do
    for ((i=0; i<last; i++)); do
        j=$((i+1))
        if [ ${tempSoldArr[i]} - lt ${tempSoldArr[j]} ]; then
            tempS=${tempSoldArr[$i]}
            tempSoldArr[$i]=${tempSoldArr[$j]}
            tempSoldArr[$j]=$tempS
            tempT=${tempTitleArr[$i]}
            tempTitleArr[$i]=${tempTitleArr[$j]}
            tempTitleArr[$j]=$tempT
            tempA=${tempAuthorArr[$i]}
            tempAuthorArr[$i]=${tempAuthorArr[$j]}
            tempAuthorArr[$j]=$tempA
            tempAvail=${tempAvailArr[$i]}
            tempAvailArr[$i]=${tempAvailArr[$j]}
            tempAvailArr[$j]=$tempAvail
        fi
    done
done

#Shows most popular books
echo "The Top 5 Books Sold are"
echo "--------------------------------------------"
printf "%-35s\t%-15s\t%-5s\n" "Title" "Author" "Qty Sold"
echo ""

#Descending order
for ((i=0; i<5; i++)); do
printf "%-35s\t%-15s\t%-5s\n" "${tempTitleArr[$i]}" "${tempAuthorArr[$i]}" "${tempSoldArr[i]}"  
done

echo ""
echo ""

#Least popular books
echo "The Least 5 Books Sold are"
echo "--------------------------------------------"
printf "%-35s\t%-15s\t%-5s\n" "Title" "Author" "Qty Sold"
echo ""

#Ascending order
for ((i=numRec-1; i>numRec-5; i--)); do
    printf "%-35s\t%-15s\t%-5s\n" "${tempTitleArr[$i]}" "${tempAuthorArr[$i]}" "${tempSoldArr[i]}"  
done

echo ""
echo ""

#Books with <20 quantity available
echo "Books with quantity available lesser than 20"
echo "--------------------------------------------"
printf "%-35s\t%-15s\t%-5s\n" "Title" "Author" "Qty Left"
echo ""

for ((i=0; i<numRec; i++)); do
    if [[ ${tempAvailArr[i]} -lt 21 ]]; then
        printf "%-35s\t%-15s\t%-5s\n" "${tempTitleArr[$i]}" \
               "${tempAuthorArr[$i]}" "${tempAvailArr[i]}"
    fi
done

echo "--------------------------------------------"
}

功能access_bookdb_data()

access_bookdb_data()
{
#Creates BookDB.txt if it does not exist
if ! [ -f $FILENAME ] ; then
    touch $FILENAME
fi

#Sort file and replace it
sort $FILENAME -o $FILENAME

#Reads sorted data and store into array
while IFS=':' read -a LINE; do
    TITLE[$COUNT]=${LINE[0]};
    AUTHOR[$COUNT]=${LINE[1]};
    PRICE[$COUNT]=${LINE[2]};
    AVAIL[$COUNT]=${LINE[3]};
    SOLD[$COUNT]=${LINE[4]};
    let COUNT++;
done<$FILENAME
}

0 个答案:

没有答案