LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-31-2006, 07:54 PM   #1
cachemonet
Member
 
Registered: Jan 2006
Distribution: Various versions of Red Hat Fedora Core and Ubuntu
Posts: 40

Rep: Reputation: 15
retain a variable each time a script re-runs


I am having difficulty writing a shell script that retains a variable. This is what I need to do.

I call a shell script via a cron job every 5 minutes. It checks how many incoming connections I have on a particular port and logs it to a web page. That portion is done. Here is my problem. In additon to those stats, I want to get an hourly total. In my mind, I need to retain that variable and keep a count of it each time the script runs. Then at the end of that hour, I want to divide that total of my previous variable(s) and divide by 12 (each time the script runs) to get a average. There are other things that need to happen so I need to continually call the script.

My problem is each time that script runs it resets my variable and I can not get a running total. I tried making a new variable each time it runs by creating a variable based on the current minute. I was receiving errors with that. Obviously I am doing something wrong here. Most of my scriupts are run once use once. So I am not certain how to proceed. I Can someone please give me a code example for a bash script or point me to a resource that will help. THANKS A LOT.
 
Old 07-31-2006, 08:17 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
The only way is to use an external database, or save intermediate results to a file.
 
Old 07-31-2006, 08:25 PM   #3
cachemonet
Member
 
Registered: Jan 2006
Distribution: Various versions of Red Hat Fedora Core and Ubuntu
Posts: 40

Original Poster
Rep: Reputation: 15
GRRR. I thought so. Thanks. Ill setup mysql.
 
Old 07-31-2006, 08:38 PM   #4
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
mysql is overkill for that.

Code:
filename=/tmp/foobar
count=2
oldcount=`cat $filename`

x=`dc << EOF
$count
$oldcount
+pq
EOF
` 

echo $x > $filename
This is a simple sample. You'd want to have code to put a zero in the file after you run your hourly stats. And you'd want to replace the code to assign "count" with your numbers. And the filename becomes whatever you want to use to store the running total.

This adds the numbers as you go. You could keep all 12 by just using echo $count >> $filename ... and then you'd have the script part which creates the other average... but that is slightly different.

Code:
averagefuction() {
     # do the averaging here and null the file out at the end.
     > $filename
}

echo $count >> $filename

if [ `cat $filename | wc -l` -eq 12 ]; then
     averagefunction
fi

Last edited by frob23; 07-31-2006 at 08:43 PM.
 
Old 08-01-2006, 12:11 AM   #5
cachemonet
Member
 
Registered: Jan 2006
Distribution: Various versions of Red Hat Fedora Core and Ubuntu
Posts: 40

Original Poster
Rep: Reputation: 15
The file containing the variable did the trick. Thanks a lot for the suggestions ggiys. Both of you were a huge help. My script is running properly now. Thanks Now I can go to sleep
 
  


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
linux time runs fast d0ggi3 Linux - Software 2 02-13-2006 12:18 PM
Fetchmail only polls successfully first time it runs FatOldSun Linux - Software 8 03-22-2005 10:44 AM
cron.daily runs at the wrong time dwz3591 Linux - Software 2 10-27-2004 09:46 AM
Command to time how long a program runs jing Linux - Newbie 1 07-19-2004 10:35 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM

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

All times are GMT -5. The time now is 08:09 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