cancel
Showing results for 
Search instead for 
Did you mean: 

Debugging scripts KSH

SOLVED
Go to solution
Enrico Venturi
Super Advisor

Debugging scripts KSH

Hello colleagues,
are there any tools to execute step by step a KSH script?
sorry for the stupid question :-)

Enrico
5 REPLIES
Steve Steel
Honored Contributor

Re: Debugging scripts KSH

Hi


See set options in manpage and put

At the start of the script



-p Disables processing of the $HOME/.profile file and
uses the file /etc/suid_profile instead of the ENV
file. This mode is on whenever the effective uid
(gid) is not equal to the real uid (gid). Turning
this off causes the effective uid and gid to be
set to the real uid and gid.
-s Sort the positional parameters.
-t Exit after reading and executing one command.
-u Treat unset parameters as an error when
substituting.
-v Print shell input lines as they are read.
-x Print commands and their arguments as they are
executed.
- Turns off -x and -v options and stops examining
arguments for options.
-- Do not change any of the options; useful in
setting $1 to a value beginning with -. If no
arguments follow this option, the positional



Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Robert-Jan Goossens
Honored Contributor

Re: Debugging scripts KSH

add the line

set -x

in the second line of your script.

Robert-Jan.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Debugging scripts KSH

Hi Enrico:

Steve has listed some of the more useful options.

You can leverage these without modifing your script by simply doing, for example:

# ksh -x yourscript

Another useful technique is to have 'trap' statements in your script for DEBUG and ERR. For example:

# trap 'echo Debug handler entered' DEBUG
# trap 'echo Error handler entered' ERR

See the 'sh-posix' or 'ksh' man pages for more information.

Regards!

...JRF...

Massimo Bianchi
Honored Contributor

Re: Debugging scripts KSH

Hi,
another little trick.

If after setting the debug mode (i call it so) you want to log everything, you can use the "script filename" command.

this utility let you dump all video output (std out and std err) and your input to the file mentioned, very usefull for debugging purpose.

Massimo
James R. Ferguson
Acclaimed Contributor

Re: Debugging scripts KSH

Hi (again) Enrico:

Here's another example of how you might leverage the DEBUG signal to monitor your script. Remember that the trap could be set and reset around regions of your code. Hence, consider this example

#/usr/bin/ksh
echo "hello world!"
[ X"$1" = X-d ] && trap 'echo "@line-${LINENO}' DEBUG
echo "where am I?"
date
[ X"$1" = X-d ] && trap '' DEBUG
echo "debug mode off"
date
echo "where am I?"
exit 0

Run the script this way:

# ./my.sh

...then, with debug mode on:

# ./my.sh -d

Regards!

...JRF...