2011/03/14

Avoiding unwanted IO redirection after echo in command line scripting.

If you happen to work with computers and you are a programmer or other power user, you may have cause to work with command line scripts or, as they used to be called, batch files. This little tidbit applies to XP, Vista, 2k8, and probably Windows 7, and it may even apply to some of the *nix command prompt / bash / things as well. But I've never, in years and years of doing this seen it documented (as an error) or seen the workaround.


Try this command at the prompt:

echo test 2>test.txt


One might expect to see nothing on the console and then if you entered:

type test.txt

you might expect to see "test 2"


In fact, you see "test" on the console right after the echo and text.txt is empty!


What happened? The 2> is redirecting any error messages from echo test to the file test.txt. It is being interpreted as a stderr redirect and that leaves no > to send "test" into the file. Experienced programmers won't be shocked by that, although I expect you might be tripped up by it as I was. In my case, I was using a web script to build a batch file that a user could download and execute after filling out a form on the web page; it's an easy / tricky way to build a "program" on the fly which is customized by the user. Some of the user input would end up being echoed into a file on the local machine. When the users happened to enter anything ending with a number, it caused the script to fail... something I didn't see comming because I didn't realze what they would enter.


Now, how to avoid it? LOL this one is what surprised me:

(echo test 2)>test.txt


Can you believe it? That actually works! So the moral of the story is that if you are going to be echoing things to a file, you should probably enclose all of those echo commands in parenthesis as a habit.


Anyway, I was just amazed to have learned that little bit after all these years, and thought I should share it incase someone else finds it useful. Yeah... right.

2011/01/17

Personal Freedom

If freedom is freedom of choice, then we are more free in a huge mega super market on the aisle with hundreds of different types of cereal, or tampons, or whatever.

If freedom is freedom of time to our selves, then we are more free in Trader Joes or the corner 7-eleven with one type of toilet paper and one type of cat litter.

Your freedom to swing your arm stops where my nose begins but my freedom to kill you depends on my countries leaders deciding what evil you are up to, and our incompetence at finding any better way of stopping you from doing harm to us and ours. The lines are not always clear, but they are crystalline in a time of action, following orders, executing the protocol, performing the function we have practiced for again and again. Persons defending personal freedom.

When I joined the Navy, my father asked me how I could give up my freedom. I said I was happy to give up my freedom to go hungry, to be without medical care, and my freedom to find a way to support my fiancĂ©… which I had not found any way to do before I talked to a recruiter. She was free to leave me after I joined.

When I was in the Navy, I knew when I was free (on leave) and when I was not (on duty) and I could manage both; enjoy either. Now, as a “bread winner” I am never free of the drive to earn more, and guilt follows every moment away from my pursuit of the dollar. Abuse victims find freedom from the abuse in their minds, but there is no freedom from our own tyranny.

It’s been said that a King has more freedom than a slave and then it’s been said that a slave has more freedom than a king.

It’s a silly little word, with no apparent meaning. And yet I’ve fought for it, without understanding; and comprehend it less with each passing day. It eludes me as I age. Will I eventually loose my freedom to live? Or find freedom some day in death?

Am I free to stop thinking about this non-sense and get back to work?

2010/12/27

The $50 lesson

A good friend of mine (who happens to be a conservative republican) send this email to all his friends, wisely using bcc for the recipients:

The $50 Lesson

I recently asked my friends' little girl what she wanted to be when she grows up.

She said she wanted to be President of the United States .

Both of her parents, liberal Democrats, were standing there.

So I asked her, "If you were President, what would be the first thing you would do?"

She replied, "I'd give food and houses to all the homeless people.."

Her parents beamed.

"Wow...what a worthy goal," I told her.

"But you don't have to wait until you're President to do that.

You can come over to my house and mow the lawn, pull weeds, and sweep my driveway, and I'll pay you $50.

Then I'll take you over to the grocery store where the homeless guy hangs out, and you can give him the $50 to use toward food and a new house."

She thought that over for a few seconds, then she looked me straight in the eye and asked, "Why doesn't the homeless guy come over and do the work, and you can just pay him the $50?"

I said, "Welcome to the Republican Party."

...............Her parents still aren't speaking to me................

And here is my reply:

I like you (a lot actually) and I appreciate what you are trying to say here, but I find myself painfully conflicted and I wish to respond in a way that is meant to get the conflict off my chest and also to try to help you understand my position, and how I see the world. E.g. My version of the truth. I hope you will not take offence, as none is meant and that you will simply see my reply here as a friendly challenge.

Although I generally agree with the idea of the post, I see two logical fallacies, to which I would enjoy hearing your reply:

1. This assumes the homeless person is capable of performing the work. Besides the mental health issues that could easily prevent the average (long term) homeless person from knowing /how/ to do the work, there are issues of physical strength and logistics which could also stand in the way. The most obvious of these is how does the homeless person get to the work place?

2. You also assume that anyone would be willing to employ a homeless person even if they could possibly do the work. Frankly, that is the fallacy I find most annoying about this. One the one hand, republicans will say "oh, the homeless should just work to earn a living" when in reality, those same republicans would NEVER hire a homeless person to do any work... no work at all... period. Ever.

