Debug mode for running scripts?

Nils Kassube kassube at gmx.net
Sat Jun 9 15:29:24 UTC 2007


Jan Sneep wrote:
> > -----Original Message-----
> > From: ubuntu-users-bounces at lists.ubuntu.com
> > [mailto:ubuntu-users-bounces at lists.ubuntu.com]On Behalf Of
> > Nils Kassube
> > Sent: June 9, 2007 8:54 AM
> > To: ubuntu-users at lists.ubuntu.com
> > Subject: Re: Debug mode for running scripts?
> >
> > Jan Sneep wrote:
> > > Is there some way to run a script in a step-by-step way to
> >
> > see how far
> >
> > > the script goes before it runs into an error? Something
> >
> > like a verbose
> >
> > > step-by-step option of some kind?
> >
> > If you include
> >
> > set -o verbose
> >
> > to the script it will print every command before execution to
> > standard
> > output.
>
> So I would edit the script file and add that line at the top of the
> file?

Yes. But in the second line, not the first line. Then the start of your 
script looks like this:

#!/bin/sh
set -o verbose

> and by standard output you mean the screen or some actual file
> someplace?

Ooops -- that was wrong! It is not "standard output" but "standard error".

I'll try to explain how it works: Every command in Linux has 3 standard 
I/O files named "standard input", "standard output" and "standard error" 
(they are called files, but they are not files you find on your hard 
disk). If you run the command from a terminal, "standard input" is 
usually connected to the keyboard, and "standard output" and "standard 
error" are connected to the screen. If the command is run from some other 
program, it depends on that program, what is connected to these files.

Now, if you run your script with the "set -o verbose" option from a 
terminal, you will see both the normal output and the name of the 
commands run by the script.

The question is, what does your script in case of a failure? If it stops 
at some point, you can see the last executed commands on the terminal. If 
it goes into an endless loop, you will see the commands of that loop 
flying up your screen (until you stop the script).

If you want to save standard output and standard error to your disk, you 
can redirect the "standard output" to a file with "> outfile", and you 
can redirect the "standard error" to a file with "2> errfile". Then your 
command line looks like this:

myscript >myscript.out 2>myscript.err

If you want to save both standard output and standard error to the same 
file, the command would be

myscript >myscript.out 2>&1

But with these commands you will not see any output on your screen, 
because it is redirected to the file(s) on your disk. With the command

myscript 2>&1 | tee myscript.out

you can see the output on your screen while it is simultaneously saved to 
a file on your disk.


Nils




More information about the ubuntu-users mailing list