On OS X at least, doing which dcmdump
to have it say /usr/local/bin/dcmdump
(if it finds the command) is not absolutely bad. But there can be issues with using which
in other environments.
But the main ding against which
in general is, it’s a separate command that’s not built into the shell, and so-called shell “builtins” are better choices when they get the job done just as well.
So if all you want is to check if a command exists (and don’t need to know where it is), you can get that just with the hash dcmdump
builtin and examining the return value; e.g., echo $?
, or:
if hash dcmdump 2>/dev/null; then
echo "OK, you have dcmdump installed. We’ll use that."
else
echo "You need dcmdump. I can install if for you, OK?"
read -e -p "Y or N? " yn
if [[ "y" = "$yn" || "Y" = "$yn" ]]; then
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install dcmtk
else
echo "We need dcmdump. Stopping."
exit
fi
fi
None of the options for checking command existence return a literal string FALSE (as you ask in your question); but using hash dcmdump
and just checking the return value will get the job done.
And if you do want to know where exactly the command is, that’s what command -v
will give you. Using type dcmdump
will also give you that info, in a slightly different form.
Anyway, hash
and command -v
and type
are all shell built-ins, so that’s in part why they’re recommended over which
for this. The canonical answer on this at SO gives more details.
btw, if your goal is to get dcmdump
on your system, you can do that by installing homebrew:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
And then after that, you can install the dcmtk
package:
brew install dcmtk
And then you really will have a dcmdump
command in /usr/local/bin/dcmdump
.