Verse Of The Day

Tuesday, December 05, 2006

A Few Unix Shell Tricks

If you want to set a value in a shell script, but only if it isn't already set (e.g. set a default value but let it be overridden), try this. The following command will set PROJECT to be mqbridge, unless PROJECT is already set. In that case, the current value overrides. I have used this trick in ksh and bash. Not sure what other shells it works in.

export PROJECT=${PROJECT:-"mqbridge"}

Redirecting stderr to stdout: If you are trying to capture output of a command into a file, you will notice the regular output goes into the file, but errors go directly to the screen instead. If you want to capture both, use shell redirection like this (2>&1) after your command.

For example, to have all output go to the screen and to a file, you can do something like this: 2>&1 | tee output.log

No comments: