1830619 Members
2516 Online
110015 Solutions
New Discussion

trap DEBUG

 
Andreas Tsamis
Advisor

trap DEBUG

I have the following lines in my .profile
echo $- | /usr/bin/grep "i" > /dev/null 2>&1
if (( $? == 0 )); then
function history_to_syslog
{
typeset -i stat=$?
typeset cmd
cmd=$(command fc -ln -0)
Session:$$, Status:$stat Runned: $c
md"
MY_TTY=`/usr/bin/tty | /usr/bin/sed 's/\/dev\///'`
MY_ID=`/usr/bin/who | /usr/bin/grep "${MY_TTY}" | /usr/bin/awk '{print $1}'`
/usr/bin/logger -p daemon.notice -t "euid: ${LOGNAME} id: ${MY_ID} $$" Status ${
stat} PWD ${PWD} TTY=`tty` \'${cmd# }\'
}
trap 'history_to_syslog' DEBUG
fi

i use it to log all the commands to syslog.
If i run this script in a bash or ksh environment with HISTFILE=~/.bash_history it runs without a problem . if i change to /usr/bin/sh shell (change using su - root) halts after executing a command like it goes through an infinite debugging loop.
ie if i type ls is shows me the results but i have to press Ctrl-C to get a command prompt. If type a new command i get at the end a command prompt but after that the same problem occurs with additional commands.
5 REPLIES 5
Viktor Balogh
Honored Contributor

Re: trap DEBUG

"I have the following lines in my .profile"
in which users .profile? is it the roots .profile?


"change using su - root"

if you switch to root this way, you are getting its environment. Check if the function "history_to_syslog" is still there after switching to root:

# functions
****
Unix operates with beer.
Andreas Tsamis
Advisor

Re: trap DEBUG

there are both in /.profile (roots) and /home/a_user/.profile (another user).
When the profile is executed under a bash shell (. /.profile) logging works without a problem. If i change to sh either by su - root (it loads the profile), or just typing sh and then . /.profile from another session i get the above problem.
#trap -l
in bash shows me the signals
in sh shows me nothing
#trap
in both
trap -- history_to_syslog DEBUG

It seems in the case of sh it keeps debuging the commands inside the history_to_syslog function. And i don't know if there is a difference in trap command according to the shell used.
Viktor Balogh
Honored Contributor

Re: trap DEBUG

> And i don't know if there is a difference in trap command according to the shell used.

good point! there should be a difference, since the trap command is a shell builtin. I don't know the exact use of trap in the different shells, so consult the relevant manpage of your shell for more info.

According to a solaris man page (there's a separate man page for trap) the command might have a different name in a different shell:

NAME
trap, onintr - shell built-in functions to respond to
(hardware) signals

SYNOPSIS
sh
trap [ argument n [n2...]]

csh
onintr [-| label]

ksh
*trap [ arg sig [ sig2...]]

****
Unix operates with beer.
Andreas Tsamis
Advisor

Re: trap DEBUG

After performing a search in the internet about differences between bash and sh shells it seems that DEBUG and ERR signals are not present in sh shell.
Is there another way to log all the shell commands in a file?
James R. Ferguson
Acclaimed Contributor

Re: trap DEBUG

Hi Andreas:

> it seems that DEBUG and ERR signals are not present in sh shell.

That is *not* true. The HP-UX 'sh' found as '/usr/bin/sh' or '/sbin/sh' is a POSIX shell documented as 'sh-posix(1)'. Look at those manpages, under the discussion of 'trap'.

Regards!

...JRF...