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-01-2005, 10:21 AM   #16
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30

Aluser,

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
 
Old 07-01-2005, 10:22 AM   #17
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Do you think this is the best way to go for what I am trying to do?
No if I stick to you first posting, where you specified twice you needed to save the file content in a variable ...

Yes, if you don't care about saving intermediate values somewhere ...
 
Old 07-01-2005, 10:25 AM   #18
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
If I end up needing to save the contents in a variable....what would be the best way to do it?
 
Old 07-01-2005, 10:33 AM   #19
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 07-01-2005, 10:41 AM   #20
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
Originally posted by mrobertson
Aluser,

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?
 
Old 07-01-2005, 10:53 AM   #21
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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.
 
Old 07-01-2005, 11:00 AM   #22
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ...
Quote:
Thanks for your time and help.
You're welcome !
 
Old 07-01-2005, 11:02 AM   #23
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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
 
Old 07-01-2005, 12:40 PM   #24
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
how do you get the code to execute?

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?
 
Old 07-01-2005, 01:24 PM   #25
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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.
 
Old 07-01-2005, 02:01 PM   #26
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
I saved the code in a etc directory as sample.
Okay, but which code ? post it there just to be sure.
Quote:
I did a cd etc.....
Okay
Quote:
then chmod 711 sample....
Okay, well "chmod a+x sample" is clearer
Quote:
then at the command line typed smaple
Typo here ...
Quote:
and ./sample
Okay
Quote:
and it said no file exists.
What said that ?
Paste the full error message for us to understand what's going on !
Quote:
I did a ls in the etc directory and the file was there..
Okay
Quote:
what could be wrong?
Don't know, depend on the full error message and the script itself ...
 
Old 07-05-2005, 07:05 AM   #27
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Here is the code that I used:

Code:
#!/bin/sh

while sleep 1
do
  cat crap1
done
I was using telnet when trying to execute the code. The error message that I get is as follows:

-sh: ./sample: not found

It almost sounds like it may be from this line: #!/bin/sh

Let me know what you think?
 
Old 07-05-2005, 07:09 AM   #28
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 07-05-2005, 07:13 AM   #29
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
even smarter?

Code:
tail -f filename
Quote:

SunOS 5.8 Last change: 18 Mar 1997 1

User Commands tail(1)


-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.
 
Old 07-05-2005, 07:16 AM   #30
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
I did try that and the file was in the directory. Any other ideas?
 
  


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
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 08:10 AM
NEED AWNSER QUICK how do I save editing a text file in Konsole? DDRfreak2 Mandriva 1 08-18-2005 06:09 PM
wiring to a text file and being able to save and add to it mrobertson Programming 1 07-27-2005 02:27 PM
Find variable in template file replace w/date+ and save as jmanjohn61 Linux - General 14 12-13-2004 06:49 AM

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

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