[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-bash] Show source code and xtrace as they are executed?
From: |
adrelanos |
Subject: |
[Help-bash] Show source code and xtrace as they are executed? |
Date: |
Sat, 21 Dec 2013 06:01:38 +0000 |
Hi,
"set -o verbose" shows when commands are read. Is there something
similar to "set -o verbose" that shows commands as they are executed?
I am asking this, because "set -o verbose" isn't so useful for debugging
when using functions.
For debugging purposes, I would like to see the source of what is
currently done (example: if [ "$x" = "$y" ]; then) and below also the
actual values (example '[' '' = 11 ']').
In other words, I would like to have a combination of xtrace (which
shows what is actually run) and a debug trap (which shows how the source
looks like *).
This is how my attempt looks:
#############################
#!/bin/bash
set -x
exec > >(tee -a foo.log)
exec 2> >(tee -a foo.log >&2)
set -o functrace
shopt -s extdebug
trap "true TRACE ${BASH_SOURCE[1]}:${BASH_LINENO[0]}:${FUNCNAME[1]}:
$BASH_COMMAND" DEBUG
y="11"
if [ "$x" = "$y" ]; then
true "Do something..."
else
true "Do something else..."
fi
This is how the log currently looks:
####################################
~ $ cat foo.log
+ set -o functrace
+ shopt -s extdebug
+ trap 'true TRACE :0:: trap "true TRACE
${BASH_SOURCE[1]}:${BASH_LINENO[0]}:${FUNCNAME[1]}: $BASH_COMMAND"
DEBUG' DEBUG
++ true TRACE :0:: trap 'true TRACE :0:: y=11' DEBUG
+ y=11
++ true TRACE :0:: trap 'true TRACE :0:: [ "$x" = "$y" ]' DEBUG
+ '[' '' = 11 ']'
++ true TRACE :0:: trap 'true TRACE :0:: true "Do something else..."' DEBUG
+ true 'Do something else...'
This is how I would ideally like it to look:
############################################
++ :0:: [ "$x" = "$y" ]'
+ '[' '' = 11 ']'
Writing it to the terminal and also to a log.
What is the closest I can get to?
Cheers,
adrelanos
* Or at least the closest bash feature I know doing that. When there are
syntax errors (example: "if [ "x" = "x" ] then" with no ";") then debug
traps unfortunately don't show the literal source.
- [Help-bash] Show source code and xtrace as they are executed?,
adrelanos <=