[TriLUG] solved: HERE document question

Jeff Schornick jeff at schornick.org
Sun Aug 1 14:24:21 EDT 2010


> http://pastebin.com/7nxNCnXw

How about a different take?

My experience, from the joys of inheriting old scripts, is that here
documents just muck up readability and maintainability.  Why don't you
separate your code from data by using two files, then use sed to
cleanly do the substitutions?

--------------

$ cat generator
#!/bin/bash

somevar1="Bob"
somevar2="Tom"

template="foo.tmpl"
outscript="generated"

sed -e s/SOMEVAR1/$somevar1/g \
    -e s/SOMEVAR2/$somevar2/g \
       $template  > $outscript

------------

$ cat foo.tmpl
Heya SOMEVAR1,

It's SOMEVAR2, from the office down the hall.

Here's some really cool bash I wrote:

  myvar="TriLUG rocks!"
  echo $myvar

Hope you like it.

  - SOMEVAR2

----------

$ cat generated
Heya Bob,

It's Tom, from the office down the hall.

Here's some really cool bash I wrote:

  myvar="TriLUG rocks!"
  echo $myvar

Hope you like it.

  - Tom

----------

Obviously, you could use a fancier convention for your template
variables.  I was lazy and used all caps.

  - Jeff



More information about the TriLUG mailing list