4 ms·
I've seen... (ok, actually I recommended)... someone do the following: if [ -x /usr/bin/blah ] then BLAH=/usr/bin/blah else BLAH=/bin
by rrauenza 8y ago
I've seen... (ok, actually I recommended)... someone do the following:
if [ -x /usr/bin/blah ]
then
BLAH=/usr/bin/blah
else
BLAH=/bin/true
fi
$BLAH foo bar baz
They wanted BLAH to be a noop if blah didn't exist. I could only think of /bin/true as a noop at the time, but I needed it to ignore parameters.
The man page defines that it does.
(in retrospect, we could have defined an sh function noop {} )
- geofft 8y agoYeah, options include a noop function (which is nice because you can change it to noop () { echo "Skipping $@" >&2 ; }) or the shell builtin :, which definitely ignores its arguments.
- JdeBP 8y agoI was going to mention that it came up just days ago on Unix and Linux StackExchange. (-: * https://unix.stackexchange.com/a/497561/5132 https://unix.stackexchange.com/a/497561/5132 The conditionally defined shell function in one of the other answers is a more appealing approach, especially given the native behaviours of some shells with respect to variable expansion. But also given that /bin/true is not necessarily the pathname. % export BLAH='/bin/true too' % dash -c '$BLAH 1 2 3' % dash: 1: /bin/true: not found % sh -c '$BLAH 1 2 3' /bin/true: not found % ksh -c '$BLAH 1 2 3' ksh: /bin/true: not found % zsh -c '$BLAH 1 2 3' zsh:1: no such file or directory: /bin/true too % bash -c '$BLAH 1 2 3' bash: /bin/true: No such file or directory % % export BLAH='/usr/bin/true too' % zsh -c '$BLAH 1 2 3' zsh:1: no such file or directory: /usr/bin/true too % bash -c '$BLAH 1 2 3' % ksh -c '$BLAH 1 2 3' % dash -c '$BLAH 1 2 3' %
- rrauenza 8y agoYeah, that was my answer -- and actually I wrote /usr/bin/true on SO but got sloppy here. I wrote that answer because I recalled it as a common idiom in autoconf or something -- some automake system I'd seen where the system seeks out the tools it needs and defines them as variables.