Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
reading some args in BASH for-loop
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
haimat
Apprentice
Apprentice


Joined: 05 Sep 2002
Posts: 239
Location: Vienna / Austria

PostPosted: Wed Jan 22, 2003 7:28 pm    Post subject: reading some args in BASH for-loop Reply with quote

Hi all,

I have a little question about BASH programming and I hope I am not too OT here...
Ok, here it comes: I have a text file with the following content:

Code:
a1 a2 a3
b1 b2 b3
c1 c2 c3


Now, I want to read these arguments in for-loop, but ALL THREE arguments from a line AT ONCE? Something like "for arg1 arg2 arg3 in ..." -- is this possible?

TIA and Greets, Matthias
Back to top
View user's profile Send private message
haimat
Apprentice
Apprentice


Joined: 05 Sep 2002
Posts: 239
Location: Vienna / Austria

PostPosted: Wed Jan 22, 2003 7:39 pm    Post subject: Reply with quote

ok, I have found a solution now, here is the code:

Code:
#!/bin/bash

cat ./test.txt | while read arg1 arg2 arg3; do
        echo $arg1
        echo $arg2
        echo $arg3
done


Cheers, Matthias
Back to top
View user's profile Send private message
jukka
Apprentice
Apprentice


Joined: 06 Jun 2002
Posts: 249
Location: Zurich, Switzerland

PostPosted: Wed Jan 22, 2003 9:13 pm    Post subject: Reply with quote

hi haimat,

a faster solution:
Code:
#!/bin/bash

while read arg1 arg2 arg3 null; do
  # whatever...
done <./test.txt

(adding a var 'null' catches possibly more words in the input stream, instead of appending them to arg3. that may or may not be what you want...).

you could also read the data into an array (read -a array_name) and access the elements with ${array_name[n]} (for n >= 0).

hth, jukka
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum