views:

721

answers:

2

Is there a way to set the debug mode(set -x) on a KornShell script globally? Currently it seems I have do something like the following:

a(){
   set -x
   #commands
}

b(){
   set -x
   #more commands
}

set-x 
a
#commands
b

I would really like to only have to call the set-x command in one place.

Note: This is all in KSH88 on AIX.

Example:

dev2:/home/me-> cat testSetX
#!/bin/ksh
set -x

a(){
   echo "This is A!"
}

b(){
   echo "This is B!"
}

a
echo "Outside"
b
dev2:/home/me-> ./testSetX
+ a
This is A!
+ echo Outside
Outside
+ b
This is B!
dev2:/home/me->
A: 

Add it to your shebang line:

#!/bin/ksh -x

Or set it at the top of your script:

#!/bin/ksh
set -x

Or start your script from the command line:

ksh -x script_name
Dennis Williamson
Unfortunately none of these work for me. I see only the names of the methods. It may be because I'm using ksh88.
C. Ross
+2  A: 

This is ksh88 on an HP-UX machine:

me@host ..dev/
$ cat ./test/verbose
#!/bin/ksh
set -x

hello() {
  print $1
}

hello kapow!
exit

[email protected]/
$ ./test/verbose    
+ hello kapow!
+ print kapow!
kapow!
+ exit

It sure looks like that works fine. I validated that it also works with a "set -x" anywhere before the first function call.

I moved to an AIX system, and experienced the problem you described. When functions are defined as either function a { or a() { in AIX ksh88, the set -x doesn't appear to carry forward into the function-local scope. Switching to ksh93 on the same AIX box, functions declared using the new function a { syntax also don't carry the outer set -x into the inner scope. However, ksh93 behaves like POSIX sh (and ksh88 on other platforms) used to behave, carrying the set -x through to the function when the function is defined in the old a(){ method. This is probably due to the backwards compatability in ksh93, where it tries to emulate the old behavior when functions are defined the old way.

Therefore, you might be able to temporarily switch the interpreter over to ksh93 for debugging purposes, and then switch back to ksh88 if you don't like having the longer arrays, associative arrays, floating point math, namespace support, and rougly 10x improvement in execution speed which ksh93 brings. ;) Because it looks like the answer is "no, you can't do that" with ksh88 on AIX. :(

dannysauer
Thanks for the great research, I'll see about getting upgraded to ksh93, but in my environment ...
C. Ross
IBM provides ksh93 and ksh88 in the same package (at least they do on the AIX 5.3 and 6.1 systems that I have looked at); the binary is just called ksh93. If that helps... :)
dannysauer