•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,450 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,613 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 3740 | Replies: 23 | Solved
![]() |
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
I changed some parts of the script to relect your changes...what is the best way to debug the script?
Another thing i was wondering is for the lines that calculate each amount etc - could you add comments as to what each line does ,,, im still trying to learn basic scripting and some of the things such as:
echo "scale=2; $PRICE*QUANT" | bc
Any help? Thanks once again
**********EDIT************
I changed the PRICE line to this:
And now it gets the correct price ... before it wasnt getting anything...just a blank...because the field seperator of file 'products' is a command (,) and there is only 3 fields, PROD-ID, NAME, PRICE
Now (with your awesome help) we have got it working in terms of each associate and their sales per product - i now should just need to add an array maybe for total[ASSOCIATE-ID]=total[ASSOCIATE-ID]+$price
Would this work? Ill try it and let you know --- lol.
Thanks folks!
Another thing i was wondering is for the lines that calculate each amount etc - could you add comments as to what each line does ,,, im still trying to learn basic scripting and some of the things such as:
echo "scale=2; $PRICE*QUANT" | bc
Any help? Thanks once again
**********EDIT************
I changed the PRICE line to this:
PRICE=`awk /^$PRODID/ products|awk -F":" '{print $3}'`Now (with your awesome help) we have got it working in terms of each associate and their sales per product - i now should just need to add an array maybe for total[ASSOCIATE-ID]=total[ASSOCIATE-ID]+$price
Would this work? Ill try it and let you know --- lol.
Thanks folks!
Last edited by tones1986 : Dec 3rd, 2007 at 3:32 pm. Reason: edited what i tried so far....little more pushing needed
•
•
Join Date: Oct 2007
Posts: 302
Reputation:
Rep Power: 3
Solved Threads: 37
Well, in basic bash scripting you cannot add floating point numbers, and i assume your price is floating point.
So in order to add those numbers you need the bc command to do this, hence the command
do a man bc on your system and it'll give you all the details. The scale=n, means n places after the decimal.
The best way to debug it, for me atleast is run it through one loop at a time with print statements, so that you can see what values are being assigned to your variables and what is being computed.
You should be able to use an array to add up the prices for each associate.
So in order to add those numbers you need the bc command to do this, hence the command
TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
do a man bc on your system and it'll give you all the details. The scale=n, means n places after the decimal.
The best way to debug it, for me atleast is run it through one loop at a time with print statements, so that you can see what values are being assigned to your variables and what is being computed.
You should be able to use an array to add up the prices for each associate.
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
After playing around with my script some i have come up with something like this:
( I am having problem in figuring out where i want to be printing the names ... under which loop - also can i use an array to store the GRANDTOTALS for each associate? --- if so i could then do a basic loop at the bottom of the main script that will print out the results as i want them:
NAME POSITION SALES
John Doe Clerk 284.74
George Bush President 1059.67
... ... ..........
If i were able to get my names and positions and total sales into arrays, i could use a simple loop like:
Would something like this be possible - and how could i get this working under my current script/code as i am unsure where to write assignment statements into the arrays as the 2 looping statements in the main block of script is confusing to me.
Here my current script (NOTE: I have part of commented out lines of code etc all over because im trying to figure out where to play assignment statements for my array(names) and how best to print out the information.
/home/lx/z109079 : ./newtest.sh
SALES FOR ASSOCIATE 21
SALES FOR ASSOCIATE 22
SALES FOR ASSOCIATE 23
SALES FOR ASSOCIATE 24
SALES FOR ASSOCIATE 25
SALES FOR ASSOCIATE 26
NAME[1]: John Doe
George Bush
Susan Worker
Fast Buck
Hillary Clinton
Dennis Miller
Thanks once again for any help. Always much appreciated...more than u can know
( I am having problem in figuring out where i want to be printing the names ... under which loop - also can i use an array to store the GRANDTOTALS for each associate? --- if so i could then do a basic loop at the bottom of the main script that will print out the results as i want them:
NAME POSITION SALES
John Doe Clerk 284.74
George Bush President 1059.67
... ... ..........
If i were able to get my names and positions and total sales into arrays, i could use a simple loop like:
while ["$ID -le 6 ]
do
echo "${NAMES[$ID]} ${POSITION[$ID]} ${TOTALSALES[$ID]}"
ID=$ID+1
doneWould something like this be possible - and how could i get this working under my current script/code as i am unsure where to write assignment statements into the arrays as the 2 looping statements in the main block of script is confusing to me.
Here my current script (NOTE: I have part of commented out lines of code etc all over because im trying to figure out where to play assignment statements for my array(names) and how best to print out the information.
#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales
#cat /tmp/newSales
ID=1
NAMES=()
while [ $ID -le 6 ]
do
awk /[2][$ID]$/ /tmp/newSales > /tmp/sales.2$ID
let ID=$ID+1
done
GRANDTOTAL=0
ID=1
while [ $ID -le 6 ]
do
echo "SALES FOR ASSOCIATE 2$ID"
while read line
do
PRODID=`echo $line|cut -d"," -f1`
QUANT=`echo $line|cut -d"," -f2`
PRICE=`awk /^$PRODID/ products|awk -F":" '{print $3}'`
TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
GRANDTOTAL=$(echo "scale=2; $GRANDTOTAL+$TOTAL" | bc)
done < "/tmp/sales.2$ID"
NAMES[$ID]=`awk -F"/" '{print $2}' associates`
# echo "NAME[$ID]: ${NAMES[$ID]}"
# echo Grand total for 2$ID: $GRANDTOTAL
GRANDTOTAL=0
let ID=$ID+1
done
echo "NAME[$ID]: ${NAMES[$ID]}" #this is just an attempt to print the names one at a time
ID=1
while ["$ID" -le 6]
do
echo "2$ID ${NAMES[$ID]} TOTAL (LATER )"
((ID+=1))
done
# removes all created files
rm /tmp/sales.2*/home/lx/z109079 : ./newtest.sh
SALES FOR ASSOCIATE 21
SALES FOR ASSOCIATE 22
SALES FOR ASSOCIATE 23
SALES FOR ASSOCIATE 24
SALES FOR ASSOCIATE 25
SALES FOR ASSOCIATE 26
NAME[1]: John Doe
George Bush
Susan Worker
Fast Buck
Hillary Clinton
Dennis Miller
Thanks once again for any help. Always much appreciated...more than u can know
•
•
Join Date: Oct 2007
Posts: 302
Reputation:
Rep Power: 3
Solved Threads: 37
You have the correct idea, but a couple of things.
1. When you extract the name from the associates file you also need to use the ID as a pattern to get the correct name.
2. You can declare the arrays to store your total sales and your names as follows
and then assign the values to them in the main loop, once for each ID. You could have a separate loop to print them out or you could just print them in the main while loop, before you increment ID. Its upto you.
1. When you extract the name from the associates file you also need to use the ID as a pattern to get the correct name.
2. You can declare the arrays to store your total sales and your names as follows
declare -a totalsales declare -a names
and then assign the values to them in the main loop, once for each ID. You could have a separate loop to print them out or you could just print them in the main while loop, before you increment ID. Its upto you.
totalsales[$ID]=$GRANDTOTAL
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
1. When you extract the name from the associates file you also need to use the ID as a pattern to get the correct name.
Does this mean something like:
NAMES[$ID]=`awk -F"/" '/^2$ID/ {print $2}' associates`If so - this doesnt work for me at all - i just get blank line if i try doing something like:
echo "NAME[$ID]: ${NAMES[$ID]}"•
•
•
•
2. You can declare the arrays to store your total sales and your names as follows
declare -a totalsales declare -a names
Where would i declare these at the top of my program like?:
#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales
declare -a totalsales
declare -a names
ID=1
NAMES=()
while [ $ID -le 6 ]
dodoes not help me in getting output for a persons name into my array - any ideas?
With the above code - i think this is the wrong way round - i want to store the associates (all 6 of them) into an array just like i would for names...so....
TOTALSALES[1]=X for sales for John Doe
TOTALSALES[2]=Y for sales for President Bush
etc
etc
Make sense - but as i said - lets deal with the names - then once i get that - i should be able to get the total sales and position arrays sorted. Thanks
*****************EDIT*******************
If i edit my line to this:
while [ $ID -le 2 ]
do
echo "SALES FOR ASSOCIATE 2$ID"
NAMES[$ID]=`awk -F"/" '/^21/ {print $2}' associates`
echo "Name: ${NAMES[$ID]}"
while read line
doI get this:
/home/lx/z109079 : ./newtest.sh
SALES FOR ASSOCIATE 21
Name: John Doe
Grand total for 21: 284.74
I just cant get it working through the entire array ----
Last edited by tones1986 : Dec 3rd, 2007 at 8:51 pm.
•
•
Join Date: Oct 2007
Posts: 302
Reputation:
Rep Power: 3
Solved Threads: 37
I usually declare the arrays at the top of the script.
Something like this perhaps ? It uses 2 arrays one to store the names and the other to store the sales values.
I am not sure why your awk line doesn't work, since it works off the command prompt. For some reason you need to split it up in your script as below. I'll need to research that some more.
Something like this perhaps ? It uses 2 arrays one to store the names and the other to store the sales values.
I am not sure why your awk line doesn't work, since it works off the command prompt. For some reason you need to split it up in your script as below. I'll need to research that some more.
declare -a totalsales
declare -a names
-
-
-
-
while [ $ID -le 6 ]
do
GRANDTOTAL=0
echo "SALES FOR ASSOCIATE 2$ID"
while read line
do
PRODID=`echo $line|cut -d"," -f1`
QUANT=`echo $line|cut -d"," -f2`
PRICE=`awk /$PRODID/ products|awk -F"." '{print $3"."$4}'`
TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
GRANDTOTAL=$(echo "scale=2; $TOTAL+$GRANDTOTAL" | bc)
done < "/tmp/sales.2$ID"
totalsales[$ID]=$GRANDTOTAL
names[$ID]=`awk /^[2]$ID/ associates|awk -F"/" '{print $2}'`
echo "Associate 2$ID ${names[$ID]} made ${totalsales[$ID]}"
let ID=$ID+1
done
Last edited by stilllearning : Dec 3rd, 2007 at 9:53 pm.
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales
#cat /tmp/newSales
declare -a names
declare -a totalsales
declare -a position
ID=1
while [ $ID -le 6 ]
do
awk /[2][$ID]$/ /tmp/newSales > /tmp/sales.2$ID
let ID=$ID+1
done
GRANDTOTAL=0
ID=1
while [ $ID -le 6 ]
do
while read line
do
PRODID=`echo $line|cut -d"," -f1`
QUANT=`echo $line|cut -d"," -f2`
PRICE=`awk /^$PRODID/ products|awk -F":" '{print $3}'`
TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
GRANDTOTAL=$(echo "scale=2; $GRANDTOTAL+$TOTAL" | bc)
done < "/tmp/sales.2$ID"
totalsales[$ID]=$GRANDTOTAL
NAMES[$ID]=`awk /^[2]"$ID"/ associates | awk -F"/" '{print $2}'`
position[$ID]=`awk /[a-z]$/ associates | awk -F"/" '{print $4}'`
#Prints all the lines as NAME POSITION TOTALSALES
echo "${NAMES[$ID]} ${position[$ID]} ${totalsales[$ID]}"
GRANDTOTAL=0
let ID=$ID+1
done
# removes all created files
rm /tmp/sales.2*
rm /tmp/newSalesMy problem now is with the highlighted code... if i comment out that line i get this as a result:
/home/lx/z109079 : ./newtest.sh
John Doe 284.74
George Bush 1059.67
Susan Worker 279.52
Fast Buck 2504.83
Hillary Clinton 151.00
Dennis Miller 1.98
(remember that is with the BOLD line in the script above (#) commented out.)
If i include the line i get this:
/home/lx/z109079 : ./newtest.sh
John Doe Clerk
President
Manager
Stock Boy
Candidate
Commedian 284.74
George Bush Clerk
President
Manager
Stock Boy
Candidate
Commedian 1059.67
....and so on through 6th person
So its a similar problem as my name array - but im using a similar line of awk for it ... im just unsure how to apply the /********/ part of the first awk section...am i doing this wrong?
Thanks folks ever so much AGAIN!!!
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
Using the bold part below i want to get something that looks like this:
Marine Parts R Us
Main catalog
name position total sales
======================================
John Doe clerk 10,000
George Bush president 97.95
.....
So the total sales part needs to be sorted by price...this is surely possible with what i have below:
echo ${NAMES[$ID]}":"${position[$ID]}":"${totalsales[$ID]} >> /tmp/output.$$
This line creates this file:
John Doe:Clerk:284.74
George Bush
resident:1059.67
Susan Worker:Manager:279.52
Fast Buck
tock Boy:2504.83
Hillary Clinton:Candidate:151.00
Dennis Miller:Commedian:1.98
Which i want the bolded code to sort but ... its not getting the right file - nor is it able to sort it correctly... any ideas?
Marine Parts R Us
Main catalog
name position total sales
======================================
John Doe clerk 10,000
George Bush president 97.95
.....
So the total sales part needs to be sorted by price...this is surely possible with what i have below:
#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales
#cat /tmp/newSales
declare -a names
declare -a totalsales
declare -a position
echo -e "Name:\tPosition:\tTotal Sales:"
ID=1
while [ $ID -le 6 ]
do
awk /[2][$ID]$/ /tmp/newSales > /tmp/sales.2$ID
let ID=$ID+1
done
GRANDTOTAL=0
ID=1
while [ $ID -le 6 ]
do
while read line
do
PRODID=`echo $line|cut -d"," -f1`
QUANT=`echo $line|cut -d"," -f2`
PRICE=`awk /^$PRODID/ products|awk -F":" '{print $3}'`
TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
GRANDTOTAL=$(echo "scale=2; $GRANDTOTAL+$TOTAL" | bc)
done < "/tmp/sales.2$ID"
totalsales[$ID]=$GRANDTOTAL
NAMES[$ID]=`awk /^[2]"$ID"/ associates | awk -F"/" '{print $2}'`
position[$ID]=`awk /^[2]"$ID"/ associates | awk -F"/" '{print $4}'`
#echo -e "Name:\tPosition:\nTotal Sales:"
echo ${NAMES[$ID]}":"${position[$ID]}":"${totalsales[$ID]} >> /tmp/output.$$
GRANDTOTAL=0
let ID=$ID+1
done
cat > /tmp/output.$$ << DONE
BEGIN {
FS=":"
print "Marine Parts R Us"
print "Main Catalog"
print "Name\t\tPosition:\t\t\tTotal Sales:"
print "======================================"
}
{
printf("%3d\t%-20s\t%6.2f\n", \$1, \$2, \$3)
}
END {
print "======================================"
}
DONE
sort /tmp/output.$$ | awk -f /tmp/output.$$
/bin/rm -f /tmp/output.$$
#GRANDTOTAL=0
#let ID=$ID+1
#done
#echo CAT OUTPUT
#cat /tmp/output.$$
# removes all created files
rm /tmp/sales.2*
rm /tmp/newSales
#rm /tmp/output.$$
This line creates this file:
John Doe:Clerk:284.74
George Bush
resident:1059.67Susan Worker:Manager:279.52
Fast Buck
tock Boy:2504.83Hillary Clinton:Candidate:151.00
Dennis Miller:Commedian:1.98
Which i want the bolded code to sort but ... its not getting the right file - nor is it able to sort it correctly... any ideas?
Last edited by tones1986 : Dec 4th, 2007 at 12:43 am.
•
•
Join Date: Oct 2007
Posts: 302
Reputation:
Rep Power: 3
Solved Threads: 37
You could make this much easier
Once you have your output file with the values, all you really need to do is cat the output and pipe it into the sort. <Check the man pages for the sort and it'll tell you how to sort a delimited file with a certain key>, and then pipe the sort results into an awk statement to give you the formatted output.
Once you have your output file with the values, all you really need to do is cat the output and pipe it into the sort. <Check the man pages for the sort and it'll tell you how to sort a delimited file with a certain key>, and then pipe the sort results into an awk statement to give you the formatted output.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Bash Shell Script - sales/product/cost (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: Need Help! Could someone please check the two tiny bash scripts to see if they work?
- Next Thread: Please help


Linear Mode