Q:

Shell scripts: Data to template text-file

I would like to create a script that takes some text-data, and produces a text-file according to a pre-created template file.
The expected functioning would be something like follows: I have created a template text file
Today we describe <text1>.
<text2>
Hope you enjoyed the topic of today!

and I would like to automatically create files with specified data <text1> and <text2> as input. I imagine the usage would be the most convenient if I could just save <text1> and <text2> as the first and second line of a text document. So assume I have created the file input.txt with just two lines (or some other separator than a line-break if it is better):
flowers
Flowers form a subset of plants. They are often beautiful.

Then I would like that running sh description.sh would produce me a file flowers.txt with the content
Today we describe flowers.
Flowers form a subset of plants. They are often beautiful.
Hope you enjoyed the topic of today!

So the question is, what should be the content of description.sh to function like this?
I am new in doing shell scripts, so probably I am not able to search with correct key words to find out if such a thing is easy or is answered here already many times.  I also appreciate explanations together with the correct line of code, to learn more. Sorry and thanks for your help!

A:

This will do what you intend. It will generate a file called 'result.txt'.
Be careful with your input as you might need to escape for sed to work as expected.
execute.sh :
#!/bin/bash

[[ "$#" -ne 2 ]] && { echo "This command requires two parameters. A dictionary and a template. Example : ./execute.sh dict.txt template.txt"; exit 1;}

dict=$1 && shift
template=$1 && shift
tmpfile="$(mktemp /tmp/executor_XXXXXXXXXXX)"

declare -a words
cntr=1
while read line
do
    words[cntr]="$line"
    (( cntr++ ))    
done <<< "$( cat input.txt )"

cat "$template" > "$tmpfile"
for((i=1; i <= "${#words[@]}" ; i++ ))
do
    sed -i "s/<$i>/${words[$i]}/g" "$tmpfile"
done

cat "$tmpfile" > result.txt

usage :
./execute.sh input.txt template.txt

Also, I changed your templating logic so you need to type <1> instead of <text1> as it requires less typing. 

