我有三个脚本: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 显示它没有运行。
为什么?以及如何解决?