0

我有三个脚本: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 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[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 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[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 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[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 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[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

'is not running'、'is running' 和 'disabled' 从命令行适当地着色为粗体红色、粗体绿色和粗体红色。在屏幕上的 geek-scripts 显示中,只有 MySQl 消息是粗体绿色,其他是常规红色,但同样,如果 Docker 正在运行,命令行显示它正在运行,但 geek_script 显示它没有运行。

为什么?以及如何解决?

4

1 回答 1

0

我发现使用$?当我从命令行运行 check_docker.sh 时工作,但当它在 Geek Tool 运行的另一个脚本中运行时不起作用。我接受了在另一个问题中找到的关于在 if、then、else 脚本中使用变量的建议,并将 check_docker.sh 更改为:

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

cat check_docker.sh
#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="\033[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="\033[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="\033[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 语句中使用的变量的方法。很感激有人告诉我如何做到这一点。但是,这很有效,效果很好,并且在我找到更好的方法之前会一直这样做。从我读到的 $? 是前一个在前台运行的命令的返回码。也许,作为极客脚本在 Geek Tool 中运行不会在前台运行该原始命令?

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

于 2019-07-23T16:39:31.820 回答