3 ms·
As someone who has fought a long time against holes in POSIX sh (cf https://git.sr.ht/~q3cpma/scripts https://git.sr.ht/~q3cpma/scripts README with links to the
by a-french-anon 3y ago
As someone who has fought a long time against holes in POSIX sh (cf https://git.sr.ht/~q3cpma/scripts https://git.sr.ht/~q3cpma/scripts README with links to the Austin group Mantis), let me list my biggest pet peeves (including this one):
* mktemp -d (mktemp itself is available via POSIX m4; the noclobber solution mentioned is moot, since FIFOs break it https://austingroupbugs.net/view.php?id=1364 https://austingroupbugs.net/view.php?id=1364), very much needed and available on GNU, *BSD and MacOS.
* local or typeset: I think only ksh88/93 doesn't have it without using the non-standard "function" keyword, a runtime check with potential alias creation is my only "solution" (https://git.sr.ht/~q3cpma/scripts/tree/b21de0bfb0a953446908936cec25b0ca847df626/item/util.sh#L5 https://git.sr.ht/~q3cpma/scripts/tree/b21de0bfb0a9534469089...)
* realpath/readlink -f/-e: as said by some, realpath is getting standardized in the next POSIX; can be emulated via a cumbersome function (https://git.sr.ht/~q3cpma/scripts/tree/b21de0bfb0a953446908936cec25b0ca847df626/item/readlinkf https://git.sr.ht/~q3cpma/scripts/tree/b21de0bfb0a9534469089...) but since you either put that massive block in your script or need realpath to reliably source it from another file...
* All the "newline in paths" mess: I gave up, just too many blind spots.
- ykonstant 3y agoIndeed, even the script you linked is not good enough; it does not deal with trailing newlines in the file or directory name, and the ls parsing will fail with a file like 'my malicious -> file'.
- o11c 3y agoAnother major hole (in all shells!): there's no way to perform quote-aware splitting of a string in a restricted manner. This is especially egregious given the lack of array variable support. `xargs` is close, but only handles a few kinds of quotes, not e.g. `$''` which is the best kind of quoting, and notably does not adapt to the current shell. `printf %q` is also pretty important to avoid injection attacks, though if you don't care about ugliness or performance you can hack it together pretty easily (but beware of trailing newlines in subshells!). (also, I am obligated to point out that BSDers are lying when they say they're using "Bourne shell". FreeBSD uses a fork of `ash`, related to `dash` and `busybox ash`, but all 3 have evolved independently. OpenBSD uses a derivative of `pdksh` (other BSDs also ship derivatives but not as /bin/sh). NetBSD has their own from-scratch shell, but NetBSD is dead and irrelevant. I have a whole tree of shells; `ash` is notable for being the most unpredictable within the tree.)