Please don't thumb up this answer but Stephen's one which I marked correct. This is but a variation easier for me to read.
If the content of the template file should only be appended to the end file if none of it is already present in the end file, you can use
cat <<-"EOF1" > myPath/myTemplate
if ! grep -F -q -f myPath/myFile myPath/myTemplate; then
cat myPath/myTemplate >> myPath/myFile
grep command searches
myPath/endFile.sh for any fixed string (
-F) contained in
myPath/myTemplate, reading one pattern per line (
-f), and indicates whether it finds any only by its exit code, with no output (
The result is then negated
!, so that the
then block of the
if-then block will only be executed if
grep doesn’t match anything in the end file.
Appendix - exit codes:
When a process exits, it returns an exit code, which is a small integer. By convention, an exit code of 0 indicates success, non-zero indicates failure or an error:
grep returns 0 when it finds a match, and 1 if it doesn’t.
if uses its argument’s exit code to decide what to do; if 0, process the
then block, otherwise the
else block (if any).
A punctuation (
!) flips that; The
then block only runs if grep doesn’t match anything (or encounters an error).