BASH: trace script into a log file

Under the cut a little tip to enable tracing bash scripts into a file if it possible.

#!/bin/bash bash_tracefd_support=$(python -c 'from distutils.version import LooseVersion; exit(LooseVersion("'"$BASH_VERSION"'") <= LooseVersion("4.1"));' 2>/dev/null; test "1" -eq "$?") if [ $bash_tracefd_support -eq 0 ]; then exec 19>${0}.trace.log export BASH_XTRACEFD=19 set -x fi echo "Testing bash tracing" set +x [ $bash_tracefd_support -eq 0 ] && exec 19>&-

Note: The most modern OS already have python on the board, so, it gives much less code than version comparison using bash.

The trick with reverted comparison in python used to “say yes” when module distutils is exists and bash supports tracing into file, or when no module distutils in the system, but I guess it’s an exception.

HH

Leave a Reply

Your email address will not be published. Required fields are marked *