LinuxQuestions.org
Help answer threads with 0 replies.
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-14-2015, 07:42 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
A bash script that remembers variable contents from previous runs.


Hi: suppose I have a bash script that is run every minute by cron. Suppose further the script is this:
Code:
 
#!/bin/bash             
let COUNT=COUNT+1
I would like the script to remember old values of COUNT, so that COUNT can reach the value 2. If I could somehow export, from the bash instance running this script, the COUNT variable to the parent procces (this is another bash instance) and parent variables where available to children, then my problem is solved. But the export mechanism (export sentence) doesn't seem to work that way. Adding an 'export COUNT' to the script doesn't work. The first question would be: affordmentioned problem solvable with bash scripts? Or should I forget about cron and make a program that works on the background, with a sentence like sleep 60?

Last edited by stf92; 08-14-2015 at 08:08 PM.
 
Old 08-14-2015, 07:55 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
How about this...

Code:
echo 1 > /path/to/count.txt

Then create cron script...

#!/bin/bash
COUNT=$(</path/to/count.txt)

... do something with COUNT ...

((COUNT = $COUNT + 1))
echo "$COUNT" > /path/to/count.txt
*** Updated per HMW's post #4 below... thanks!

Last edited by astrogeek; 08-15-2015 at 12:24 PM. Reason: Cannot do anything without typos...
 
2 members found this post helpful.
Old 08-14-2015, 08:17 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Of course! Making a file work like a variable. But now the variable is non volatile. The other solution would be to write a daemon, that is
Code:
[program body]
[Interrupt service routine]
Hang the ISR from some signal
I decide on the first approach. The initialization can be done at boot time, /etc/profile for example.
Thanks.

Last edited by stf92; 08-15-2015 at 11:02 AM.
 
Old 08-15-2015, 10:33 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by astrogeek View Post
How about this...

Code:
echo 1 > /path/to/count.txt

Then create cron script...

#!/bin/bash
COUNT = $(</path/to/count.txt)

... do something with COUNT ...

((COUNT = $COUNT + 1))
echo "$COUNT" > /path/to/count.txt
Let me first of all say that this is an excellent solution to this problem. And rep has been given accordingly, and I realize the code is a mockup of sorts.

However, just to avoid anybody becoming disgruntled. This:
Code:
COUNT = $(</path/to/count.txt)
Will not work due to the spaces, it should be:
Code:
COUNT=$(</path/to/count.txt)
So, again, no critique against the code. Just correcting a minor error.

Best regards,
HMW
 
1 members found this post helpful.
Old 08-15-2015, 12:23 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by HMW View Post
However, just to avoid anybody becoming disgruntled. This:
Code:
COUNT = $(</path/to/count.txt)
Will not work due to the spaces, it should be:
Code:
COUNT=$(</path/to/count.txt)
Thanks! Of course, you are correct.

I usually write a test case before posting code, and did so with this one... but then I typed it in instead of copy/paste and had some sort of brain glitch... a recurring problem lately!

Thanks for the correction (also fixed in my post to prevent problems for others trying to use it)!

Last edited by astrogeek; 08-15-2015 at 12:24 PM.
 
Old 08-15-2015, 12:27 PM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by astrogeek View Post
Thanks for the correction (also fixed in my post to prevent problems for others trying to use it)!
No problem astro, I get those glitches too!

Best regards,
HMW
 
  


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
Bash - obtain contents of file after variable substitution Chaos Daemon Linux - General 6 09-21-2011 11:37 AM
grep a shell script variable contents kushalkoolwal Programming 8 02-04-2009 06:15 AM
bash variable loses contents in mysql statement thedude2010 Programming 3 06-02-2006 04:15 AM
get file contents with newlines into bash variable otoomet Linux - Software 2 01-06-2005 01:23 PM

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

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