Tuesday, November 23, 2010

Enablling traces in Bash script


#!/bin/bash

# require that all variables be set, otherwise the bash script will abort
set -o nounset

# pltTrace=2 # uncomment pltTrace to enable trace

export nodeType=""
export BaseProgName=`basename $0`
export ProgName=${BaseProgName}.$$

# if pltTrace is not already defined, define it
# this will allow individual programs to be
# debugged without turning on debugging for
# all scritps that source this file
if ! [ ${pltTrace:+1} ]
then
declare -x pltTrace=0
#change pltTrace to 1 to turn on debug for
# all scripts that haven't defined pltTrace
fi

if [ $pltTrace -ge 2 ]
then
PS4='${ProgName}:$LINENO: pltTrace-> '
set -o xtrace
fi

echo `date '+%Y%m%d.%H%M.%S'` ${BaseProgName} ": ENTER: Echoing here ..."

echo hello
if [ ! : ]; then
echo hello 2
fi
echo hello 3

---END---
Execute:
export pltTrace=2
./test

ttest.16898:37: pltTrace-> date +%Y%m%d.%H%M.%S
test.16898:37: pltTrace-> echo 20101124.0558.51 test ': ENTER: creating ZFS pools and files systems ... starting'
20101124.0558.51 test : ENTER: creating ZFS pools and files systems ... starting
test.16898:40: pltTrace-> echo hello
hello
test.16898:42: pltTrace-> '[' '!' : ']'
test.16898:47: pltTrace-> echo hello 3
hello 3

No comments: