Debugging shell scripts comes handy, especially when unexpected things happen. Trust me, they do. I use two different approaches, based on what’s needed: debug while running it or when ran in the background, maybe by cron.
- Instead of ./script.sh run
sh -x script.sh
orbash -x script.sh
- When the script is invoked by cron things are slightly different, though still simple:
- add
set -x
to your script, after#!/bin/sh
- keep an eye on
/var/log/messages
&/var/log/syslog
- add
Enjoy 😉
PS: The bash reference manual has many useful information