如何验证程序是否存在,以返回错误并退出,或继续执行脚本?

看起来应该很容易,但这让我很为难。


当前回答

which命令可能有用。男子谁

如果找到可执行文件,则返回0;如果找不到或不可执行,则返回1:

NAME

       which - locate a command

SYNOPSIS

       which [-a] filename ...

DESCRIPTION

       which returns the pathnames of the files which would
       be executed in the current environment, had its
       arguments been given as commands in a strictly
       POSIX-conformant shell. It does this by searching
       the PATH for executable files matching the names
       of the arguments.

OPTIONS

       -a     print all matching pathnames of each argument

EXIT STATUS

       0      if all specified commands are 
              found and executable

       1      if one or more specified commands is nonexistent
              or not executable

       2      if an invalid option is specified

这样做的好处是,它可以确定可执行文件在运行环境中是否可用-这节省了一些问题。。。

其他回答

假设您已经遵循了安全壳做法:

set -eu -o pipefail
shopt -s failglob

./dummy --version 2>&1 >/dev/null

这假设命令可以以这样的方式调用,即它(几乎)什么都不做,比如报告其版本或显示帮助。

如果找不到伪命令,Bash将退出并返回以下错误。。。

./my-script: line 8: dummy: command not found

这比其他命令-v(和类似的)回答更有用,也更不冗长,因为错误消息是自动生成的,并且还包含相关的行号。

如果您检查程序是否存在,您可能会稍后运行它。为什么不先尝试运行它?

if foo --version >/dev/null 2>&1; then
    echo Found
else
    echo Not found
fi

这是一个更值得信赖的检查程序运行,而不仅仅是查看PATH目录和文件权限。

此外,您可以从程序中获得一些有用的结果,例如其版本。

当然,缺点是有些程序启动起来会很重,有些程序没有--version选项可以立即(并成功)退出。

仅zsh,但对于zsh脚本非常有用(例如,在编写完成脚本时):

zsh/parameter模块可以访问内部命令哈希表等。来自man zshmodules:

THE ZSH/PARAMETER MODULE
       The zsh/parameter module gives access to some of the internal hash  ta‐
       bles used by the shell by defining some special parameters.


[...]

       commands
              This  array gives access to the command hash table. The keys are
              the names of external commands, the values are the pathnames  of
              the  files  that would be executed when the command would be in‐
              voked. Setting a key in this array defines a new entry  in  this
              table  in the same way as with the hash builtin. Unsetting a key
              as in `unset "commands[foo]"' removes the entry  for  the  given
              key from the command hash table.

尽管它是一个可加载的模块,但只要zsh不与--simulate一起使用,它似乎是默认加载的。

例子:

martin@martin ~ % echo $commands[zsh]
/usr/bin/zsh

要快速检查某个命令是否可用,只需检查哈希中是否存在密钥:

if (( ${+commands[zsh]} ))
then
  echo "zsh is available"
fi

请注意,散列将包含$PATH文件夹中的任何文件,无论它们是否可执行。为了绝对确定,您必须对此进行统计:

if (( ${+commands[zsh]} )) && [[ -x $commands[zsh] ]]
then
  echo "zsh is available"
fi

对于感兴趣的人来说,如果您希望检测已安装的库,则前面的答案中的方法都不起作用。我想你要么要检查路径(可能是头文件之类的),要么就这样(如果你是基于Debian的发行版):

dpkg --status libdb-dev | grep -q not-installed

if [ $? -eq 0 ]; then
    apt-get install libdb-dev
fi

从上面可以看到,查询中的“0”表示未安装包。这是“grep”的函数-“0”表示找到匹配项,“1”表示没有找到匹配项。

以下是检查命令是否存在于$PATH中并可执行的便携式方法:

[ -x "$(command -v foo)" ]

例子:

if ! [ -x "$(command -v git)" ]; then
  echo 'Error: git is not installed.' >&2
  exit 1
fi

需要进行可执行检查,因为如果$PATH中找不到具有该名称的可执行文件,bash将返回一个不可执行文件。

还请注意,如果$PATH中存在与可执行文件同名的不可执行文件,则dash会返回前者,即使后者会被执行。这是一个bug,违反了POSIX标准。[错误报告][标准]编辑:从破折号0.5.11(Debian 11)开始,这似乎是固定的。

此外,如果要查找的命令已定义为别名,则此操作将失败。