简体   繁体   English

如何确定是否使用 bash 安装了二进制文件?

[英]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:下面是它的工作原理:

  • The entire expression is a boolean OR (written as || ) of two commands.整个表达式是两个命令的布尔 OR(写为|| )。 The left-hand-side of the OR is the command which dcmdump 2>/dev/null , the right-hand-side is echo FALSE . OR 的左侧是命令which dcmdump 2>/dev/null ,右侧是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 .命令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”.如果找到,则将其绝对路径打印到标准输出并以指示“成功”的状态退出,否则,它可能会或可能不会向标准错误输出打印错误消息并返回指示“失败”的状态。
  • 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.由于我们不想要which的错误消息,而是我们自己的,我们将which的标准错误输出重定向到黑洞/dev/null2>/dev/null部分。
  • The command 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.无论如何, hashcommand -vtype都是 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

POSIX compatible: POSIX兼容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM