SitefinderOracle and Sun
Secure Search

BigAdmin System Administration Portal
Community-Submitted Tech Tip
Print-friendly VersionPrint-friendly Version
This content is submitted by a BigAdmin user. It has not been reviewed for technical accuracy by Sun Microsystems, though it may have been lightly edited to improve readability. If you find an error or would like to comment on the article, please contact the submitter or use the comment field at the bottom of the article. Community submissions may not follow Sun trademark guidelines. For information on Sun trademarks, please see http://www.sun.com/suntrademarks/.
 
 

Easier Shell Script Debugging

Bá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 set -x or the echo command into the script. You may want to test the script in a test environment. And at least before publishing the script, you have to delete the debug lines. The following tip gives a hint about how to solve this problem without errors.

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 echo a parameter:

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 DEBUG variable in the shell:

# export DEBUG=yes

During the execution of the script, the DEBUG lines will be executed. If you publish the script, you can forget the deletion of the debug lines; they will not disturb the functionality.


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
  
 
BigAdmin Solaris 10 Survey
 
Oracle - The Information Company