PATH
is having an empty component mean "working directory". This shouldn't be supported any more, but I think the loop shouldn't just stop when hitting an empty component./
.set -f
to avoid pathname expansion in the test -x
.set -f
must be done before set --
, otherwise the path components themselves are subject to pathname expansion, and the test must include testing for a "regular file", which includes symlinks, but will exlude directories)-a
and -o
operators of test(1) are non-portable, so avoid them as well)Yes, boolean logic in the shell is based on the exit status of executed commands and functions, and a non-zero exit status indicates an error condition.Question if the result of a test is 0 , is this true. And nothing zero considered as false.
[ Let's say a program exists, with exit code 0. That's ok. Not zero is an error. ]
It's the exact opposite in e.g. C (and most other languages that can convert a number to a boolean value).Or is it is vice verse ?
Yes, if A gets evaluated at all.If i understand correctly:
A && B , is do B when A is true.
A || B , is do B when A is false.
I actually bookmarked your message as a remainder because it is really well explained, good job.I just explained it more in depth here: https://forums.freebsd.org/threads/...am-exists-and-is-executable.92343/post-645001
Result of switching to sh(1):Because in 13.2 the root's shell is csh, and its syntax is different:see csh(), in the section Input/output:Code:% which which >& /dev/null
Code:>& name .... The file name is used as standard output. .... The forms involving `&' route the diagnostic output into the specified file as well as the standard output.
root@thinkpad:/usr/src # sh
root@thinkpad:/usr/src # which which > /dev/null 2> /dev/null
root@thinkpad:/usr/src #
How do I tell whether a program is installed and is executable from within a script?
which -s ls && HAVE_LS=true || HAVE_LS=false
# Now you can use $HAVE_LS directly in other conditionals:
if $HAVE_LS; then
ls foo/bar
else
echo "Sorry, _ls_ is not available."
fi
-s
switch, which(1) provides its only "output" via exit code, so no need to redirect anything, either.Unfortunately, not portable. E.g. in Debain-based Linux distros which from debianutils doesn't provide such option.With the-s
switch, which(1) provides its only "output" via exit code, so no need to redirect anything, either.
Fair enough. For posix portability, useUnfortunately, not portable. E.g. in Debain-based Linux distros which from debianutils doesn't provide such option.
command -v foo >/dev/null && HAVE_FOO=true || HAVE_FOO=false