脚本testing,看看是否已经加载了一个工具?

我正在学习docker,而且我正在尝试执行shelltesting以查看是否已经加载了关键工具。 我正在做一些testing,将docker部署到不同的服务器。 这是脚本,保存为dockerScript.sh

#!/bin/bash ## run "$ chmod +x dockerScript.sh" to make the script executable. ## then run "./dockerScript.sh" to activate the script touch docker-compose.yml cat > docker-compose.yml <<EOL version: "2" services: (etc...) EOL if[docker-compose is not already loaded] <--- what goes here? then curl -L https://github.com/docker/compose/releases/download/1.14.0-rc2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose fi docker-compose up -d 

在这种情况下,我想validation是否加载了docker-compose 。 我不太确定如何做到这一点。

我看到,如果我$ docker-compose -v我得到一个响应

docker-compose version 1.14.0-rc2, build 24dae73成功

而且

-bash: docker-compose: command not found

bash: docker-compose: command not found

作为一个响应,如果工具没有加载取决于我将使用的Linux的味道。

有没有一种方法来testing操作系统的存在/安装docker-compose工具并完成我的shell脚本?

你可以使用hash命令

 hash docker-compose 2>/dev/null || { echo >&2 "I require docker-compose but it's not installed."; } 

hash是一个bash内置的命令。 哈希表是bash的一个function,它可以防止每次键入命令时都必须search$PATH是将结果caching到内存中。

你可以使用hash命令,不用担心在你的系统上安装了hash函数。 如果你有bash那么你有hash

hash some_cmd 2>/dev/null表示将错误输出从stderrredirect到/ dev / null