let LIMIT=50
function check {
if [[ LIMIT -eq 50 ]]; then
print LIMIT OK
else
print "LIMIT changed!"
fi
}
trap check DEBUG
print $LIMIT
LIMIT=$((LIMIT + 30))
trap - DEBUG
Can anyone tell me how debugging is accomplished??
What shell are you using?
In recent versions of ksh, trap action DEBUG
causes action to be executed just before each simple command the shell executes after that until the trap is removed. The DEBUG trap action is not inherited by subshells nor in functions. To get a better view of what is going on, you might want to try the following modification of your code:
#!/bin/ksh
let LIMIT=50
function check {
printf "In check: %s\n" "${.sh.command}"
if [[ LIMIT -eq 50 ]]
then print LIMIT OK
else print "LIMIT changed!"
fi
}
echo 1
trap check DEBUG
echo 2
print $LIMIT
if [ $LIMIT -gt 40 ]
then echo 3t
else echo 3f
fi
LIMIT=$((LIMIT + 30))
echo 4
trap - DEBUG
echo 5
Note that the compound variable .sh.command
is the command that is about to be executed. The above script produces:
1
In check: echo 2
LIMIT OK
2
In check: print 50
LIMIT OK
50
In check: '[' 50 -gt 40 ']'
LIMIT OK
In check: echo 3t
LIMIT OK
3t
In check: LIMIT=80
LIMIT changed!
In check: echo 4
LIMIT changed!
4
In check: trap - DEBUG
LIMIT changed!
5
2 Likes
Thanks Don, your post was useful.