Shell Script Debugging for Beginners

Running FileBot from the console, Groovy / FileBot scripting, shell scripts, etc
Post Reply
User avatar
The Source
Posts: 15958
Joined: 16 Nov 2011, 08:59
Location: Taipei

Shell Script Debugging for Beginners

Post by rednoah » 09 Oct 2015, 16:48

It's easy to debug shell scripts when you can call them manually in the console and see what happens, but in many cases your script is called by another program (e.g. cron, Task Scheduler, Automator, etc).

How to debug scripts that are called by other tools?

If your script is called by another tool, you won't see any of the output. If something fails, or isn't working, the first thing is to add IO Redirection to all the calls to redirect the console output to a file.

It's simple:
  • Make sure your script is actually called
  • Make sure to collect all the output so you can see what's going on

First we make sure that our script is actually called, by adding simple debugging statements like this:

Code: Select all

printenv > "/path/to/log.txt"
By making a simple call that can't fail, we confirm that the script is called, and that we have file permissions to our log file. Make sure that the user running your script does have write permissions for your log location.

Then we add IO redirection to the call that doesn't work:

Code: Select all

filebot -script fn:sysenv > "/path/to/log.txt" 2>&1
At this point we know it's called, so it must error out for some reason. Make sure to collect BOTH standard output and standard error output.

Look at the example debug script: ... h/
:idea: Please read the FAQ and How to Request Help.

Post Reply