Link to home
Start Free TrialLog in
Avatar of Les Ostness
Les OstnessFlag for United States of America

asked on

How to test a bash script without it making any changes?

Is there a way to test a bash script in a way that it doesn't actually make any changes but it will show you the changes it would have made (or display the commands it would have executed)?
These are the Linux versions I'm using.
SUSE Linux Enterprise Server 11 SP4
Oracle Linux Server 7.6
Avatar of David Favor
David Favor
Flag of United States of America image

No.

Just clone a copy of the script. Make your changes. Test the cloned script.
Copy the script and replace every command that performs an action by putting echo in from of it, then run that copy. It takes some work but you can do it.

So:

cp ${file} /var/tmp

would become:

echo cp ${file} /var/tmp

and when running it, it would echo the cp command with the real file name that would have been copied.

Adding "read line" statement to 'pause' the script at certain points can help as well:

echo "enter to continue" ; read blah
Echo the prior comments, from my point it is difficult to answer without knowing what the scrip does.

You can add a check whether this is a test or a real

You would then on a test
If real, do the stuff, if test, echo this is what would have been done.

Note this is not a real test as it lacks the ability to confirm if executed, would be permitted..
ASKER CERTIFIED SOLUTION
Avatar of Dr. Klahn
Dr. Klahn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Dr. Klahn,

> "The only debugging in a bash script is what you put in yourself.  Sad but true."
How can you say that?  Are you not aware of bash's debug switch (i.e. -x)?
Either put it on the shebang line like this:
    #!/bin/bash -x
or run the script like this:
    bash -x ./myscript.sh

I know that doesn't make it a dry run (which is what Les seems to want), but it can be useful for debugging, right?
The -x is useful to a point if not mistaken it validates syntax and includes verbose output
A shell script relies on other commands which is why the suggestion deals that you have to setup the tests. Validation before executing....

A dry run has to be a viable option in the commands you run/use.
I don't know if this will be of any use,

But when I need to debug or test a script I wrote or modified;

If it's just moving (deleting etc) non critical files - I run it against a test directory

If there's a chance something could get messed up - I run it in a virtual machine
I do this all the time in PERL, by adding a --dr|--dryrun option to all my scripts.

Also a --d|--debug option.

The --debug option prints debug info.

The --dryrun option skips things like system() calls.

While there's no built in facility to provide a dryrun in any language, it's fairly trivial to add this in as part of your normal script templating you use.
Avatar of Les Ostness

ASKER

Thank you all for your input. You guys are all awesome!!!