Small Mosaic


Categories:

books
career
codinghorrors
comics
events
geekstuff
justdont
languages
languages/bash
linkshot
magazines
meta
misctech
movies
nottech
operatingsystems
operatingsystems/linux
operatingsystems/linux/debian
operatingsystems/solaris
paranoidadmin
perl
presentations
programming
python
ruby
security
security/apache
security/tools
serversmells
services
services/dns
sites
specifications
sysadmin
tools
tools/commandline
tools/firefox
tools/gui
tools/network
tools/online
tools/online/greasemonkey
unixdaemon

Archives:

July 20093
June 20091
April 20093
March 20097
February 20094
January 200917
December 20081
November 20084
October 20085
September 20084
August 200812
July 20089
April 20084
March 20081
February 20081
January 200815
August 20072
June 20079
May 20076
April 20078
March 200731
February 20073
January 200721
December 20061
November 20064
October 20066
September 200632
August 200617
July 200614
June 20069
May 200613
March 200611
February 200616
January 200611
December 20051
November 20056
October 200519
September 200525
August 200516
July 200516
June 200513
May 20052
April 200519
March 200531
February 200520
January 200531
December 200421
November 200430
October 200432
September 200418
August 20047
July 200414
June 20045

Mon, 06 Jun 2005

Simple Bash Debugging: set -{x,u,n}
The bash shell gets more negative press than it deserves from most "real" programmers. Between the "I can't see what it's doing, I need an echo after nearly every line!" and the "Why doesn't it have a check option like perl's -c!?" most people that only occasionally dip in to bash end up frustrated by it's lack of features. All because they can't be bothered to read the man page...

I'm going to show you three simple bash "tricks" that'll make your script debugging a lot easier; and none of them require that much searching to find!. Each of the three tidbits presented can be used in one of two ways, they can either be placed one to a line near the top of your shell scripts, just under the shebang line like this, 'set -x', or they can be used as command line arguments to bash itself (bash -x script.sh).

First up we have the '-x' option. This provides a simple trace of each and every command executed by the shell script, it also shows the interpolated values of the variables; which can save a lot of echo's and printf's!

Next up we have '-n', the syntax checker. When this is specified the script doesn't actually run but instead has it's syntax checked for any errors. This removes the annoying "String not terminated" messages that you'll occasionally get from not terminating your strings. Only when '-n' is specified you don't run half a script, leaving the system in an inconsistent state.

The last of our time-savers is the '-u' option, this forces any undefined variables to terminate the script rather than carry on with no values. Although this is often useful, and saves you backtracking through the output of '-x' to find the undeclared value, you can't combine it with the '-n' option. You have to allow the script to run in order for the undefined values to be detected; while this is less than ideal it's better than nothing...

I hope these three lesser known bash features make your scripting more enjoyable and productive, I've lost count of the time they've saved me!

Like this post? - Digg Me! | Add to del.icio.us! | reddit this!

Posted: 2005/06/06 21:55 | /tools/commandline | Permanent link to this entry | This entry + same date


books career codinghorrors events geekstuff justdont languages/bash magazines meta misctech movies nottech operatingsystems/linux operatingsystems/linux/debian operatingsystems/solaris perl presentations programming python ruby security security/apache security/tools serversmells services/dns sites specifications sysadmin tools/commandline tools/firefox tools/gui tools/network tools/online tools/online/greasemonkey unixdaemon

Copyright © 2000-2005 Dean Wilson XML feed logo