[英]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?使用终端,确定用户系统上是否安装了名为
dcmdump
的二进制文件的最简单方法是什么? If installed I need to know its location (eg /usr/local/bin
) and if its not installed then I'd like the Terminal to echo FALSE
.如果安装了,我需要知道它的位置(例如
/usr/local/bin
),如果它没有安装,那么我希望终端回显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 )输出目录 dcmdump 安装在(如果已安装 - 这很好)但如果没有则没有回显(我希望它回显字符串FALSE )
You can use this:你可以使用这个:
$ which dcmdump 2>/dev/null || echo FALSE
Here is how it works:下面是它的工作原理:
||
) 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
. which dcmdump 2>/dev/null
,右侧是echo FALSE
。which NAME
looks for an executable named NAME
in your current shell $PATH
.which NAME
寻找一个可执行文件名为NAME
在当前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”.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.which
的错误消息,而是我们自己的,我们将which
的标准错误输出重定向到黑洞/dev/null
和2>/dev/null
部分。echo TEXT
simply outputs TEXT
to the standard output.echo TEXT
简单地输出TEXT
到标准输出。 If you wanted FALSE
to be printed to the standard error output, you could redirect it using echo FALSE >&2
.FALSE
打印到标准错误输出,您可以使用echo FALSE >&2
重定向它。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.至少在 OS X 上,执行
which dcmdump
让它说/usr/local/bin/dcmdump
(如果它找到命令)并不是绝对坏的。 But there can be issues with using which
in other environments .但是在其他环境中使用
which
可能会出现问题。
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.不过,最主要的丁对
which
的一般是,它是不是内置在外壳一个单独的命令,和所谓的壳“内建”是更好的选择,当他们做一样好工作。
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;因此,如果您只想检查命令是否存在(并且不需要知道它在哪里),您可以使用内置的
hash dcmdump
并检查返回值来获得它; eg, echo $?
例如,
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);检查命令存在的选项都没有返回文字字符串FALSE (正如您在问题中所问的那样); but using
hash dcmdump
and just checking the return value will get the job done.但是使用
hash dcmdump
并只检查返回值就可以完成工作。
And if you do want to know where exactly the command is, that's what command -v
will give you.如果您确实想知道命令的确切位置,这就是
command -v
会给您的。 Using type dcmdump
will also give you that info, in a slightly different form.使用
type dcmdump
也会以稍微不同的形式为您提供该信息。
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.无论如何,
hash
和command -v
和type
都是 shell 内置hash
,所以这就是为什么推荐which
用于此的部分原因。 The canonical answer on this at SO gives more details. SO 上对此的规范答案提供了更多详细信息。
btw, if your goal is to get dcmdump
on your system, you can do that by installing homebrew :顺便说一句,如果您的目标是在您的系统上获取
dcmdump
,您可以通过安装homebrew 来实现:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
And then after that, you can install the dcmtk
package:然后,您可以安装
dcmtk
包:
brew install dcmtk
And then you really will have a dcmdump
command in /usr/local/bin/dcmdump
.然后你真的会在
/usr/local/bin/dcmdump
有一个dcmdump
命令。
You can use which
and check if the return value is null like bellow:您可以使用
which
并检查返回值是否为空,如下所示:
#!/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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.