How to debug ash/bash shell scripts

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
kethd
Posts: 451
Joined: Thu 20 Oct 2005, 12:54
Location: Boston MA USA

How to debug ash/bash shell scripts

#1 Post by kethd »

How to debug ash/bash shell scripts

Add echo statements in various places to show what is happening where. $LINENO is available in bash. Use set and env to dump the values of all the variables. In bash, <trap DEBUG> allows added code to be run after each statement.

Two flags are available, -v verbose and -x xtrace. They can be invoked with the start of the script:
# sh -vx scriptfile
or within the script:
set -vx
and can then be turned off within the script:
set +vx

The output can be copied to a logfile:
# sh -vx scriptfile 2>&1 | tee logfile
or redirected from within the script:
exec 1>logfile 2>&1
But that exec statement takes away all the screen output, leaving you running blind. For advanced tips, see:
http://www.murga.org/%7Epuppy/viewtopic.php?t=4802
capture screen output in a file

There is rumored to be a bash debugger for the latest version, but no news yet of any sightings within the known pupverse.

Post Reply