Another alternative -- a pattern I've seen in project auto configure scripts:
if [ -x /usr/bin/figlet ]
then
FIGLET=/usr/bin/figlet
else
FIGLET=:
fi
$FIGLET "Hello, world!"
In your specific case you could even do,
if [ -x /usr/bin/figlet ]
then
SAY=/usr/bin/figlet
elif [ -x /usr/local/bin/figlet ]
then
SAY=/usr/local/bin/figlet
elif [ -x /usr/bin/banner ]
then
SAY=/usr/bin/banner
else
SAY=/usr/bin/echo
fi
$SAY "Hello, world!"
If you don't know the specific path, you can try multiple elif
(see above) to try known locations, or just use the PATH
to always resolve the command:
if command -v figlet >/dev/null
then
SAY=figlet
elif command -v banner >/dev/null
then
SAY=banner
else
SAY=echo
fi
In general, when writing scripts, I prefer to only call commands in specific locations specified by me. I don't like the uncertainty/risk of what the end user might have put into their PATH
, perhaps in their own ~/bin
.
If, for example, I was writing a complicated script for others that might remove files based on the output of a particular command I'm calling, I wouldn't want to accidentally pick up something in their ~/bin
that might or might not be the command I expected.
figlet ... || true
. – Giacomo Alzetta Jan 30 '19 at 13:40figlet || true
, but in your case probably a shell function which Echos plaintext If no Banner can be printed is more likely what you want. – eckes Jan 31 '19 at 01:26