LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script: add all numbers from command output (https://www.linuxquestions.org/questions/programming-9/bash-script-add-all-numbers-from-command-output-79452/)

wi-Z-art 08-06-2003 06:27 AM

Bash script: add all numbers from command output
 
Hi everyone,

I just wrote a small shell script to count all the spam I trapped using my spamassassin. Herein I also print the spam-count from each message.
Now I would like to add the sum to the output but I'm not really sure how to do this.
In the past I sometimes used "bc" for arithmetic operations in my scripts. But I havo no idea how to add it here.
This is the code I have atm, you are free to copy any part if you find it of any use to you.

Code:

#!/bin/sh
pushd . > /dev/null
cd /home/profiles
echo "Counting Spam..."
for i in `ls * -d`; do
        MESSAGES=`find $i/Maildir/.Spam | grep anubis`
        if [ -z "$MESSAGES" ]
        then
                continue
        fi
        COUNT=`echo $MESSAGES | wc -w`
        echo $COUNT Spam messages for $i trapped;
        for i in $MESSAGES; do
                grep -h "Content analysis details" $i | cut -d : -f 2 | cut -d , -f 1 | cut -d \( -f 2;
        done
done
popd > /dev/null

A sample output is this:
Code:

5 Spam messages for carsten trapped
8.10 points
9.60 points
8.40 points
6.90 points
14.70 points
1 Spam messages for uwe trapped
9.70 points

I'd really appreciate any idea...
Greetings
Marcus

TheLinuxDuck 08-06-2003 09:08 AM

wi-Z-art:

Before the main loop starts, init a variable as 0, something like:
Code:

totalCount=0
echo "Counting Spam..."
for i in `ls * -d`; do
  ...

Then, inside of the loop, after $COUNT is defined, add it to the total with a similar statement to:
Code:

  ...
  COUNT=`echo $MESSAGES | wc -w`
  echo $COUNT Spam messages for $i trapped;
  totalCount=$(($totalCount+$COUNT))
  ...

That should do it! (=

wi-Z-art 08-06-2003 10:16 AM

Floating points...
 
Hey,

thanks for your reply.
As always the simplest things are often the best ones :)
The only problem I had was that the numbers are floating point and bash isn't able to add floating point numbers, so I used bc to do this.
This is the new version with a nicer output:
Code:

#!/bin/sh
MAXUSER=""
MAXSPAM=0
pushd . > /dev/null
cd /home/profiles
echo "Counting Spam..."
for USER in `ls * -d`; do
        MESSAGES=`find $USER/Maildir/.Spam | grep anubis`
        if [ -z "$MESSAGES" ]
        then
                continue
        fi
        COUNT=`echo $MESSAGES | wc -w`
        USERSPAM=0
        for msg in $MESSAGES; do
                SPAMPOINTS=`grep -h "Content analysis details" $msg | cut -d : -f 2 | cut -d , -f 1 | cut -d \( -f 2 | cut -f 1 -d
                if [ `echo "$SPAMPOINTS>$MAXSPAM" | bc -q` -gt 0 ]
                then
                        MAXSPAM=$SPAMPOINTS
                        MAXUSER=$USER;
                fi
                USERSPAM=`echo "$USERSPAM+$SPAMPOINTS" | bc -q`;
        done
        echo $USER had $USERSPAM spam points in $COUNT messages
done
echo The highest ranked mail was sent to $MAXUSER. It had $MAXSPAM points!
popd > /dev/null

The output looks like this:
Code:

...
carsten had 73.30 spam points in 7 messages
uwe had 9.70 spam points in 1 messages
...
The highest ranked mail was sent to carsten. It had 14.70 points!

Thanks again! I'll use this script to get an automatic statistics mail about blocked spam every morning. This brightens up every admins morning :D

Greetings
Marcus


All times are GMT -5. The time now is 03:05 AM.