LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Saving the output of a file as a variable (https://www.linuxquestions.org/questions/programming-9/saving-the-output-of-a-file-as-a-variable-340906/)

mrobertson 07-07-2005 07:20 AM

Saving the output of a file as a variable
 
I currently have the following shell script:

Code:

while sleep 1
    do
            cat camdata
done

Which prints out a text file that looks something like this:

123456
12.66
13.99
1234.99

I need for this file to be printed out and each of these numbers saved as its own variable (id, width, length, speed) respectively. Can anybody help me as to how this would be done?

pycoucou 07-07-2005 07:28 AM

Hi,

if you do the following:
for i in `cat camdata`

i is going to store iteratively each value of camdata. You need to add a loop of control of the number of parameters to get 4 times i, do your processing and go on. I don;t how to work the file line by line, but this method would work...

mrobertson 07-07-2005 07:45 AM

I am not sure that I understand what you are saying. Where do you define what you want each instance of i to be? Would it be possible to give a more detailed explanation?

pycoucou 07-07-2005 03:18 PM

if camdata =
1 2 3 4
5 6 7 8

for i in `cat camdata`
do
echo $i
done

would give:
1
2
3
4
5
6
7
8

And in fact, i don't know how to read 4 times the input ..... Sorry, I don't help. I don't know awk enough to dare suggesting an alternative...

rstewart 07-07-2005 05:35 PM

Hello,

Quote:

am not sure that I understand what you are saying. Where do you define what you want each instance of i to be? Would it be possible to give a more detailed explanation?
I would suggest that you take a look at the following tutorials. I think that they could help you to understand what is involved in writing shell scripts. Maybe this could save you some time in the long run. :)

http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html
http://www-128.ibm.com/developerwork...ry/l-bash.html
http://www.tldp.org/LDP/abs/html/

jlliagre 07-08-2005 12:43 AM

Code:

while read a b c d
do
  echo a=$a b=$b c=$c d=$d
done < camdata



All times are GMT -5. The time now is 05:15 AM.