I have three scripts: mysql_monitor.sh, sip_monitor.sh, and check_docker.sh that each perform some test and show a message where the status is 'red' if not there and 'green' if not. These scripts are in another script called newmain.bash that is run inside a geek_scripts shell. If I run newmain.bash from the command line, then it detects if Docker is running or not, and puts the correct colors and highlighting on each of them. However, when it runs from the geek_scripts shell, it does not detect that Docker is running or not, always saying it is not running. Further, only the mysql_monitor.sh colors are correct. The others are NOT highlighted, but are muted.
Here are the scripts:
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
and
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
These are called from newmain.bash as:
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
The file geek_scripts/newmain.bash also has #!/bin/bash as the first line.
I'd like to understand what is happening and also how to get what I want, i.e. check_docker.sh show whether Docker is running or not from the command line, properly colored and in bold. Why doesn't it show the colors high lighted and above all, why doesn't it determine whether Docker is running correctly?
I know I have explicit color codes and will factor those out at some point, but they work when run in each individual file from the command line, AND also when I run newmain.bash from the command line, viz.
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
The 'is not running', 'is running', and 'disabled' are colored bold-red, bold-green, and bold-red appropriately from the command line. In the geek-scripts display on the screen, only the MySQl message is bold-green, the others are regular-red, but again, if Docker is running, the command line shows it running, but the geek_script shows it as not running.
Why? and how to fix?