Im trying to reconstruct a bash commandline in it's whole, as in including a pipe; && or ";".
I'm using the DEBUG trap on my bash(s) to generate a global command.log (like history but also for remote systems). I have something like:
preexec () { :; }
preexec_invoke_exec () {
[ -n "$COMP_LINE" ] && return # do nothing if completing
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
local this_command=$(echo "${USER}@${HOSTNAME}:${PWD}> $BASH_COMMAND" | ${HOME}/cmd_digest.sh);
preexec "$this_command"
}
trap 'preexec_invoke_exec' DEBUG
I found this snipped via Does bash have a hook that is run before executing a command?
My previous Version used the more obvious:
PROMPT_COMMAND='echo "${USER}@${HOSTNAME}:${PWD}> $(HISTTIMEFORMAT= history 1|cut -c8-)" | ${HOME}/cmd_digest.sh'
Problem here is commands like ssh or screen are logged extremely out of time. This issue gets fixed by the DEBUG trap which ensures this_command to be executed before the command itself.
But, now I have to deal with bash invocations. Commandlines like:
echo foo | grep bar
are getting trapped twice, which of course can't be prevented. However if I could gather information about the BASH_COMMAND I might able to reconstruct the original commandline issued.
So what env variable could be used to achieve that? Any other Ideas are highly welcome.