Implementing dry-run in bash scripts

Posted by Apikot on Server Fault See other posts from Server Fault or by Apikot
Published on 2010-06-02T22:32:40Z Indexed on 2010/06/02 22:34 UTC
Read the original article Hit count: 412

Filed under:
|

How would one implement a dry-run option in a bash script?

I can think of either wrapping every single command in an if and echoing out the command instead of running it if the script is running with dry-run.

Another way would be to define a function and then passing each command call through that function.

Something like:

function _run () {
    if [[ "$DRY_RUN" ]]; then
        echo $@
    else
        $@
    fi
}

_run mv /tmp/file /tmp/file2

DRY_RUN=true _run mv /tmp/file /tmp/file2

Is this just wrong and there is a much better way of doing it?

© Server Fault or respective owner

Related posts about bash-scripting

Related posts about dry-run