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...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 30. Debugging<br />

The Bash shell contains no debugger, nor even any debugging−specific commands or constructs. [60] Syntax<br />

errors or outright typos in the script generate cryptic error messages that are often of no help in debugging a<br />

non−functional script.<br />

Example 30−1. A buggy script<br />

#!/bin/bash<br />

# ex74.sh<br />

# This is a buggy script.<br />

a=37<br />

if [$a −gt 27 ]<br />

then<br />

echo $a<br />

fi<br />

exit 0<br />

Output from script:<br />

./ex74.sh: [37: command not found<br />

What's wrong with the above script (hint: after the if)?<br />

Example 30−2. Missing keyword<br />

#!/bin/bash<br />

# missing−keyword.sh: What error message will this generate?<br />

for a in 1 2 3<br />

do<br />

echo "$a"<br />

# done # Required keyword 'done' commented out in line 7.<br />

exit 0<br />

Output from script:<br />

missing−keyword.sh: line 10: syntax error: unexpected end of file<br />

Note that the error message does not necessarily reference the line in which the error occurs, but the line<br />

where the Bash interpreter finally becomes aware of the error.<br />

Error messages may disregard comment lines in a script when reporting the line number of a syntax error.<br />

What if the script executes, but does not work as expected? This is the all too familiar logic error.<br />

Chapter 30. Debugging 319

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

Saved successfully!

Ooh no, something went wrong!