View previous topic :: View next topic |
Author |
Message |
haimat Apprentice
Joined: 05 Sep 2002 Posts: 239 Location: Vienna / Austria
|
Posted: Wed Jan 22, 2003 7:28 pm Post subject: reading some args in BASH for-loop |
|
|
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 |
|
|
haimat Apprentice
Joined: 05 Sep 2002 Posts: 239 Location: Vienna / Austria
|
Posted: Wed Jan 22, 2003 7:39 pm Post subject: |
|
|
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 |
|
|
jukka Apprentice
Joined: 06 Jun 2002 Posts: 249 Location: Zurich, Switzerland
|
Posted: Wed Jan 22, 2003 9:13 pm Post subject: |
|
|
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 |
|
|
|