【问题标题】:Bash, ... check if a program is installed or not, using bash script [duplicate]Bash,...检查是否安装了程序,使用 bash 脚本 [重复]
【发布时间】:2015-06-27 06:09:13
【问题描述】:

我正在尝试创建一个小脚本来检查是否安装了程序。我正在尝试使用 tmux,...

`tmux --help` | grep "tmux: command not found" &> /dev/null
if [ $? == 1 ]; then
    echo "tmux is not installed"
    exit
fi

安装tmux后,我得到:

usage: tmux [-2lquvV] [-c shell-command] [-f file] [-L socket-name]
            [-S socket-path] [command [flags]]
tmux is not installed

如果一个程序没有安装,appair 字符串“tmux: command not found”。这可以解释为什么我 grep tmux --help 命令的输出。检查 tmux 是否安装的正确方法是什么?

脚本总是回显“tmux 未安装”。即使我安装了 tmux。它有什么问题?

【问题讨论】:

  • 您不希望tmux --help 周围的反引号将执行tmux --help,然后尝试将返回的帮助字符串作为命令执行。
  • 大声笑。你完全正确

标签: bash


【解决方案1】:

您可以使用commandtypehash 内置函数来测试给定命令在当前shell 会话中是否可用。

这不会告诉您它是否在当前PATH 之外的某个位置可用。

您应该避免为此目的使用which(尽管这是您会从很多人那里得到的默认建议),因为它不是一个标准化工具(不完全)而且它是一个外部 em> 工具与上面的工具相比,它们都是内置在 shell 中的(因此检查成本更高)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 2013-09-26
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多