On Sat, Mar 24, 2018 at 06:05:08PM +0100, Thomas A. Schmitz wrote:
Arthur's
if command -v ldd >/dev/null && ldd --version 2>&1 | fgrep -q '^musl'
works for me, but again, there may be other corner cases that we don't see now. I would suggest reversing the logic of this test: default to linux unless you clearly find the string "musl" in the output; don't rely on a zero result, which may be prevented by a number f reasons...
Yes, well. About that ... If you think about what “default” means, you’ll realise that the bit of code you quote does actually make glibc the default: indeed, if [some complex condition]; then libc=musl else libc=glic fi or, equivalently, Henri’s suggested alternative libc=glibc if [some complex condition]; then libc=musl fi means exactly “set libc to glibc, unless [some complex condition] is met”. The trick is that in your case, the test unexpectedly *succeeds* (which confused me yesterday). How can that be? The exact test is command -v ldd >/dev/null && ldd --version 2>&1 | grep -q ^musl which in your case boils down to grep -q ^musl [output of ldd --version] meaning that grep will search for the regexp ^musl in the output of ldd --version. Or will it? As I explained, the string ^musl can in zsh (if EXTENDED_GLOB is set) be interpreted as “all file names in the current directory but musl”, which means that it is actually expanded as grep -q [filename 1] [filename 2] [filename 3] ... [output of ldd --version] and grep will thus look for the first file name in that list (whose exact order is hard to predict and depends in particular on your locale). Every file in the current directory (and the output of ldd --version) will be searched for that name, so if it’s simple enough there’s a high chance that it will be found. Amusingly, in my case, the first file name is the rather unlikely 20170802093441240.pdf, but it is found anyway, because I sent it as an email attachment one day, so the line Content-Disposition: attachment; filename="20170802093441240.pdf" is found in my sent messages (which is a plain text file in my home directory). That is not all! As you experienced, grep does actually report an error for each directory name, and should thus return a non-zero value; which indeed it does in a number of experiments I conducted. But! In the original test, grep is called with the -q switch, that has the following specification in POSIX: Quiet. Nothing shall be written to the standard output, regardless of matching lines. Exit with zero status if an input line is selected. And here we are. For a number of unlikely reasons, the condition, that should only hold true in a very specific situation, ends up being satisfied. Best, Arthur