LinuxQuestions.org
Visit Jeremy's Blog.
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 05-22-2007, 03:39 PM   #1
rmount
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS
Posts: 22

Rep: Reputation: 15
bash, for loop, and arithmetic


Hello all, i've been scratching my head on this one. I'm not sure what i've done wrong.

I've got a bunch of subdirectories, each with a log file in them. I'm grepping the files for an error count, then totaling the number. Here's the script i'm using:

Code:
#!/bin/bash
#
# Error counter
# ---------------------

file="error.log"
number=0
total=0

for dir in /var/log/mht*
do
    number=`grep -i ^errorcount $dir/$file | awk -F'=' '{ print $2 }'`
    number=$(( $number + $total ))
done

echo "Total error count = $total"

exit
My total is always 0. Can anyone see what i'm doing wrong, or give me another way to skin the cat?

Thanks!
 
Old 05-22-2007, 03:43 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You're not setting total to anything other than zero in the initialization. You probably want that second assignment in the loop to be "total="...
 
Old 05-22-2007, 03:44 PM   #3
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Nothing ever gets added to total.

Perhaps where you said number=$(( $number + $total )) you meant total=...?
 
Old 05-22-2007, 03:59 PM   #4
rmount
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Matir
Nothing ever gets added to total.

Perhaps where you said number=$(( $number + $total )) you meant total=...?
Duh... Been playing around with it so much. I corrected that. Also had to re-order it to this:
Code:
total=$(( $total + $number ))
If i use "total=$(( $number + $total ))" the script hangs.

So with the corrected script, i now get this:
Code:
 : syntax error: operand expected (error token is " ")
 
Old 05-22-2007, 05:05 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Probably a variable that is not initialized.

Old school: echo the variables before the assignment.

Newer school: run the script with "-vx":
Code:
sh -vx file-in-which-script-resides
Best wishes ... cheers, makyo
 
Old 05-22-2007, 08:51 PM   #6
rmount
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by makyo
Newer school: run the script with "-vx":
Here's the output:
Code:
#!/bin/bash
#
# Error counter
# ---------------------

file="error.log" 
+ file=error.log
number=0
+ number=0
total=
+ total=

for dir in /var/log/mht*
do 
    number=`grep -i ^errorcount $dir/$file | awk -F'=' '{ print $2 }'`
    total=$(( $total + $number )) 
    echo $total
done 
grep -i ^errorcount $dir/$file | awk -F'=' '{ print $2 }'
++ grep -i '^errorcount' /var/log/mht-001/error.log
++ awk -F= '{ print $2 }'
+ number=10
+ total=10
+ echo 10
10
grep -i ^errorcount $dir/$file | awk -F'=' '{ print $2 }'
++ grep -i '^errorcount' /var/log/mht-002/error.log
++ awk -F= '{ print $2 }'
+ number=10
 : syntax error: operand expected (error token is " ")
So it runs the first pass just fine, then bails on the total of the second file.

I tried echoing before assignment and get the same error
 
Old 05-22-2007, 09:11 PM   #7
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I'm wondering if it might be a whitespace issue in $number. Try something (silly as it seems) like "number=`echo $number`".
 
Old 05-22-2007, 09:27 PM   #8
rmount
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Matir
I'm wondering if it might be a whitespace issue in $number. Try something (silly as it seems) like "number=`echo $number`".
Good thought. I added that in before the "total=$(($total+$number))" and it still gives the same error
 
Old 05-22-2007, 09:43 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{FS="="}/^errorcount/{ count+=$2}END{print "total: "count}' /var/log/mht*/error.log
 
Old 05-22-2007, 09:55 PM   #10
rmount
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74
Code:
awk 'BEGIN{FS="="}/^errorcount/{ count+=$2}END{print "total: "count}' /var/log/mht*/error.log
Simple, yet genius. Thanks Ghostdog74, that did the trick and replaces almost all of the script
 
Old 05-22-2007, 10:46 PM   #11
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Man, I need to learn awk.
 
Old 05-22-2007, 10:54 PM   #12
rmount
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Matir
Man, I need to learn awk.
I agree, that and sed. I've only ever used either in a very basic way.
 
Old 05-22-2007, 11:09 PM   #13
sidney.harrell
Member
 
Registered: Apr 2006
Location: Stafford, VA
Distribution: Ubuntu 6.06, 7.04, Slackware 11
Posts: 45

Rep: Reputation: 15
For some light reading...
http://www.gnu.org/software/gawk/manual/gawk.html
Great, just what I needed, another complete language to learn. There are not enough hours in the day.
Quote:
On one of many trips to the library or bookstore in search of books on Unix, I found the gray AWK book, a.k.a. Aho, Kernighan and Weinberger, The AWK Programming Language, Addison-Wesley, 1988. AWK's simple programming paradigm—find a pattern in the input and then perform an action—often reduced complex or tedious data manipulations to few lines of code. I was excited to try my hand at programming in AWK.
Is that the same Kernighan from the C language fame?

Last edited by sidney.harrell; 05-22-2007 at 11:13 PM.
 
Old 05-23-2007, 01:27 AM   #14
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
total = `expr $total + $number`

End
 
  


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 loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
BASH, simple arithmetic helptonewbie Programming 9 01-10-2007 05:53 PM
Bash arithmetic Blackout_08 Programming 2 06-08-2006 10:37 PM
simple arithmetic in bash gfrair Linux - Newbie 9 03-16-2005 02:09 PM
Bash, non-integers and arithmetic causticmtl Programming 5 07-16-2003 09:15 AM

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

All times are GMT -5. The time now is 11:40 PM.

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