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.
I have a text file that needs to be read, printed, and then saved as a variable. I know that to read and print a file I would use:
cat filename
But after it is read and printed, how would I save it as a variable. To give everyone a bit more insight on what I am trying to do. I have a text file that is overwritten every second...it looks like this
10
103
45
67
These four values are overwrote by new values every second. I need to find a function to save this as a variable and read the new text file evey second. How would I do this?
Will this be true if the numbers change? The numbers that I gave in the example will not ever be the same....they are always changing. How would I account for changing numbers?
Could you explain your code better...I am new to shell coding and do not understand what you are tryinging to do. It looks like your setting up an array, but this program will run all day everyday and a new textfile will be generated ever second of the day, therefore; I would not want an array just some type of loop that will constantly read and print out the text file and only stop on a command.
You have me confused now and I am sorry for that. I just dont understand what I need to do. First you told me to use:
Code:
for i in 0 1 2 3 4 5 6 7 8 9 10
do
a[$i]="$(<filename)"
echo "${a[$i]}"
sleep 1
done
echo "${a[5]}"
echo "${a[10]}"
What is the significants of 5 and 10 and why is the array 0-10. You print in the loop and out of the loop. What are you printing each time?
Then you tell me
while true
do
foo="`cat filename`"
echo "$foo"
sleep 1
done
Would I get the following functionality:
1) Whatever is in filename will be read and set to the variable foo
2) I print foo to the screen
3) Wait and then repeat.........read....set equal to foo.....print......read....set equal to foo.....print..etc
If so what will the done do and how would I ever get out of this loop if I needed to....or is there a better way to structure my loop?
that loop is infinite. You can always hit control-C on your terminal to kill it. If you want some other terminating condition, tell us what it should be : )
The "done" just tells the shell that's where the bottom of the loop is; stuff below done won't be included in the loop. (The shell doesn't know what you mean by indenting the body of the loop; you just do that so it's more readable to a human)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.