Hello, and welcome to LQ.
One stylistic hint - you can use [code] tags to preserve formatting of posted source code, or anything else which should be displayed in a fix-width font.
Ok, to your question... You can prefix a command or loop with "[n]<filename" to open the file
filename and assign this open file to the numerical descriptor [n]. This file descriptor can then be passed to
read and some other shell-internal commands. You can open more than one file this way.
The descriptors 0 1 and 2 are usually pre-allocated to be standard input, standard output and standard error, and you should not use numbers greater than 9 as these might interfere with the shell's own file operations. So you have 3 - 9 to play with.
Here's a very simple implementation of what you want to do:
If you have two files,
names and
contents, the former containing the file names you would like to use (one per line), the latter containing the data you with to put into the resulting files (one file's content per line), you can do it like this:
Code:
#!/bin/bash
while read -u3 filename; do
read -u4 data
echo "$data" > "$filename"
done 3<names 4<contents
The read command can use the -u option to specify which file descriptor to read from. The default is standard input.
One thing to be a little careful of here: if the file
contents does not have as many lines as
names, you will end up having some nearly empty files. They will in fact contain a single new line character as the "" in the echo line will still print the newline character.
In humanity
Matthew