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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Assuming you need to run the script for an arbitrarily length period of time, an array is probably a bad idea, it would be wiser to save the file content to the file system.
e.g.:
Code:
while true
do
a="$(<filename)"
echo "$a" | tee /somedirectory/filename.$(date +"%Y-%m-%d@%T")
sleep 1
done
take a look at the first post of this thread. If you ned more info let me know and Ill fill you in a little more
your first post says you need to save the file contents to a variable but not why. So, I don't know if you need to save it or not, and thus I can't tell if the loop with cat and not saving the file to a variable is what you want.
Sooooo, what is your script actually supposed to accompish? and why not just run these code snippets to see if they do what you want?
My script just needs to read a text file every second and print out whatever is in the file. I am debating as to whether I need to save the contents as a variable each time they are read or not. If you could give me an idea of how both would work....that is saving the contents as a variable and not saving as a variable...it would be greatly appreciated. Thanks for your time and help.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
My script just needs to read a text file every second and print out whatever is in the file.
This has been answered many times.
Quote:
I am debating as to whether I need to save the contents as a variable each time they are read or not.
That is your right.
Quote:
If you could give me an idea of how both would work....that is saving the contents as a variable and not saving as a variable...it would be greatly appreciated.
To save or not to save ... perhaps can you give us a clue of what you intend to do with the stored data ...
a while back jllagre posted two equivalent snippets, one which saves to a variable and one which doesn't. The difference is that the save-to-variable one is slightly less efficient.
If you want to *do* something with that variable inside the loop, other than just echo it once which is effectively what cat does, then you should start with the snippet that saves to the variable.
If you only want to cat the file once every second, then by god just cat it once every secon
I saved the code in a etc directory as sample. I did a cd etc.....then chmod 711 sample....then at the command line typed smaple and ./sample and it said no file exists. I did a ls in the etc directory and the file was there.. what could be wrong?
to OP: when *all* you want to do is cat a file every 2 secs, I'd recommend watch(1) (go play with it anyways--it's about just as cool as screen & wget (okay, almost)). If it's part of something bigger, then a sleep-2-loop is probably better. You can also put the loop in the background:
Code:
while true; do
...
done &
Google for `advanced bash scripting guide'. I'd also like to suggest that you read "Smart Questions" (ESR)--especially the part about describing the goal and not just the step.
I understand that one of your goals is to learn, and that changes things a bit (from somewhat technical to somewhat... didactic/mental/psycological).
$ set CENTS="`seq 2 3 | head -n 1`"
$ whoami
jonas
$ exit
Last edited by jonaskoelker; 07-01-2005 at 01:26 PM.
No as the error say : -sh: ./sample: not found
I think sh does not find sample script
You could try ls command to see if sample is here
before attempt to ./sample
-f Follow. If the input-file is not a pipe, the program
will not terminate after the line of the input-file
has been copied, but will enter an endless loop,
wherein it sleeps for a second and then attempts to
read and copy further records from the input-file.
Thus it may be used to monitor the growth of a file
that is being written by some other process.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.