Saturday, February 25, 2012

Two Simple Tricks for Shell Error Handling

Here are two simple tricks that are easy to use and will make your scripts much more robust.

    Turn on -e mode (do you feel lucky - punk?)

    In this mode any command your script runs which returns a non-zero exitcode - an error in the world of shell - will cause your script to itself terminate immediately with an error.

    You can do that in your shebang line:

    #!/bin/sh -e

    Or using set:

    set -e

    Yes, this is what you want. A neat predictable failure is infinitely better than a noisy unreliable failure.

    If you REALLY want to ignore an error, be explicit about it:

    # I don't care if evil-broken-command fails
    evil-broken-command || true

    Oh and as long as you're messing with shell modes, -e goes well with -x (which I like to think of as shell X-ray).

    Like this:

    #!/bin/sh -ex

    Or like this:

    # turn -x on if DEBUG is set to a non-empty string
    [ -n "$DEBUG" ] && set -x

    That way you can actually see what your script was doing right before it failed.

    Use trap for robust clean-ups

A trap is a snippet of code that the shell executes when it exits or receives a signal. For example, pressing CTRL-C in the terminal where the script is running generates the INT signal. killing the process by default generates a TERM (I.e., terminate) signal.

I find traps most useful for making sure my scripts clean-up after themselves whatever happens (e.g., a non-zero error code in -e mode).

    For example:


    #!/bin/sh -e

    TMPFILE=$(tempfile)
    trap 'echo "removing $TMPFILE"; rm -f $TMPFILE' INT TERM EXIT

    echo TMPFILE=$TMPFILE
    echo hello world > $TMPFILE
    cat $TMPFILE

    # gives user a chance to press CTRL-C
    sleep 3

    # false always returns an error
    false

    echo "NEVER REACHED"

    Note that you can only set one trap per signal. If you set a new trap you're implicitly disabling the old one. You can also disable a trap by specifying - as the argument, like this:

    trap - INT TERM EXIT

Sunday, January 1, 2012

Digital Camera: A Buying Theory

I was wandering a lots of websites to find what camera suites my needs as well as my pocket. There are lot of brands to choose from and even more websites giving reviews of them. I was totally confused what to buy, where to buy.

I believe that some of the following steps can help a novice to buy a camera.

First is you should know what are your requirement. It depends on why are you really buying a digital camera. Is it for you to start taking hobby photographs, then i would say that one must consult a good photographer or a knowledgeable person in this field to gain some knowledge, about the lenses, the flash, the hardware that gives more sharp and clear images etc etc.

Other probable reasons are buying it for someone who just need a Camera to pic and shoot and capture some memories and they don't require much of the sophistication. For them you can buy some of the widely available ranges of camera's from Nikon, Canon, Sony, Olympus,Samsung, phillips, Kodak, etc etc.I have a good example to quote for this one. I have to buy a digital camera for my home so that if my mom and dad are visiting some place then they can easily get some pics and it should be easy to handle and i want the range to be reasonable as they dont need any high quality features.

Second is you should be careful about the brand you are going for. I would suggest everyone to go for brands and do not waste any time going for local products for this thing. The brand gives you high quality lenes as well as the hardware is of good quality, whereas this "always" does not imply for local products.

Third one is the Technical specificatons you are looking for. Check the optical and digital zoom. Check if the camera supports image stabilization and what kind of image stabilization is present, Hardware or software. You can check the aspect ratio and compare in different cameras.

The next point to consider is the battery life. Check how many photos can be taken from it. I want to mention important point that i dont prefer camera with a AA battery(but thats just me), So check what kind of battery is provided if you want to be choosy on that point too.

I hope this helps everyone..!!