假設我正在使用我的程序中的一個選項。 我有一個文本文件,其中包含以下內容。2個變量的乘法
格式爲Title:Author:Price: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
下面是我的函數:
printf "%-22s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price","Qty Avail.", "Qty Sold", "Total Sales"
grep "$1" BookDB.txt | while IFS=: read Title Author Price QtyAvailable QtySold; do
printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f\n", "$Title" "$Author" "$Price" "$QtyAvailable" "$QtySold"
done
的問題是我需要另一列被稱爲總銷售額,乘以Price
和QtySold
計算。不過,我嘗試過不同的方式,例如$Price * $QtySold
或$3 * $5
,但該程序仍然無法爲我計算出來。哪個應該是解決這個問題的正確方法或方法?
這是你使用,bash shell的? – stdcall
@Mellowcandle是的,我使用bash。 – Andres
@andres請記住,儘管今天你使用的是bash,但確保你的代碼是可移植的sh是非常好的做法。 Bash提供了一些(次要的)方便,但它們只能用於命令行。 –