4 ms·
Neither is GP’s solution portable either.
by jsjohnst 7y ago
Neither is GP’s solution portable either.
- stephenr 7y ago`$()` `cd` and `pwd -P` are all part of the POSIX spec. While that doesn't guarantee they're implemented in all shells, it's a lot more likely than things like readlink which are known to not be portable with the flags required to resolve a symlink.
- jsjohnst 7y ago`readlink -f` while not POSIX proper, works on FreeBSD, OpenBSD, NetBSD, and most Linux distros. If you really need to support a wider range than that, reality says you will likely have conditional OS checks in your script anyway, so I’m not sure your point other than being pedantic?
- stephenr 7y ago`readlink -f` on GNU canonicalises the path. `readlink -f` on a *BSD is for formatting the output `readlink -f` on macOS throws an error because it's an supported option.
- jsjohnst 7y agoFrom the FreeBSD man page [0] on readlink: > if the -f option is specified, the output is canonicalized by following every symlink in every component of the given path recursively. readlink will resolve both absolute and relative paths, and return the absolute pathname corresponding to file. In this case, the argument does not need to be a symbolic link. [0] https://www.freebsd.org/cgi/man.cgi?query=readlink&sektion=1 https://www.freebsd.org/cgi/man.cgi?query=readlink&sektion=1 Update: And here’s the OpenBSD man page from 1997 refuting your claim it’s for formatting as well: https://man.openbsd.org/OpenBSD-2.2/readlink.1 https://man.openbsd.org/OpenBSD-2.2/readlink.1 Update 2: OpenBSD (1997): http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/readlink/readlink.c?rev=1.10 http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/readlink/r... GNU/Linux (2003): https://lists.gnu.org/archive/html/bug-gnu-utils/2003-02/msg00008.html https://lists.gnu.org/archive/html/bug-gnu-utils/2003-02/msg... So it definitely did exist in at least one *BSD before Linux.
- stephenr 7y agoCool story bro, still not in POSIX, still doesn't work on macOS. Maybe next time understand what does and doesn't work before you complain about a solution that is both in POSIX and works on every platform.
- deleted 7y ago[deleted]
- jsjohnst 7y agoAlso, readlink (the function anyway) is in the POSIX[0] spec. [0] http://pubs.opengroup.org/onlinepubs/9699919799/ http://pubs.opengroup.org/onlinepubs/9699919799/
- stephenr 7y agoWhich is irrelevant to a shell script. But thanks for playing.