Easier Shell Script DebuggingBátori István, December 2006 Write your shell scripts this way to make them easier to test and debug.
Shell script debugging is not easy. You have to put Put the following lines at the beginning of the script: if [[ -z $DEBUG ]];then alias DEBUG='# ' else alias DEBUG='' fi Everywhere you put a line that is only for testing, write it in the following way: DEBUG set -x Or DEBUG echo $PATH Or set a parameter that is valid only during the test: DEBUG export LOGFILE=$HOME/tmp Before executing the script, set the # export DEBUG=yes During the execution of the script, the Sample Script
#!/usr/bin/ksh
# test script to show the DEBUG alias
if [[ -z $DEBUG ]];then
alias DEBUG='# '
else
alias DEBUG=''
fi
LOG_FILE=/var/script.out
DEBUG LOG_FILE=$HOME/tmp/script.out
function add {
DEBUG set -x
a=$1
b=$2
return $((a + b))
}
# MAIN
DEBUG echo "test execution"
echo "$(date) script execution" >>$LOG_FILE
echo "if you do not know it:"
add 2 2
echo " 2 + 2 = $?"
Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under this License. |
BigAdmin SubscriptionsBigAdmin Areas
BigAdmin Sun Center
BigAdmin Topics | |||