| ▲ | kazinator 5 hours ago | |||||||||||||||||||||||||||||||
> ( : < dataset.json ) && echo YES # is dataset.json readable? The subshell execution parentheses and the colon are superfluous here, just:
Redirections do not require a colon command to hang off of, and there is no need to fork a subshell to execute such a command.> ( : >> result.json ) && echo YES # is result.json writable? As a go-to idiom for a writability test, it gives me pause. If the file didn't exist, we created a zero-length one. That might be okay if we are going to write to it anyway as the next action. If we are testing because we intend to overwrite it, why not just "> result.json" (which is by itself an idiom for truncating a file to zero length). When would we every do this? Maybe before some command which takes the file name as a destination file argument rather than using output redirection, and which performs a lengthy computation before trying to open the file for writing. We can catch the permission error early. I don't think I've ever coded such a test; normally you just do the operation that writes to the file and let that fail. In POSIX C, there is a function access() for doing these kinds of tests. But it has a special purpose: it is meant to be used by a setuid root process to perform a permission test as if it were the real user/group (the one which elevated privilege to root). I.e. it's not can we do this operation, but should we do this operation (would we still be allowed, if we dropped privileges back to the original user). | ||||||||||||||||||||||||||||||||
| ▲ | refp 4 hours ago | parent | next [-] | |||||||||||||||||||||||||||||||
They are NOT superflous, and all you need to prove it is `zsh` (but there are others that follow suit in similar fashions):
So, if you want something that "everyone can use" without going into details about the difference between commonly used shells.. you'd use the null-command.--- and given that we use the null-command, it _WILL_ behave different with or without subshell.. and all you need is `bash --posix` to prove it:
The output above is not truncated, `no-subshell.sh` will stop executing due to the broken read.--- One should never trust things just because they are written, but that also applies to comments on HN. Originally when I read your message I actually thought I made a mistake, I was very close to writing an apology comment and adding a note to the blog post, but not close enough - I had to test it again. I'm thankful for the watchful eyes and scrutiny when reading things online, that's good - keep it up, but your message is factually wrong - on so many levels. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
| ▲ | 4 hours ago | parent | prev [-] | |||||||||||||||||||||||||||||||
| [deleted] | ||||||||||||||||||||||||||||||||