I’m posting this in case I’m not the last person to realize this. While I’ve used the Unix sh longer than DHH has been alive, I either never realized or have long since forgotten that it supports here documents. Example:
ruby <<EOD | sort | uniq -c | sort -n
Dir['*'].each do |name|
puts name.split('.',0)[1] || '<null>'
end
EOD
Dear Sam,
You are not the last person to realize this. I am.
No, I am. Thanks for sharing. Perhaps more people would know about this feature if it had a more sane name, like, for instance “inline documents"? The word "here” is fun and giggly once you know about it, but it’s completely non-discoverable unless you actually know what to search for (or read the whole frikkin' sh manual before going to sleep every night).
Brings back fond memories of a couple of build-masters we had at a company I used to work for. They never bothered to migrate to something modern like perl - surviving entirely in shell (in their case korn) with here doc’d AWK scripts embedded right in the shell scripts. If was a godawful mess but it was a 20 year old functional mess.
Good stuff in any case. Very few learn to make full use of Unix Shell scripting, even those who spend all their time on Unix. A few good examples now and then on your weblog aught to raise the average slightly. :)
Used to find the huge Redhat startup scripts somewhat annoying, when I was still using Redhat. Much less than brilliant use of shell scripting.
OK, so I can’t resist. A slightly more general version in ancient script:
find -type f | awk 'BEGIN{n["<null>"]=0;} { sub(/.*\//,""); split($0,a,/\./); i=length(a); v=a[i]; n[(1 == i) ? "<null>" : v]++; } END { for(v in n) print n[v] " " v }' | sort -n
that’s cool - ruby obviously copied the syntax when providing the ability to quote a large amount of text e.g.
very_large_text_string = <<END_DELIMITER
Welcome! You seem to be new here. In order to minimize spam, your
comment will not appear on the weblog until it has been moderated in by
the owner of this weblog
Be aware that all links you enter in your comment or as your URI will
be marked as
NOFOLLOW
and therefore not indexed by major search engines like Google, MSN, or
Yahoo!.
Be advised that your ip address is
being tracked, and this weblog owner is very diligent about removing spam and
adding both the ip address and the address of any
urls referenced by such posts to his blacklist
END_DELIMITER
I have never read the full man page for sh(1), but I knew this because I learned UNIX from Kernighan and Pike’s The UNIX Programming Environment, which I can warmly recommend to anyone who didn’t.