Operating System - HP-UX
1846883 Members
3404 Online
110256 Solutions
New Discussion

Stepping thru shell scripts

 
SOLVED
Go to solution
Sanjay Tailor
Frequent Advisor

Stepping thru shell scripts

Hello,

I have some shell scripts that I have written. I always end up running the scripts to see if it works the way I want it to.

Is there a way to step through each line in the script to see what it is doing? And also see the value of the variables etc etc... I guess debug the script?

Thank you,
Sanjay
11 REPLIES 11
Sridhar Bhaskarla
Honored Contributor

Re: Stepping thru shell scripts

Sanjay,

sh -x your_script

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Mark Greene_1
Honored Contributor

Re: Stepping thru shell scripts

depends on the shell. ksh has the -x option:

ksh -x [script file name]

where it will display each line as it is run; you can also add the following to the scripts to get line numbers to display:

PS4='${0##*/}:$LINENO: '
typeset -ft $(typeset +f)


HTH
--
mark
the future will be a lot like now, only later
Sachin Patel
Honored Contributor

Re: Stepping thru shell scripts

Hi
If it is csh script then
#csh -x script_name

Sachin
Is photography a hobby or another way to spend $
Uday_S_Ankolekar
Honored Contributor

Re: Stepping thru shell scripts

Hi,

sh -x "script name" would the fast and best way to check.

-USA..
Good Luck..
Craig Rants
Honored Contributor

Re: Stepping thru shell scripts

To see what is going on being the scenes I usually call a script with sh -x script.sh. You get a lot of output so you may want to pipe to and output instead of STDOUT. As far as stepping through a script, -v will show the commands as they are executed, -t will run one command and quit, but I don't know of anything to step through a script.

HH,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Chris Vail
Honored Contributor
Solution

Re: Stepping thru shell scripts

In the ksh, you can put set -x on some line, this is similar to the sh -x command mentioned by others. However, this won't help you if you need to step through a loop and watch conditions change.

The only way I know to do this is to use a read command (example: read trash) in the middle of the loop. This stops the process and waits for an key.

When you're through with debugging, just do a global search and delete on the 'read trash' commmand.

Good luck with your debugging.

Chris
Steven Sim Kok Leong
Honored Contributor

Re: Stepping thru shell scripts

Hi,

In addition to what has been highlighted above, if you want to see the states of all environmental shell variables (exported ones) and script variables during your script debugging, you can insert set command statements in your script. Simply insert "set" after the line you want to debug.

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
fg_1
Trusted Contributor

Re: Stepping thru shell scripts

Sanjay

Use the -x option in the 1st line of the script where you usually specify the shell you are writing in:

ex. /usr/bin/ksh -x
/usr/bin/sh -x
/usr/bin/csh -x
/usr/bin/bash -x
etc.....

Steven is right about the "set" command, use it preceeding any of your variables to see how they are utilized in the script.

Good luck
fg_1
Trusted Contributor

Re: Stepping thru shell scripts

My bag on the set command.

Use after the variable line in the script, not preceeding it. Sorry about the mistake (no flogging at this time).
Sanjay Tailor
Frequent Advisor

Re: Stepping thru shell scripts

Hello,

Thank you for all your replies. I have tried the -x option and it works very good. The set command also works great. I know the set command displays all the variables, is there a way to only display only the local variables that I use in the script?

Thank again,
Sanjay
fg_1
Trusted Contributor

Re: Stepping thru shell scripts

Sanjay

If you want to display local variables, I believe that if you define the set command within a function area of the script you will then display the local variables:

Example using ksh.

function asystem {
print in function $0: $1 $2
var1="in function"
}

var1="outside of function"
print var1: $var1
print $0: $1 $2
asystem systemarg1 systemarg2
print var1: $var1
print $0: $1 $2

Once you invoke this script the function asystem changes the value of the variable var1 from "out of function" to "in function", that change is known outside the function while $0, $1, & $2 will have different functions within the script.

Hope this helps.