Click to See Complete Forum and Search --> : [BASH] Test a script without running it


hop-frog
06-19-2003, 08:06 PM
Sorry to have three questions here in one day, but I've been doing a lot of bash scripting lately.

I working on a Bash script and I want to test it out without actually running it. I want to see if it has any errors in it. Since it takes about 45 minutes when I run it normally, I'd like to cut that time down just for testing purposes. Also since it is meant to run on a freshly installed system it interferes with itself when I run it more than once on the same computer.

Is there a way to run my script without really running it?

arn0ld
06-19-2003, 08:16 PM
set -n
see bash man page

hop-frog
06-19-2003, 08:47 PM
Thanks. I've be sure not to run that from console or I might not be able to turn of the computer.

Strogian
06-19-2003, 09:35 PM
Doesn't work from the console. You're safe. :)

bwkaz
06-19-2003, 11:24 PM
You can also bash -n yourscriptname, if you want. That accomplishes the exact same thing.

Also, there's set -x, which turns on bash's sort-of-the-same-thing-as make's -n option. With make -n, make prints out what it would run, but doesn't run it -- with set -x, bash prints out what it'll run, then does run it (slightly different, and not what you asked for, but possibly relevant anyway). Use set +x to turn it off.

And I'd think that if set -n worked on a terminal, then set +n would turn it off just fine, too. Of course, I don't know for sure. :)

Strogian
06-19-2003, 11:36 PM
Originally posted by bwkaz
And I'd think that if set -n worked on a terminal, then set +n would turn it off just fine, too. Of course, I don't know for sure. :)

I guess they might make that a "special case" or something, just so you wouldn't screw yourself too much. :) I dunno though.. you can't get out of a 'suspend -f' command without another terminal, AFAIK.

hop-frog
06-20-2003, 03:39 PM
Okay, no I'm confused. I thought it was working, but I guess it isn't.

I'm typing bash -n ./myscript from a freshly opened terminal. It doesn't show any output at all, even though I have tons of echo commands throughout. Could this feature be disabled by default in SuSE?

I've tried the other commands that you people suggested and they don't seem to work either.

Maybe I should instead be asking if there is something that will check my bash script to make sure there isn't any errors in it, but without actually executing the script.

Strogian
06-20-2003, 03:54 PM
Originally posted by hop-frog
Okay, no I'm confused. I thought it was working, but I guess it isn't.

I'm typing bash -n ./myscript from a freshly opened terminal. It doesn't show any output at all, even though I have tons of echo commands throughout. Could this feature be disabled by default in SuSE?

I've tried the other commands that you people suggested and they don't seem to work either.

Maybe I should instead be asking if there is something that will check my bash script to make sure there isn't any errors in it, but without actually executing the script.

Riight.. that is exactly what the -n option does. ;)

echo commands
Ok, you want to _not_ execute the script, right? That's what the -n option does, so those echo commands will _not_ execute. They will be parsed by bash to make sure there are no syntax errors, but they will not run. If you, say, throw a ( in there somewhere, you might get an error.

hop-frog
06-20-2003, 04:34 PM
Here is the exact output of various things I've tried. Each set is tried in a new rxvt window.

1)
$ ./myscript
...
This launches the script like it normally would run.

2)
$ set -n
$ ./myscript
$
This outputs nothing at all. Even when I sabotage the code.

3)
$ set -n ./myscript
$
Same as above.

4)
$ bash -n ./myscript
$
Same as above.

5)
$ set -x
$ ./myscript
...
This runs the code just like normal. It carries out all of the actions in the code. It is creating real files and real directories.

6)
$ set -x ./myscript
$
This returns no output.

Either I am running those commands all wrong or I need to rephrase my question:

What I am looking for is something that will run the script, show any messages that the script would normally show, report syntax errors like it normally would (if there were errors), but without actually carrying out the actions that are listed for real.

If a line says echo "hello" I want it to print hello. If a line says mkdir scripts I don't want it to actually create the directory, but it should look at that line and if there is already a directory called scripts it should print mkdir: cannot create directory `scripts': File exists.

Because of all of the various things this script does, if I run the script a second time for on the same computer then it will print out a zillion error messages.

arn0ld
06-20-2003, 05:00 PM
script contains:
echo hello
for x in

bash -n script:
line 3: syntax error: unexpected end of file
shell returned 2


What I am looking for is something that will run the script, show any messages that the script would normally show, report syntax errors like it normally would (if there were errors), but without actually carrying out the actions that are listed for real.

If a line says echo "hello" I want it to print hello. If a line says mkdir scripts I don't want it to actually create the directory, but it should look at that line and if there is already a directory called scripts it should print mkdir: cannot create directory `scripts': File exists.

clearly impossible except for certain syntax errors. script contains:
[ $A -eq 5 ] || EX="echo hello"
[ $A -eq 4 ] || EX="for x"
eval $E

A=4 bash script
A=5 bash script

bwkaz
06-20-2003, 08:07 PM
Originally posted by hop-frog
If a line says echo "hello" I want it to print hello. If a line says mkdir scripts I don't want it to actually create the directory, but it should look at that line and if there is already a directory called scripts it should print mkdir: cannot create directory `scripts': File exists. Nothing in bash that I know of does that. And this makes perfect sense, considering that bash doesn't control the behavior of any process that it launches. :)

What you seem to want it to do is pass something like bash's -n option to each and every executable that the script launches. No such option exists for many of these programs.

On top of that, bash couldn't do anything like that for you, anyway. How can it know the difference between /bin/echo and /bin/rm?

hop-frog
06-20-2003, 08:29 PM
I guess I'll just have to try my script and hope it works right the first time, because it would take a lot of work to undo everything the script does in order to run it (again after making fixes to it).

arn0ld
06-20-2003, 08:47 PM
if you put
set -x as the 1st executable statement in the script, it will "trace" script execution, producing a humongous amount of output to STDERR, which you can redirect into a file for debugging - make sure you have plent of free disk space in the file system