bash 如何检查bash脚本中的依赖关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33297857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:47:47  来源:igfitidea点击:

How to check dependency in bash script

linuxbash

提问by Rohit

I want to check whether nodejs is installed on the system or not. I am getting this error:

我想检查系统上是否安装了nodejs。我收到此错误:

Error : command not found.

错误:找不到命令。

How can i fix it?

我该如何解决?

#!/bin/bash

if [ nodejs -v ]; then
echo "nodejs found"
else
echo "nodejs not found"
fi

回答by hek2mgl

You can use the commandbash builtin:

您可以使用command内置bash

if command -v nodejs >/dev/null 2>&1 ; then
    echo "nodejs found"
    echo "version: $(nodejs -v)"
else
    echo "nodejs not found"
fi

回答by Manuel Barbe

You may check the existence of a program or function by

您可以通过以下方式检查程序或函数的存在

type nodejs &>/dev/null || echo "node js not installed"

However, there is a more sophisticated explanation available here.

然而,有可用的更复杂的解释在这里

回答by edi9999

The name of the command is node, not nodejs

命令的名称是node, 不是nodejs

which returns the path to the command to stdout, if it exists

它将命令的路径返回到标准输出(如果存在)

if [ $(which node 2>/dev/null) ]; then
  echo "nodejs found"
else
  echo "nodejs not found"
fi

回答by meanderer

If all you want is to check to see if a command exists, use which command. It returns the patch if the command is called, and nothing if it is not found

如果您只想检查命令是否存在,请使用 which 命令。如果调用命令,则返回补丁,如果未找到则返回任何内容

if [ "$(which openssl)" = "" ] ;then
echo "This script requires openssl, please resolve and try again."
exit 1
fi

回答by Scooby-2

This is not what the OP asked for (nearly 3 years ago!), but for anyone who wants to check multiple dependencies:

这不是 OP 所要求的(将近 3 年前!),而是对于任何想要检查多个依赖项的人:

#!/bin/bash
echo -n "Checking dependencies... "
for name in youtube-dl yad ffmpeg
do
  [[ $(which $name 2>/dev/null) ]] || { echo -en "\n$name needs to be installed. Use 'sudo apt-get install $name'";deps=1; }
done
[[ $deps -ne 1 ]] && echo "OK" || { echo -en "\nInstall the above and rerun this script\n";exit 1; }

Here's how it works. First, we print a line saying that we are checking dependencies. The second line starts a "for name in..." loop, in which we put the dependencies we want to check, in this example we will check for youtube-dl, yad and ffmpeg. The loop commences (with "do") and the next line checks for the existence of each command using the bash command "which." If the dependency is already installed, no action is taken and we skip to the next command in the loop. If it does need to be installed, a message is printed and a variable "deps" is set to 1 (deps = dependencies) and then we continue to the next command to check. After all the commands are checked, the final line checks to see if any dependencies are required by checking the deps variable. If it is not set, it appends "OK" to the line where it originally said "Checking dependencies.... " and continues (assuming this is the first part of a script). If it is set, it prints a message asking to install the dependencies and to rerun the script. It then exits the script.

这是它的工作原理。首先,我们打印一行表示我们正在检查依赖项。第二行开始一个“for name in...”循环,我们将要检查的依赖项放入其中,在本例中,我们将检查 youtube-dl、yad 和 ffmpeg。循环开始(使用“do”),下一行使用 bash 命令“which”检查每个命令是否存在。如果已经安装了依赖项,则不采取任何操作,我们跳到循环中的下一个命令。如果确实需要安装,则会打印一条消息,并将变量“deps”设置为 1(deps = 依赖项),然后我们继续下一个命令进行检查。检查完所有命令后,最后一行通过检查 deps 变量来检查是否需要任何依赖项。如果未设置,则附加“OK” 到它最初说“检查依赖关系...”的那一行并继续(假设这是脚本的第一部分)。如果已设置,它会打印一条消息,要求安装依赖项并重新运行脚本。然后退出脚本。

The echo commands look complicated but they are necessary to give a clean output on the terminal. Here is a screenshot showing that the dependencies are not met on the first run, but they are on the second.

echo 命令看起来很复杂,但它们对于在终端上提供干净的输出是必要的。这是一个屏幕截图,显示第一次运行时没有满足依赖关系,但它们在第二次运行时满足。

Bash dependency check script screen print

Bash 依赖性检查脚本屏幕打印

PS If you save this as an script, you will need to be in the same directory as the script and type ./{name_of_your_script} and it will need to be executable.

PS 如果您将其保存为脚本,您将需要在与脚本相同的目录中并键入 ./{name_of_your_script} 并且它需要是可执行的。