Friday, September 16, 2005

Watch/view shell script lines during execution?

Watch/view shell script lines during execution?

> Any suggestions? I'm primarily running Solaris with occasional forays
> into Linux.

You didn't say which shell.

If it's a Bourne shell variant, you can use two variables

-v Print shell input lines as they are read.
-x Prints lines after eexpanding values

There are several ways to do this

1)
sh -x command arguments
sh -v command arguments

You can combine this with
sh -xv command arguments

2) start the first line with
#!/bin/sh -x
#!/bin/sh -v
#!/bin/sh -xv

3) In the middle of the script
set -x (turns it on)
set +x (turns it off)
set -v
set +v

For csh/tcsh, the first two techniques work

csh -xv command arguments
#!/bin/csh -xv

in addition, you can use -X or -V, which turns the flag on before
executing ~.tcshrc/.cshrc

For in-the-middle-of-the-script, use
set echo
set verbose
unset echo
unset verbose

OOOOOOOOOOOOORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

For ksh variants, set PS4:

#!/bin/ksh
PS4='[$LINENO]+'

Create a function:

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function debugOn {
typeset -ft
$(typeset +f)

}

And include a getopts such as:

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
while getopts "d" C
do case $C in
d) . debugOn;;
esac done

The advantage of this over the "-x", "set -x", echo, etc already
mentioned is that you don't have to insert/delete junk when you
want to see what's going on and it works for functions where the
"-x" on the first line doesn't.

0 Comments:

Post a Comment

<< Home