Bash: Input from multiple files.
- April 25th, 2009
- Posted in Documentation
- Write comment
exec 3<$1 4<$2
while read file1 <&3 && read file2 <&4
do
code
done
exec 3<$1 4<$2
while read file1 <&3 && read file2 <&4
do
code
done
I got “bash: $1: ambiguous redirect” for “exec 3<$1 4<$2” from my Ubuntu Linux.
You need to pass the two files as parameters to the script.
Perhaps, this will clarify. I have two files called file1 and file2. Below is a script to print the current line in each file. To run the script, ./myscript file1 file2.
#!/bin/bash
#!Script name: myscript
exec 3<$1 4<$2 while read linefile1 <&3 && read linefile2 <&4 do echo file1: $linefile1 file2: $linefile2 done