4 ms·
The issue there is that testing then executing can introduce racing issues in a multi threaded scenario (unless I misunderstand how `test` works)
by maest 5y ago
The issue there is that testing then executing can introduce racing issues in a multi threaded scenario (unless I misunderstand how `test` works)
- Macha 5y agoThis is true for stat too, which was the grandparent example. Though you're unlikely to be writing multi-threaded bash, but entirely possible for another process to delete it too.
- bell-cot 5y agoI would not trust things like `ls -sf` to be thread-safe either. For shell scripts, a reasonable approach is to use a (correctly implemented) lock file at the start, then a bunch of `test` (aka `[`) blocks to check things. That does a decent job of documenting, too. Vs. everyone who looks at the script needing to know the less-common command line options. Then, in the bigger picture, make sure that nobody else's script - maybe with its own lock file - starts overlapping with your script.
- kbenson 5y agoYes. I'm not making a case that test gets around that problem, just that in the cases where you want to test something about a file, such as existence, emptiness/non-emptiness, etc, test is the more versatile (and more standard) choice.