27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The DEBUG argument to trap causes a specified action to execute after every command in a script. This<br />

permits tracing variables, for example.<br />

Example 30−7. Tracing a variable<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

#!/bin/bash<br />

trap 'echo "VARIABLE−TRACE> \$variable = \"$variable\""' DEBUG<br />

# Echoes the value of $variable after every command.<br />

variable=29<br />

echo "Just initialized \"\$variable\" to $variable."<br />

let "variable *= 3"<br />

echo "Just multiplied \"\$variable\" by 3."<br />

# The "trap 'commands' DEBUG" construct would be more useful<br />

# in the context of a complex script,<br />

# where placing multiple "echo $variable" statements might be<br />

# clumsy and time−consuming.<br />

# Thanks, Stephane Chazelas for the pointer.<br />

exit 0<br />

trap '' SIGNAL (two adjacent apostrophes) disables SIGNAL for the remainder of the script. trap<br />

SIGNAL restores the functioning of SIGNAL once more. This is useful to protect a critical portion of a<br />

script from an undesirable interrupt.<br />

trap '' 2 # Signal 2 is Control−C, now disabled.<br />

command<br />

command<br />

command<br />

trap 2 # Reenables Control−C<br />

Chapter 30. Debugging 324

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!