Friday, December 9, 2016

Consequence without compassion

There are some things in this world that are unforgiving. This post is about a couple of things that you can type into a computer's command line with devastating results.

Just today, I needed to use a file named SROSTERS.TXT, which had been written by a DOS program on a Windows machine. But, I needed to use it on my MacBook Pro. It was important to today's work, but I noticed that each of its 33,000+ lines ended with a carriage return, line feed. Those two characters are invisible, so I only noticed it because one line of interest was exactly one character longer in the file than the equivalent line produced on the Mac, although they appeared to be identical.

There is a handy Bash command that will remove all the carriage return characters, leaving only a line feed at the end of each line, which is what the Mac expects. So, I set out to use that command:
cat SROSTERS.TXT  |  tr  -d  '\r'  >srosters.txt
 The cat command outputs the file contents to standard output, which is then piped (notice the vertical bar aka "pipe") into the standard input of the tr (translate) command, with the option to delete all  carriage return characters (-d '\r'). It was my intention that the output of the translation would go into a new file named srosters.txt which could then be used on the Mac.

Now the greater-than sign (>) in Bash has two different roles. It is followed by a file name, and if the file doesn't already exist, it is created (empty) before the command runs, which was my intention. But, if it does exist, it is truncated to an empty file, again before the command runs.

In my mind, based on years of experience using UNIX® and various linux systems, the two file names SROSTERS.TXT and srosters.txt would be for different files. Not so on the Mac's OS X version of linux. These two* names are both names for the same file!

So, when I pressed the enter key, my computer first truncated the file to an empty file, then sent its contents (also empty) through the pipe into the translate command whose output went into the file (still empty!).

And that is how I experienced a severe consequence, with my computer expressing no compassion.

You know, the greater-than sign has bitten me before, so maybe I should have been more careful.

As a graduate student, 30 years ago at the University of Calgary, I had spent most of a day working on a class assignment. When I finished at six o'clock, I did what I ordinarily did at the end of a day, which is to type this command to remove all of my backup files:
rm  *.bak
This would remove all files whose names ended with ".bak" and that is what it had done every day before. But that night, I was in a hurry, and my left pinkie finger didn't leave the shift key quite fast enough as my right ring finger went for the period key. So, that key got shifted and the command that I actually typed was instead:
rm  *>bak
Ouch.  If only I'd had time to look at the command line before pressing the enter key, but my fingers were on automatic. After running the command, I did, as always, check to see that the backup files had indeed been removed. To my astonishment, all of the files were gone, and there was a new one, named bak, which was empty. The rm (remove) command removed all of the files (as specified by the wild-card *) and sent its (empty) output into the newly created file named bak. Just as my command specified. Again, no apologies from the computer!

That night, I learned that sometimes you can do a project better the second time you try it. I had to have the assignment done, so I stayed another hour or so and re-wrote all of the code from memory.

Today, I'm going to resolve my problem by having a friend who works near my desk in the office find a USB key in my drawer and email me the file contents.

A bonus story, where a great deal of much-appreciated compassion was shown, from 20 years ago, when I worked in the Advanced Technology Group of WordPerfect Corporation as a senior scientist. Our administrative assistant (who I will call Virginia) was having some trouble with her computer and emailed the group a screenshot** of a rather ominous warning message. I flippantly fired back an email message saying (as best as I can remember)
It looks like you have the dreaded M$ Windows virus on your machine. It's easy to get rid of. Just type DELTREE C:\WINDOWS and that'll take care of it.
About half an hour later, when I'd forgotten all about my little joke, I was called into my boss's office. He tried to keep a straight face as he reprimanded me. Virginia hadn't seen my message as a joke, and had issued the command, which completely removed the operating system from her computer. A technician came and it took most of the afternoon to get her machine working again.

The compassion came as my boss chose to forgive me, and we still laugh about it when our paths cross.

* And not just these two names, but all of the 2,048 possible variations, mixes of upper and lower-case letters, of this file name. Whichever of the variants you use, you will be referring to the same file.
** Unfortunately, I no longer have access to that email system, or I could have displayed the warning message in this blog post.