Friday, March 28, 2014

Fail fast shell scripts with set -e

Shell script number one:
ls /pippo
ls /pappo

chmod 775
ls: /pippo: No such file or directory
ls: /puppo: No such file or directory
now let's insert a "set -e" at the top of the script, and run again:
ls: /pippo: No such file or directory

See? Listing an nonexistent directory returns a non-zero error code, and this makes the script fail immediately, no need for boring explicit testing of the exit code of each command.... I only wish there was an error handler to print a nice error message.... I guess 100 years from now bash will evolve to something decent, for the time being be happy with the horrible crap it is.

No comments: