LinuxQuestions.org
Review your favorite Linux distribution.
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-07-2005, 08:20 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
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?
 
Old 07-07-2005, 08:28 AM   #2
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
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...
 
Old 07-07-2005, 08:45 AM   #3
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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?
 
Old 07-07-2005, 04:18 PM   #4
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
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...
 
Old 07-07-2005, 06:35 PM   #5
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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/
 
Old 07-08-2005, 01:43 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Code:
 while read a b c d
do
  echo a=$a b=$b c=$c d=$d
done < camdata
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:25 PM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 01:40 PM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 08:54 PM
Saving gcc output files to new directory Fe98 Programming 3 12-08-2003 08:18 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 07:12 PM


All times are GMT -5. The time now is 08:03 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration