Question : Bash script help

Hi ,
Basically im trying to write a bash shell script for a simple Point Of Sale system, it is an interactive menu system allowing the option of

1)Add new sales transaction
2)Remove sales transaction
3)Edit sales entries
4)Inquire sales transaction
5)Sales summary report
6)Quit

Any new sales transaction will be stored in a text file.

The part i am having problem is with option 5 - Sales summary report ,
it should display something similar to the display below

Date                  Total Sales
24Mar09            $350
25 Mar09            $238
26 Mar09            $451
27 Mar09            $239  
Grand Total                  $1278

But it seems only the dates are displayed but not the Total Sales and Grand total

I have attached my files , could someone please help me with this problem


Answer : Bash script help

Your problem is in print_dtotal in menu.sh.

You had a reference to f4 which shoudl have been f3, and you were missing the bits which actually added the total up.  Also the summary .txt you supplied was a bit confused - 2 of the entries were missing the date, and one had the date in a different format (lower-case letter on the month, and a 4-digit year, whereas the others had upper-case initial letter on the month and 2-digit year - either will do, but you must be consistent!).

a corrected print_dtotal is:

print_dtotal()
{
count=0
grep -i : $BOOK | cut -d: -f4 | sort -u | while read i
do
  dtotal=0
  n=`num_lines ":$i"`
  grep -i ":${i}$" $BOOK | while read x
  do
    count=`expr $count + 1`
    temp1=`echo $x | cut -d: -f2`
    temp2=`echo $x | cut -d: -f3`
    dtotal=`expr $dtotal + \( $temp1 \* $temp2 \)`
    if [ "${n}" -eq "${count}" ]; then
      echo $i" $"$dtotal
    fi
  done
done

I'm not sure what the following is doing in summary_item() either:
#print_gtotal
echo "---------------------------"
print_dtotal | sort -f4| while read z
do
echo $z | cut -f4 -d' '
return 0

print_gtotal isn't referred to elsewhere, and the "sort -f4" line doesn't make sense.  Just delete these lines.
Random Solutions  
 
programming4us programming4us