Bash脚本将字符串与其中的进行比较

Bash脚本将字符串与其中的进行比较,bash,macos,docker,Bash,Macos,Docker,无法将字符串与其中的“:”进行比较 脚本是macOS上的bash 我拥有的字符串来自调用docker compose ps输出的grep,是(健康:开始) 我尝试了if[[$health=='(health:starting)]];然后和变体。为了测试这个值,我重复了它,并得到了(健康状况:),我尝试用一个小脚本来重现这个问题,但似乎效果不错 此脚本正在进行中,因此可能存在其他问题 function getStatus { local __resultvar=$1 local

无法将字符串与其中的“:”进行比较

脚本是macOS上的bash

我拥有的字符串来自调用
docker compose ps
输出的grep,是
(健康:开始)

我尝试了
if[[$health=='(health:starting)]];然后
和变体。为了测试这个值,我重复了它,并得到了
(健康状况:
),我尝试用一个小脚本来重现这个问题,但似乎效果不错

此脚本正在进行中,因此可能存在其他问题

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


使用
case/esac
这种方法怎么样

  case $health in
  (*:*) echo has a colon;;
  (*)   echo has no colon;;
  esac

这很容易扩展到
(健康)
等的其他测试中。

使用
case/esac的这种方法怎么样

  case $health in
  (*:*) echo has a colon;;
  (*)   echo has no colon;;
  esac
这很容易扩展到
(健康)
等的其他测试中。

使用

call=$(docker-compose --compatibility ps | grep "^$1 ")
a=( $call )
,字符串
(运行状况:开始)
将在数组中拆分为2个元素,因此它将无法按预期工作。您可以这样进行验证:

[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] $
不确定docker compose ps是否支持其他shell友好的输出格式。如果不支持,您必须自己解析输出。

使用

call=$(docker-compose --compatibility ps | grep "^$1 ")
a=( $call )
,字符串
(运行状况:开始)
将在数组中拆分为2个元素,因此它将无法按预期工作。您可以这样进行验证:

[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] $

不确定docker compose ps是否支持其他shell友好的输出格式。如果不支持,您必须自己解析输出。

我不认为是冒号,我认为是冒号后面的空格——如果我了解发生了什么,您创建数组的方式会将“(健康:“在一个数组元素中,并且“开始”)在下一个例子中。事实上,您拆分和解析输出的方式似乎是不愉快的即兴和脆弱的。我不认为是冒号,我认为是冒号后面的空格——如果我了解发生了什么,那么您创建数组的方式将放在“(health:“在一个数组元素中,并且“开始”)在下一篇文章中。事实上,你拆分和解析输出的方式似乎是不愉快的临时和脆弱的。谢谢,问题解决了。我应该自己解决,而不是假设!谢谢,问题解决了。我应该自己解决,而不是假设!不是一个解决方案,而是我没有想到的,谢谢。不是解决方案,而是我想的没想到,谢谢。