Hi,

The below script echoes (when saved to bash_command.bash):
Trace:
  1: bash_command.bash:29 onerror ()
  2: bash_command.bash:29 mymain ()
  3: bash_command.bash:31 main ()
mymain: 29 status=1 command=return 1.

1) BASH_COMMAND is foo's 'return 1'. Shouldn't that be 'foo'?
   There is no error in 'return 1' per se.
   According to the trace, foo is not being run.
   The ERR trap is not inherited (set -E/+E doesn't matter here).
   'man bash' says: it is the command executing at the time of the trap.
2) Why use 'main' as toplevel function?
   I normally use 'main' as function, this had me slightly confused.
   Would rather have the empty string here. Is that (still) possible?
3) Couldn't BASH_COMMAND become an array?
4) Would FUNCARGS[] be an addition? 
   Holding the actual expanded arguments to the corresponding FUNCNAME.

The foretold script:
###############################################################
#!/usr/bin/env -S -i bash --noprofile --norc

show_trace () {
  local -i fid
  local path="${0%/*}/" file line function
  builtin echo 'Trace:'
  for fid in "${!FUNCNAME[@]}"; do
    (( fid >= 1 )) || continue ## skip show_trace; would need LINENO.
    file="${BASH_SOURCE[fid]#$path}"
    line="${BASH_LINENO[fid - 1]}"
    function="${FUNCNAME[fid]}"
    builtin echo "  $fid: $file:$line $function ()"
  done
}

onerror () {
  local st=$? bc="$BASH_COMMAND"
  builtin trap '' err
  show_trace
  builtin echo "${FUNCNAME[1]}: ${BASH_LINENO[0]} status=$st command=$bc."
}

foo() {
  return 1
}

mymain () {
  builtin trap 'onerror' err
  foo
}

mymain
###############################################################

-- 
Regards, Mike Jonkmans

Reply via email to