首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >前面一个问题的不同方面:为什么在从geek_scripts运行时不会测试Docker工作的存在?

前面一个问题的不同方面:为什么在从geek_scripts运行时不会测试Docker工作的存在?
EN

Stack Overflow用户
提问于 2019-07-20 00:09:13
回答 1查看 86关注 0票数 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颜色是正确的。其他的没有突出显示,而是静音。

以下是脚本:

代码语言:javascript
复制
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

代码语言:javascript
复制
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中调用为:

代码语言:javascript
复制
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_script/newmain.bash文件也有#!/bin/bash作为第一行。

我想了解发生了什么,以及如何得到我想要的,即check_docker.sh显示Docker是否正在从命令行运行,并以正确的颜色和粗体显示。为什么它不显示高亮度的颜色,最重要的是,为什么它不能确定码头是否运行正常?

我知道我有显式的颜色代码,并会在某个时候将这些代码分解出来,但是当从命令行在每个单独的文件中运行时,以及当我从命令行运行newmain.bash时,它们都能工作。

代码语言:javascript
复制
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显示它没有运行。

为什么?以及如何修复?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-23 16:39:31

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

(610):~/bin/geek_script>

代码语言:javascript
复制
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;

注意,我替换了$吗?用以下词语表示:

代码语言:javascript
复制
if grep -q "Cannot connect" ~/tmp/docker_status; then

在此之前:

代码语言:javascript
复制
/usr/local/bin/docker info > ~/tmp/docker_status 2>&1

我不喜欢每次计算时都要写到磁盘上,但是找不到一种方法将标准输出并将错误输出到一个变量中,以便在下面的grep语句中使用。会很感激有人告诉我怎么做的。然而,这是工作,工作非常好,并会做,直到我找到一个更好的方法。从我读到的$?前一个命令在前台运行的返回代码。也许,在Geek工具中以极客脚本的形式运行并不是在前台运行原始命令吗?

因此,在脚本中使用脚本时,最好的建议是在线计算函数,而不是将它们的值收集到变量中,除非您能够弄清楚如何将std和error-out都输入到同一个变量中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57120776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档