18 déc. 2014

shell-debugger.sh: a small shell script debugger

It's been a while since I posted anything here - but that doesn't mean this blog is dead. I continue to read your messages, block spam, think to other articles and so on.

And today, I'd like to present you a small script of mine whose goal is to help the difficult task of debugging a shell script. It leverages the trap ... DEBUG builtin of your not-that-prefered shell (i.e. bash, ash or dash) and allows you to run your script step by step, putting watches or executing additional commands while doing so.

Isn't that fun?

There's nothing utterly clever here. Everything is quite simple. In order to propose something which is at leastof minimal interest I tried to add as many features as I could think of (OK, I don't have much time to think about features thus the feature set is not that impressive).

Using shell-debugger.sh is quite simple (and hopefully not too invasive). It still requires you to locally edit your shell script.

  1. Source (using either the source or the . builtin) shell-debugger.sh from your own script
  2. if you want to debug the main script, call the DEBUG_SCRIPT alias at the beginning of the script
  3. If you want to debug a shell function, call the DEBUG_FUNCTION alias at the beginning of the function

Whenever you enter a function that does not contain a DEBUG_FUNCTION call, that function will not be steppable (i.e. it will run in full before control is returned to you).

Here is a small usage example

. shell-debugger.sh
local v=1 echo "v=${v} + $@" return 0 }
if -n "$1"; then func "$@" else func "no argument" fi exit 0

Here is what you (can) get:


The trap ... DEBUG builtin will stop execution before each shell instruction for which the trap is valid. At each stop, shell-debugger.sh proposes you a prompt which will allow you to perform a variety of action (print a specific variable, execute a command (including calling a script function or sourcing another script), add a variable watch...).

The shell code is attached to this post. It's distributed under the very liberal terms of the Zlib/PNG license.

Ajouter un commentaire

Les commentaires peuvent être formatés en utilisant une syntaxe wiki simplifiée.

Fil des commentaires de ce billet