I don't know why you do that, but you have two templates here. One is your 'database' and one is your real template. Both are easy to handle with shtpl. (private project of mine, so not widely in use, but was developed to solve actually those kind of problems)
With shtpl you would do something like this:
Content of file 'configuration':
template_main=main.txt
template_other=other.txt
text1=whatever
text2=blah
Content of file 'database' (I assumed, that the delimiter is tab(\t)):
#% . "$CONFFile"
#% if [ -z "$template_main" ] || [ -z "$template_other" ] || \
#% [ -z "$text1" ] || [ -z "$text2" ]; then
#% printf "database could not be generated!\n" > /dev/stderr
#% exit 1
#% fi
#%# outputfile template data1 data2 data3
first.txt $template_main $text1 abcd 1234
second.txt $template_main $text2 efgh 5678
third.txt $template_other $text1 ij 90
Content of generatetemplates.sh:
#!/bin/bash
if [ ! -s "$CONFFile" ]; then
if [ ! -s "$1" ]; then
printf "CONFfile is not set or empty!\n"
exit 1
else
export CONFFile="$1"
fi
fi
DB="$( bash -c "$( shtpl database )" )"
if [ -z "$DB" ]; then
printf "Database is empty! Abort.\n"
exit 2
fi
IFS=$'\t'
printf "%s" "$DB" | while read "Out" "In" "data1" "data2" "data3"; do
data1="$data1" data2="$data2" data3="$data3" \
bash -c "$( shtpl "$In" )" > "$Out"
done
Content of main.txt (other.txt is quite the same):
main.txt template
$data1
$data2
$data3
So executing generatetemplates.sh
$ bash generatetemplates.sh "./configuration"
generates us first.txt, second.txt and third.txt.
$ cat first.txt | $ cat second.txt | $ cat third.txt
main.txt template | main.txt template | other.txt template
whatever | blah | whatever
abcd | efgh | ij
1234 | 5678 | 90
Little explanation:
In generatetemplates.sh is first the needed 'database' generated from your configuration-file. And secondly for every tupel in the database finally the corresponding Out-file from your In-template.
Note: An empty data[123] troubles read. So it is not possible with this approach.
So, hope this is simple enough for your needs.
Have fun!