Debugging bash scripts is pretty straightforward, throwing around a couple echo and set -x quickly gives you what you need. But what if you want to add a nice breakpoint, debugging to lots of paces in the code or turn all debugging on or off at once? Then this little script I wrote is the right thing for you: debug.sh just download it to the same directory as your script and include it with the following line:
1 |
. debug.sh
|
It contains 4 simple functions that will make your bash coding easier.
debug and breakpoint both print the argument with a timestamp to STDERR
You can turn off all the functions by adding a DEBUG=false into your code
Example:
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/bin/bash
. debug.sh
debug_on
echo foo
debug_off
debug "I am a test"
breakpoint "wait for a bit"
echo done
|
Output:
1
2
3
4
5
6
7
8
9
10
|
+ echo foo
foo
+ debug_off
+ true
+ set +x
[16:37:47] I am a test
[16:37:47] -breakpoint- wait for a bit
press any key to continue
done
|
One thought on “How to add debugging to shellscripts”
Comments are closed.