[SOLVED] A bash script that remembers variable contents from previous runs.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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)!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.