Have trouble comparing a string with a ':' in it!
The script is bash on macOS.
The string I have is from grep of the output of a call to docker-compose ps
and is (health: starting)
.
I have tried if [[ $health == '(health: starting)' ]];then
and variants. To test the value I have echoed it and get (health:
I have tried to reproduce the issue in a small script but it seems to work fine.
This script is a work in progress so may have other problems
function getStatus {
local __resultvar=$1
local myresult='some value'
# call=$(docker-compose --compatibility ps | grep "^$1 ")
# a=( $call )
# asize=${#a[@]}
case $asize in
4)
status=${a[2]}
;;
6)
status=${a[4]}
;;
7)
status=${a[5]}
if [[ "$status" =~ [^A-Za-z] ]]; then
status=${a[4]}
fi
;;
8)
status=${a[6]}
;;
9)
status=${a[4]}
;;
10)
service=${a[0]}
if [[ $service == "identification-service" ]]; then
status=${a[7]}
else
status=${a[4]}
fi
;;
11)
status=${a[8]}
if [[ $status -eq "Up" ]]; then
health=${a[9]}
else
health=""
fi
;;
12)
status=${a[8]}
if [[ $status -eq "Up" ]]; then
health="${a[9]}"
else
health=""
fi
;;
13)
status=${a[8]}
if [[ $status -eq "Up" ]]; then
health="${a[9]}"
else
health=""
fi
;;
*)
echo "Cant decode that! $asize elements"
echo
index=0
while [ $index -le $asize ]
do
echo "${a[$index]}"
((index++))
done
exit
;;
esac
if [[ "$health" ]]; then
if [[ $health == '(healthy)' ]];then
result='healthy'
elif [[ $health == '(unhealthy)' ]];then
result='unhealthy'
elif [[ $health == '(health: starting)' ]];then
result='starting'
else
result=$health
fi
else
result=$status
fi
}
call=$(docker-compose --compatibility ps | grep "^$1 ")
# echo "$call"
a=( $call )
asize=${#a[@]}
# echo "Size: $asize - '$call'"
result=""
for VARIABLE in {1..10}
do
getStatus
echo "result = $result"
if [[ $result == 'Up' ]]; then
echo "$1 is up"
exit
elif [[ $result == 'healthy' ]]; then
echo "$1 is up and healthy"
exit
fi
echo "currently $result - waiting 3 second before checking again"
sleep 3
done
echo "status of $1 is: $status but $health"
echo
What about this method using case/esac
?
case $health in
(*:*) echo has a colon;;
(*) echo has no colon;;
esac
This is easy to extend for your other tests for (healthy)
etc.
With
call=$(docker-compose --compatibility ps | grep "^$1 ")
a=( $call )
, the string (health: starting)
will be split into 2 elements in the array so it won't work as you expect. You can verify like this:
[STEP 101] $ out=$( echo "... (health: starting) ..." )
[STEP 102] $ echo "$out"
... (health: starting) ...
[STEP 103] $ a=( $out )
[STEP 104] $ declare -p a
declare -a a=([0]="..." [1]="(health:" [2]="starting)" [3]="...")
[STEP 105] $
Not sure if docker-compose ps
supports other shell-friendly output format. If not you have to parse the output all by yourself.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.