我正在学习docker,我正在尝试进行shell测试以查看是否已加载关键工具。我正在做一些测试,将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
在这种情况下,我想验证是否已加载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的确切风格。
有没有办法测试操作系统是否存在/安装docker-compose
工具并完成我的shell脚本?
答案 0 :(得分:1)
您可以使用hash
命令
hash docker-compose 2>/dev/null || { echo >&2 "I require docker-compose but it's not installed."; }
hash
是一个bash内置命令。哈希表是bash的一个特性,它可以防止每次键入命令时都必须通过将结果缓存到内存中来搜索$PATH
。
您可以使用hash
命令,不用担心在您的系统上安装了hash
utillity。如果您有bash
,那么您有hash
。
hash some_cmd 2>/dev/null
表示将stderr的错误输出重定向到/ dev / null