先前问题的不同方面:当 geek_scripts 中的 运行 时,为什么不测试 Docker 的存在不起作用?
Different aspect of an earlier question: Why doesn't test for existence of Docker working does not work when run from geek_scripts?
我有三个脚本:mysql_monitor.sh、sip_monitor.sh 和 check_docker.sh,每个脚本都执行一些测试并显示一条消息,如果不存在则状态为 'red' 并且'green' 如果没有。这些脚本位于另一个名为 newmain.bash 的脚本中,该脚本位于 geek_scripts shell 中的 运行。如果我从命令行 运行 newmain.bash ,那么它会检测 Docker 是否是 运行ning ,并在每个颜色上放置正确的颜色和突出显示。但是,当它从geek_scriptsshell中运行时,它并没有检测到Docker是否是运行ning,总是说它不是运行宁。此外,只有 mysql_monitor.sh 颜色是正确的。其他的没有突出显示,而是静音。
这是脚本:
cat geek_scripts/mysql_monitor.sh#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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 是否 运行ning,正确着色并以粗体显示。为什么它不显示高亮颜色,最重要的是,为什么它不能确定 Docker 是否 运行ning 正确?
我知道我有明确的颜色代码,并且会在某些时候将它们分解出来,但是当 运行 从命令行在每个单独的文件中以及当我 运行 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 是 运行ning,命令行显示它 运行ning,但 geek_script 显示它不是 运行ning。
为什么?以及如何解决?
我发现使用$?当我从命令行 运行ning check_docker.sh 时工作,但当它在 Geek Tool 的另一个脚本 运行 中 运行ning 时不工作。我采纳了我在另一个问题中找到的关于在 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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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 语句中使用的变量的方法。很感激有人告诉我如何做到这一点。但是,这有效,非常有效,并且会一直有效,直到我找到更好的方法。从我读到的$?是前台上一个命令运行的return代码。也许,运行ning 在 Geek Tool 中作为一个 geek 脚本不会 运行 原来的命令在前台?
因此,在脚本中使用脚本时,最好的建议是评估函数 in-line 而不是将它们的值收集到变量中,除非你能弄清楚如何同时获得 std-out 和 error-out到同一个变量。
我有三个脚本:mysql_monitor.sh、sip_monitor.sh 和 check_docker.sh,每个脚本都执行一些测试并显示一条消息,如果不存在则状态为 'red' 并且'green' 如果没有。这些脚本位于另一个名为 newmain.bash 的脚本中,该脚本位于 geek_scripts shell 中的 运行。如果我从命令行 运行 newmain.bash ,那么它会检测 Docker 是否是 运行ning ,并在每个颜色上放置正确的颜色和突出显示。但是,当它从geek_scriptsshell中运行时,它并没有检测到Docker是否是运行ning,总是说它不是运行宁。此外,只有 mysql_monitor.sh 颜色是正确的。其他的没有突出显示,而是静音。
这是脚本:
cat geek_scripts/mysql_monitor.sh#!/bin/bash
#br="$(tput -S <<<$'setaf 1\nbold\n')" # Bold Red
br="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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 是否 运行ning,正确着色并以粗体显示。为什么它不显示高亮颜色,最重要的是,为什么它不能确定 Docker 是否 运行ning 正确?
我知道我有明确的颜色代码,并且会在某些时候将它们分解出来,但是当 运行 从命令行在每个单独的文件中以及当我 运行 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 是 运行ning,命令行显示它 运行ning,但 geek_script 显示它不是 运行ning。
为什么?以及如何解决?
我发现使用$?当我从命令行 运行ning check_docker.sh 时工作,但当它在 Geek Tool 的另一个脚本 运行 中 运行ning 时不工作。我采纳了我在另一个问题中找到的关于在 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="3[1;31m";
#bg="$(tput -S <<<$'setaf 2\nbold\n')" # Bold Green
bg="3[1;32m";
#ar="$(tput sgr0)" # Text attributes reset
ar="3[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 语句中使用的变量的方法。很感激有人告诉我如何做到这一点。但是,这有效,非常有效,并且会一直有效,直到我找到更好的方法。从我读到的$?是前台上一个命令运行的return代码。也许,运行ning 在 Geek Tool 中作为一个 geek 脚本不会 运行 原来的命令在前台?
因此,在脚本中使用脚本时,最好的建议是评估函数 in-line 而不是将它们的值收集到变量中,除非你能弄清楚如何同时获得 std-out 和 error-out到同一个变量。