前面问题的不同方面:为什么从geek_scripts运行时不测试 Docker 工作是否存在不起作用?



我有三个脚本:mysql_monitor.sh、sip_monitor.sh 和 check_docker.sh,每个脚本都执行一些测试并显示一条消息,其中状态为"红色"(如果不存在),如果不存在,则为"绿色"。这些脚本位于另一个名为 newmain.bash 的脚本中,该脚本在 geek_scripts shell 中运行。 如果我从命令行运行 newmain.bash,那么它会检测 Docker 是否正在运行,并在每个颜色上放置正确的颜色和突出显示。 但是,当它从 geek_scripts shell 运行时,它不会检测到 Docker 是否正在运行,总是说它没有运行。 此外,只有mysql_monitor.sh颜色是正确的。 其他的不会突出显示,而是静音。

以下是脚本:

cat geek_scripts/mysql_monitor.sh#!/bin/bash
#br="$(tput -S <<<$'setaf 1nboldn')" # Bold Red
br="33[1;31m";
#bg="$(tput -S <<<$'setaf 2nboldn')" # Bold Green
bg="33[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="33[0m ";
UP=$(pgrep mysqld | wc -l);
if [ $UP != 1 ];
then
printf "MYSQL is ${br}down.${ar}n";
else
printf "MySQL is ${bg}running.${ar}n";
fi
cat geek_scripts/sip_monitor.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1nboldn')" # Bold Red
br="33[1;31m";
#bg="$(tput -S <<<$'setaf 2nboldn')" # Bold Green
bg="33[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="33[0m ";
#
# Monitor the status of the System Integrity Protection (SIP)
#
#printf "`csrutil status | grep --color 'disabled'`n";
if  csrutil status | grep 'disabled' &> /dev/null; then
printf "System Integrity Protection status: ${br}disabled${ar}n";
else
printf "System Integrity Protection status: ${bg}enabled${ar}n";
fi

cat geek_scripts/check_docker.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1nboldn')" # Bold Red
br="33[1;31m";
#bg="$(tput -S <<<$'setaf 2nboldn')" # Bold Green
bg="33[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="33[0m ";
#
# Check if docker is available and running
#
## will throw and error if the docker daemon is not running and jump
## to the next code chunk
docker_state=$(docker info >/dev/null 2>&1)
if [[ $? -ne 0 ]]; then
printf "Docker  is ${br}not running.${ar}n";
else
printf "Docker is ${bg}running.${ar}n";
fi

这些从newmain.bash称为:

cat geek_scripts/check_docker.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1nboldn')" # Bold Red
br="33[1;31m";
#bg="$(tput -S <<<$'setaf 2nboldn')" # Bold Green
bg="33[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="33[0m ";
#
# Check if docker is available and running
#
## will throw and error if the docker daemon is not running and jump
## to the next code chunk
docker_state=$(docker info >/dev/null 2>&1)
if [[ $? -ne 0 ]]; then
printf "Docker  is ${br}not running.${ar}n";
else
printf "Docker is ${bg}running.${ar}n";
fi

文件 geek_scripts/newmain.bash 的第一行也有 #!/bin/bash。

我想了解正在发生的事情以及如何获得我想要的东西,即check_docker.sh从命令行显示 Docker 是否正在运行,颜色正确且粗体。 为什么它不显示突出显示的颜色,最重要的是,为什么它不能确定 Docker 是否正常运行?

我知道我有明确的颜色代码,并且会在某个时候将它们排除在外,但是当从命令行在每个单独的文件中运行时,以及当我从命令行运行newmain.bash时,它们都可以工作,即。

geek_scripts/newmain.bash
Fri Jul 19 20:04:09 EDT 2019
woo-va-air
Mac OS X 10.15 19A512f
8gb RAM
Intel(R) Core(TM) i7-3667U CPU @ 2.00GHz
Docker  is not running.
MySQL is running.
System Integrity Protection status: disabled
up : 1 day, 10:27, RAM : , sys, 89.77% idle user  sys
CPU usage: 35.64
PhysMem: 7387M used
Disk Space:
Used: 10% Available: 93Gi
Used: 59% Available: 93Gi
Used: 3% Available: 93Gi
Used: 40% Available: 1.0Ti
Latest: 2019-07-19-195351
">

未运行"、"正在运行"和"禁用"在命令行中适当地以粗体红色、粗体绿色和粗体红色着色。 在屏幕上显示的极客脚本中,只有 MySQl 消息是粗体绿色,其他消息是常规红色,但同样,如果 Docker 正在运行,命令行显示它正在运行,但geek_script显示它未运行。

为什么?以及如何解决?

我发现当我从命令行运行check_docker.sh时,使用 $? 有效,但当它在 Geek Tool 运行的另一个脚本中运行时则无效。 我采纳了我在另一个问题中找到的关于在 if 中使用变量的建议,check_docker.sh然后将 else 脚本更改为:

(610)[:~/bin/geek_scripts]>

cat check_docker.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1nboldn')" # Bold Red
br="33[1;31m";
#bg="$(tput -S <<<$'setaf 2nboldn')" # Bold Green
bg="33[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="33[0m ";
#
# Check if docker is available and running
#
## will throw and error if the docker daemon is not running and jump
## to the next code chunk
/usr/local/bin/docker info > ~/tmp/docker_status 2>&1
if grep -q "Cannot connect" ~/tmp/docker_status; then
printf "Docker  is ${br}not running.${ar}n";
else
printf "Docker is ${bg}running.${ar}n";
fi
/bin/rm ~/tmp/docker_status;

请注意,我将 $? 替换为表达式:

if grep -q "Cannot connect" ~/tmp/docker_status; then

并在此之前:

/usr/local/bin/docker info > ~/tmp/docker_status 2>&1

我不喜欢每次评估时都必须写入磁盘,但找不到一种方法来获取标准并将错误输出到我可以在以下 grep 语句中使用的变量中。 希望有人告诉我如何做到这一点。 但是,这有效,效果很好,直到我找到更好的方法为止。 从我读到的$?是在前台运行的上一个命令的返回代码。 也许,在极客工具中作为极客脚本运行不会在前台运行原始命令?

因此,在脚本中使用脚本时的最佳建议是内联评估函数,而不是将它们的值收集到变量中,除非您能弄清楚如何将 std-out 和 error-out 放入同一个变量中。

最新更新