Quantcast
Channel: Java mon amour
Viewing all articles
Browse latest Browse all 1121

Fail fast shell scripts with set -e

$
0
0
Shell script number one:
vi testsete.sh

ls /pippo
ls /pappo

chmod 775 testsete.sh
./testsete.sh

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.

Viewing all articles
Browse latest Browse all 1121

Trending Articles