Reputation: 11
Trying to pull an entry from two files (foofile, barfile) and pass them into a third file (dothis.sh) as arguments. It works for bar but not for foo since this looks be a scope related issue. I tried to nest the foo loop into the bar loop too with no success:
#!/bin/bash
while read foo
do
#echo $foo
export foo
done < FooFile
while read bar
do
#echo $bar
export bar
./dothis.sh $bar $foo
done < BarFile
Upvotes: 1
Views: 239
Reputation: 16662
Here's a way to loop with two inputs at the same time in bash:
#!/bin/bash
while read -u 100 foo && read -u 101 bar; do
./dothis.sh "$foo" "$bar"
done 100<FooFile 101<BarFile
It will terminate when one file has no more lines to read.
Upvotes: 2
Reputation: 4455
foo is in scope in your program, but it is empty.
Consider this code:
foo="test"
while read foo; do echo $foo; done < /dev/null
echo "foo=$foo"
The result is:
foo=
The problem is that foo will be set to empty when read has no input. That's what's happening in your program.
That said, pay attention to oguzismail's comment. (S)he is leading you a good direction:
paste FooFile BarFile | xargs -n 2 ./dothis.sh
It's really an excellent start and probably all you'll need in the simple cases where FooFile and BarFile have the same amount of entries.
Upvotes: 1