[Semibug] Indented "here documents"

Nick Holland nick at holland-consulting.net
Wed Aug 22 22:53:13 EDT 2018


As I mentioned after Michael's presentation on "ed" yesterday, when
moving my shell scripting tips presentation from whatever it started on
to whatever I finished using, I missed a slide.

Often forgot when scripting are "here documents" -- done by doing
something like this:

program <<__EOT
input for program 1
input for program 2
input for program 3
__EOT

where the three lines between <<__EOT and __EOT are fed to "program".
"__EOT" can be any unique string that isn't in the body of the text
being fed to the program...double leading underscores are common, but
not at all required.

Lots of people know about this...though I know I often forgot about them
until I started putting together my shell scripting tips presentation.

The problem is, in a neatly formatted script, here documents are often
ugly -- crammed against the left margin, even though the "program" may
be indented...so your here document might look like this:

while bla; do
    program <<__EOT
input for program
input for program
input for program
__EOT
    done
bla bla

Well, turns out as is often the case, Unix coders long ago noticed the
same problems we thought we discovered, and fixed it long ago.  It turns
out if you change the << to <<- the shell will strip off leading tabs
(and ONLY tabs), so your script can now look more like this:

while bla; do
    program <<-__EOT
	input for program
	input for program
	input for program
	__EOT
    done
bla bla

Now note, both the "here document" text and the end marker are all
indented with a tab.  Now, the tabs are all stripped, so you can have
multiple (or inconsistent) tabs if needed/desired.


And ... here's a script I used today, slightly sanitized, I ran today.
A few weeks ago, I updated the /etc/hosts file on all our customers
systems to fix a long standing and painful set of problems, purging a
lot of junk and replacing it with consistent data, while not messing
with the system specific data.  And about two days after completing this
pain in the *** task, I found out I forgot one entry...times 90 systems.

So ... a simple ed script to see if the machine needs the update, if so
find where I wanted the new line to be inserted, open up a new line, add
the new text, exit insert mode, write it, quit.  Move on to the next.

#!/usr/bin/ksh

for H in `/opt/bin/progthatlistssystems`; do
    echo === $H ===
    if ! ssh $H "host img-gf.example.org >/dev/null 2>&1"; then
        echo "must fix $H"
        ssh -t $H "ed /etc/hosts" <<-__ENDED
		/img-sf.example.org/
		a
		10.3.217.20     img-gf.example.org        img-gf
		.
		w
		q
		__ENDED
    fi
    sleep 1
done

Note that I tend to use a 4 space indent.  By the time we got to the ed
input, we should have been at 12 chars, so there's a TWO tab leading indent.

And while I have blundered my way through ed before, I wrote that
completely from memory from Michael's talk, no man page or book used. :)

Nick.



More information about the Semibug mailing list