Dry Run – a quickie

While I was enjoying all the down time (Kitties Ho!), I reasoned that it was probably best not to end up waiting half an hour every time I run my scripts, especially when I want to just test some small change in logic. I consulted our resident bash expert and he handed me this gem.

function run() {
    echo "$@"
    if [[ ! $DRY_RUN == true ]]; then
       "$@"
    fi
}

Drop this in your scripts and prepend any commands you want to execute conditionally with ‘run’, e.g.:


dry_runner.sh

function run() {
    echo "$@"
    if [[ ! $DRY_RUN == true ]]; then
       "$@"
    fi
}

run echo "this is awesome!"

At the command line:

$ DRY_RUN=true ./dry_runner.sh
echo this is awesome!
$ ./dry_runner.sh
echo this is awesome!
this is awesome!

If you run it with DRY_RUN set to true it only echos out what it would do. Otherwise it both echos and executes the command.

Tags:

Leave a Reply

You must be logged in to post a comment.