- set -e: Enables checking of all commands. If a command exits with an error and the caller does not check such error, the script aborts immediately. Enabling this will make your scripts more robust. But don't wait until your script is "complete" to set the flag as an afterthought, because it will be a nightmare to fix the scrip to work with this feature enabled. Just write set -e as the very first line of your code; well... after the shell bang.
- set -x: If you are writing simple scripts that are meant to, well, script the execution of a few tasks (as opposed of being full-flown programs written in shell), set this flag to trace the execution of all commands. This will make the interpreter print each command right before it is executed, so it will aid you in knowing what is happening at any point in time.
Sunday, January 24, 2010
set -e and set -x
If you write shell scripts, you definitely need to know about two nice features that can be enabled through the set builtin:
Subscribe to:
Post Comments (Atom)
3 comments:
Congratulations Julio! Now that you are a married man, I have a few English corrections on this post:
/check such error/check the error/
/sell bang/shebang/
/full-flown/full-blown/
Hope you're having a great time in Korea!
There's one really cool bash feature that NetBSD's (k)sh could copy: "set -o pipefail". This makes a script with "set -e" abort also if any command in a piped command sequence fails, not just the last one.
For "set -x", you can also just run your script with "sh -x".
Actually, I stumbled upon this "pipefail" option yesterday and was surprised that the standard sh does not have a way to emulate the behavior...
Indeed, it would be nice to copy the feature! Maybe file a PR so that the request doesn't fall in the void? :-)
Post a Comment