5 ms·
This is generally terrible advice. A better option is to 'set -e' and ensure that the bash script exits when there's a failure. Bash scripts can't be idempoten
by united893 7y ago
This is generally terrible advice. A better option is to 'set -e' and ensure that the bash script exits when there's a failure.
Bash scripts can't be idempotent because they operate in an external environment that can't ever be. The better option is to just be extra safe.
See:
> http://redsymbol.net/articles/unofficial-bash-strict-mode/ http://redsymbol.net/articles/unofficial-bash-strict-mode/
- godelski 7y agoI'm not sure why people see errors as a bad thing. Errors are good. They stop you from doing stupid things. But maybe this article is good for telling people why you should handle errors well or when you write code to make sure it fails gracefully. Because if you don't people are going to start telling others to use the force flag when removing (almost always a terrible idea). Learn to handle errors, don't learn to brute force your way through them.
- jmts 7y agoMy understanding of 'set -e' is to ensure that errors are handled appropriately, not to brute force around them. If an error occurs that is not handled, this is treated as a problem and the script exits to ensure that one failure doesn't cause additional unforeseen problems as a result of the script running to completion.
- godelski 7y agoThe brute force method is the force flag (eg: `rm -f`). `set -e` is error handling. Force flag was constantly used in the post which is what I disagree with.
- dymk 7y agoThat's orthogonal to the article - in fact, the article hints that you'd take this advice when your bash script is failing midway, such as it might with `set -e`. The article is about writing bash scripts that don't care if you've ran them once, or 10 times.
- unixhero 7y agoPrecisely.
- farslan 7y agoOp here. Exactly this. I’ve seen many times where people had scripts and had to modify it (such as commenting code) when something has failed in the middle of the script.
- snissn 7y agoDon't forget about pipefail!! set -eu set -o pipefail Anything else I'm missing?
- khc 7y agonounset
- richardwhiuk 7y ago`set -x` can be very useful.
- jaytaylor 7y agoIIRC, `set -o nounset` is the bash equivalent for `set -u` in sh.
- news_hacker 7y agoSeems like all the pedants came out for this article. In good-faith I understand the principles OP is suggesting and have first-hand experience of their usefulness. But it's easier to nit-pick I guess.
- dredmorbius 7y agoThis is a case where the pedents should at least be considered.
- 7y ago
- xhgdvjky 7y agoI agree. using -f is a poor excuse for not handling errors properly. imagine doing that in any other language.... oof