Garry Pettet
Garry Pettet

Reputation: 8288

How can I determine if a binary is installed using bash?

Using Terminal, what's the easiest way to determine if a binary named dcmdump is installed on the user's system? If installed I need to know its location (e.g. /usr/local/bin) and if its not installed then I'd like the Terminal to echo FALSE.

I know very little Terminal script but typing:

command -v dcmdump

Outputs the directory dcmdump is installed in (if installed - which is great) but nothing is echoed if its not (I want it to echo the string FALSE)

Upvotes: 7

Views: 7289

Answers (4)

martin
martin

Reputation: 990

POSIX compatible:

command -v dcmdump || echo FALSE

or use the below if you need to reuse the path

if cmd=$(command -v dcmdump); then echo $cmd; else echo FALSE; fi

Upvotes: 1

sideshowbarker
sideshowbarker

Reputation: 88186

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.

Upvotes: 8

AKJ88
AKJ88

Reputation: 733

You can use which and check if the return value is null like bellow:

#!/bin/sh
bi=$(which $1)
if [ -z $bi ]
then
    echo "FALSE"
else
    echo $bi
fi

Then the script can be run like this:

./script command

Upvotes: 1

5gon12eder
5gon12eder

Reputation: 25429

You can use this:

$ which dcmdump 2>/dev/null || echo FALSE

Here is how it works:

  • The entire expression is a boolean OR (written as ||) of two commands. The left-hand-side of the OR is the command which dcmdump 2>/dev/null, the right-hand-side is echo FALSE.
  • Thanks to lazy evaluation (or short-circuiting), if the left-hand-side evaluates to “true” (or “success”), then the right-hand-side will not be evaluated. Otherwise, it will.
  • The command which NAME looks for an executable named NAME in your current shell $PATH. If it finds one, it prints its absolute path to the standard output and exits with a status indicating “success”, otherwise, it might or might not print an error message to the standard error output and returns a status indicating “failure”.
  • Since we don't want which's error message but our own, we redirect which's standard error output to the black hole /dev/null with the 2>/dev/null part.
  • The command echo TEXT simply outputs TEXT to the standard output. If you wanted FALSE to be printed to the standard error output, you could redirect it using echo FALSE >&2.

Upvotes: 10

Related Questions