So let me challenge you to answer those two fallacies to the same list of people to whom you sent this first email, and further, let me challenge you to hire a homeless person to do ANY work AT ALL!

Until you have risen to either of those challenges, I call hypocrite.

Remember, your real friends are the ones who will, hopefully politely, tell you when they think you are wrong. I could just forget about it, or ask you to simply not send me republican based emails, but I enjoy debate, and I like to try to bring everyone, myself included, to a middle ground.

I hope you don’t mind that.

Yes, we will all die, but HOW? When? And how long can we avoid it?

The National Safety Council has released it's 2010 list of "what's gonna get yah" based on data from the 2006 census and health statistics:
http://www.nsc.org/news_resources/Documents/nscInjuryFacts2011_037.pdf

So here is the break down:

0. Dying from Death. You have a 1:1 chance of dying from something. Get used to the idea. Your only hope is putting it off as long as possible.

1. Heart Disease will take 1 in 6 of us. Tick Tock. It's more common in Women than most people think. There are lots of simple things you can do to reduce your risk of dying this way:
http://www.heart.org/HEARTORG/Conditions/HeartAttack/PreventionTreatmentofHeartAttack/Lifestyle-Changes_UCM_303934_Article.jsp

2. Cancer, The Big C, is a close second, taking 1 of every 7 of us. Eat organic, avoid irritants, injury, the sun, and the amazing number of products containing possible carcinogens.
http://cancer.about.com/od/causes/tp/topreventcancer.htm

3. Stroke gets 1 in 28. Note that stroke (blood not flowing) is different than heart disease (blood not pumping). Avoid long periods of not moving followed by sudden activity, keep regular aspirin on hand.
http://www.ninds.nih.gov/disorders/stroke/preventing_stroke.htm

Which brings us to my personal favorite, which is UP this year from 5 to 4:

4. Motor-vehicle Accidents kills 1 out of every 85 of us. Think about that. If you love 85 people, one of them is going to die on the roads at some point. I personally know 2 people already who died in cars, but lucky for me, I didn't love either of them. The first spun out on "black" ice in a car going WAY too fast on a windy road in Oregon and was hit by a flat bed truck. They had just passed our school bus and all us kids got to see a good lesson on the consequences of unsafe driving. Jay was an ass, but it was still sad that he died. The second was my old bosses daughter... he was also an ass, but no one deserves to loose a daughter. Actually, technically, she wasn't killed; but brain dead is dead in my book. They held on to her body for a while before they unplugged her.

Airplanes kill 1 in 5,862, Lightning and earthquakes are down in the 1 in 100,000 range. Terrorism doesn't even make the list this year. It was in the 600,000's last time if I remember correctly.

One terrorist attack changed our laws, personal freedoms, and way of looking at the world forever. 9/11 killed less than 3,000 people. About 60,000 people die on the roads every single year. More than 40,000 per year on the freeways and then an unknown additional number on surface streets. And unlike heart attack, cancer, and stroke, cars kill young people more often than old.

Have any of us checked out any of these sites?
http://www.safercar.gov/
http://www.fmcsa.dot.gov/
http://www.nhtsa.gov/

You may notice a bias in those .gov websites: They concentrate on the behavior of the driver, and give second place to the safety of cars. Why? Industry pressure? Pocket Politicians? Perhaps; but I think the truth is more interesting: As cars become safer, drivers adapt and take greater risks, eliminating the life saving effect of the industry regulations. Don't agree? Ok, but read these before you decide:
http://www.be.wvu.edu/divecon/econ/sobel/NASCAR/
http://en.wikipedia.org/wiki/Unsafe_at_Any_Speed#.22The_Peltzman_Effect.22

Guess what? Despite all the improvements in car safety since the '60s, the death rate has stayed within a percent of the current number! When it comes right down to it, the 4th biggest killer in our country is:

YOU (and me)

...when we drive.

Please consider these possibilities:

1. Try to find a job that allows you to telecommute or just work from home. It's green, it's frugal, it's safer. It took me 10 years of working on my boss to do it, but I finally got to telecommute, and he is as happy with it as I am.

2. On the freeway, don't bunch up with the other cars; find the empty space between the lemming packs and stay as far away from the other cars as you can. Leave more space in front and look for the "escape routes" you can aim for to avoid a collision. If someone tailgates, just speed up or move over if it's safe, otherwise tap your brakes three times; the goal is space, not enforcement or "teaching anyone a lesson".

3. Driving is a full occupation time. No talking, texting, eating, primping, dreaming, or raging. (yes, I know I'm a hypocrite) To help stay focused, make a game out of playing "what if" and thinking about your response to unexpected dangers. Be afraid every time you drive. You should be.

4. Mass transit is many times safer than individual commuting. Take a train, bus, etc... Even in California, it can be done. The extra walking is healthy.
http://maps.google.com/help/maps/transit/index.html

I'm preaching to myself as much as anyone else here... I hope we listen.