Friday, July 2, 2021

Munging Standard Input

I was writing a shell script at work to extract a few lines from from some test output. It wasn't working like I expected so I examined the file: it had Windows-style linebreaks.

Converting "DOS" text files into Unix style is a hall of fame problem that's up there with "how do I rm a file with a name that starts with a minus sign?" and there are several fine solutions. The problem I have is when I run the script I don't want to have to remember do the conversion myself. The script should do it for me!

Here's my first pass:

tr -d '\r' | (
  while read line; do
    echo "Got a line: ${line}"
  done
)

Trust me that my real script did actual work, not just a silly echo. It is also several lines longer.

The tr filters the input stream to Unix-style if it's not already, and then pipes it to the real script action inside parentheses.

CHEERS: The caller doesn't have to convert input. Also works with plain old /bin/sh
JEERS
: Virtually the entire script has to live inside parens

Next I tried process substitution, which is possible with bash and zsh (though not Mac OS /bin/sh):

exec 3< <(tr -d '\r')

while read <&3 line; do
  echo "Got a line: ${line}"
done

CHEERS: The script doesn't need to be wrapped in parens!
JEERS: It has to read from alternate file descriptor 3 instead of STDIN. And it doesn't work with /bin/sh anymore.

How about I replace FD 0 (STDIN) with this new FD 3?

exec 3< <(tr -d '\r')
exec 3>&0

while read line; do
  echo "Got a line: ${line}"
done

CHEERS: The while loop and anything else in the script can just read from STDIN like usual
JEERS: We're still opening up this weird file descriptor 3 what we never use after line 2 directly

How about substituting FD 0 from the start? Will that break anything?

exec 0< <(tr -d '\r')
 

while read line; do
  echo "Got a line: ${line}"
done

CHEERS: No more stray file descriptors
JEERS: The first line is cryptic? Just kidding this is 100% excellent. I see no flaws.

Thursday, May 9, 2019

Surfing with Lee

Here I was sitting in the casino at my psychic's timeshare, when who should walk in but my old friend, a bail bondsman who I haven't heard from since our days in business services and asset management. He'd been in rehab, finished his degree and tells me that he's looking to get involved in medical coding services, or maybe insurance.
He handed me an LP, an archival release of a surf instrumental album Lee Hazelwood made but never released. It's an odd one but I kind of dig it. Is that an early drum machine?

Tuesday, January 15, 2019

SMOKIN': The Best of Brick Smokestack


This 1973 best-of was the final official release from The 'Stack. It also proved to be their best-selling and highest-charting. You may not have had it, but your friend's older brother did. 'Stack it!

Friday, August 17, 2018

HOLD ON! The Best of Joan Bannister

Poet/songwriter Joan Bannister's 1990 contract-fulfiller for Nonesuch. Who can forget all those times she opened for Bruce Cockburn at the Park West for an 'XRT show?


Whole Hog! The Best of Taihu Pig

A close-out bin score from 1988, this is, for most of us, all the Taihu Pig we're ever going to need.

Tuesday, June 13, 2017

Peng!

I ran across this circa 1969-1970 comic that should feel familiar to Stereolab fans:
I think the translation is a bit off. Here's my attempt (with help from Babelfish):
The Deadly Finger

So, you're a stand-up person, you love peace, order and the police…

Worked more than 20 years for the same company, popular with supervisors, no criminal record, no prosecutions.

You joke, yodel, read the Neue Zürcher Zeitung, believe in God and are in the shooting club…

The kids don't like you, especially the long-haired and disobedient, because…

You're a lieutenant in the army!

High time that you're dead!

Bang!

There's a bit more on "Cliff" in this post over at Dangerous Minds.

Tuesday, April 30, 2013

The Reggae Banana Consolation

Has it really been 2 years? I tell ya, cloud computing disaster recovery is worse than online live roulette!