Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
So, you want to print what the command will do (including the values of the $VARIABLES) without actually executing the command. I use something like your _run() function, sometimes with 'set -xv', but I would love a better way. –  Stefan Lasiewski Jun 2 '10 at 22:46
    
yeah, exactly that. I'm wondering if there isn't an internal variable you can set in bash so commands don't actually execute, instead of passing them through a function –  Andrei Serdeliuc Jun 2 '10 at 22:48
1  
There's no reason that this question should have been marked community wiki. –  Dennis Williamson Jun 2 '10 at 23:36
    
Bash is a shell. There is no reason for it to do dry runs. I don't think there is a better way of doing it than you've already done. –  d-_-b Jun 3 '10 at 0:31
add comment

2 Answers

up vote 2 down vote accepted

See BashFAQ/050 for a discussion of this subject.

share|improve this answer
    
Not an alternative, but it looks like an excellent resource for testing and working with longer bash programs. –  Andrei Serdeliuc Jun 3 '10 at 8:03
    
I rolled back the edit that added an anchor to a particular part of the linked page since the point of my answer, as stated, is to read the discussion as a whole rather than point to one specific how-to portion. The point of the linked page is generally to try to avoid putting commands to be executed into variables since there are a lot of gotchas. –  Dennis Williamson Jun 3 '10 at 9:34
add comment

I wanted to play with the answer from @Dennis Williamson's. Here's what I got:

Run () {
    if [ "$TEST" ]; then
        echo "$*"
        return 0
    fi

    eval "$@"
}

The eval "$@" is important here, and is better then simply doing $*. $@ returns all parameters and $* returns all parameters with no whitespace/quoting.

$ mkdir dir
$ touch dir/file1 dir/file2
$ FOO="dir/*"
$ TEST=true Run ls -l $FOO
ls -l dir/file1 dir/file2
$ Run ls -l $FOO
-rw-r--r--  1 stefanl  stefanl  0 Jun  2 21:06 dir/file1
-rw-r--r--  1 stefanl  stefanl  0 Jun  2 21:06 dir/file2
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.