LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2003, 05:27 AM   #1
wi-Z-art
LQ Newbie
 
Registered: Oct 2002
Location: Cologne, Germany
Distribution: Debian
Posts: 9

Rep: Reputation: 0
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
 
Old 08-06-2003, 08:08 AM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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! (=
 
Old 08-06-2003, 09:16 AM   #3
wi-Z-art
LQ Newbie
 
Registered: Oct 2002
Location: Cologne, Germany
Distribution: Debian
Posts: 9

Original Poster
Rep: Reputation: 0
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

Greetings
Marcus

Last edited by wi-Z-art; 08-06-2003 at 09:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
Bash: add date to output line blizunt7 Programming 7 11-10-2004 03:45 AM
bash script to output only differences in two files czarherr Programming 4 11-08-2004 06:25 PM
Command output conrolling in bash pazvant Linux - Software 1 10-24-2003 07:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration