Operating System - HP-UX
1820614 Members
1787 Online
109626 Solutions
New Discussion юеВ

Debug script shell ( posix )

 
SOLVED
Go to solution
Paulo Afonso Bruno
Occasional Advisor

Debug script shell ( posix )

Hi

I don?t remember how debug script or trace all the task of script

thanks

Paulo
4 REPLIES 4
Alan Riggs
Honored Contributor

Re: Debug script shell ( posix )

set -x turns on execute tracing. set +x turns it off.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Debug script shell ( posix )

Hi:

Use the -x and/or -v options.

-v to print shell input lines as they are read.

-x to print commands and their arguments as they are executed.

Thus:

# sh -x -v
...JRF...
Timothy Czarnik
Esteemed Contributor

Re: Debug script shell ( posix )

Paulo,

You can also just set the -x on the first line that specifies your shell:

#!/usr/bin/ksh -x

-Tim
Hey! Who turned out the lights!
Ralph Grothe
Honored Contributor

Re: Debug script shell ( posix )

Hi Paulo,

what the others wrote is really the usual way to do something what comes anyway close to debugging as far as shell scripts are concerned.

Actually I prefer piping stdout and stderr directly into more

$ sh -v -x myscript.sh 2>&1 | more

Apart from this you can do it the old Fortran way (before the advent of debuggers)
in spreading echo and read statements in your code to mimic breakpoints.
Since you don't have to recompile your code each time, as you would have if you didn't use an interpreted language, I would say this is acceptable.

Another option may be the following link to the Oreilly FTP server, where you can download from some wrapper code that acts as a debugger for the Kornshell.
Just get the tarball from there:

ftp://ftp.oreilly.com/published/oreilly/nutshell/ksh/

But if you want to do some more serious scripting none of the Unix shells is really an option IMHO because they all lack the possibility to modularize the code.
In the long run you alway end up cutting and pasting bits ad nauseam.

Thus, I would advise to spend some time in getting to know Perl.
It really pays, and with Perl you still can do (almost) all what you would do from shell scripts.


Madness, thy name is system administration