繁体   English   中英

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

[英]Bash script comparing string with : in it

在比较字符串中带有 ':' 时遇到问题!

macOS 上的脚本是 bash。

我拥有的字符串来自 grep 的 output 调用docker-compose ps并且是(health: starting)

我试过if [[ $health == '(health: starting)' ]];then和变体。 为了测试我已经回显并得到的值(health:我试图在一个小脚本中重现这个问题,但它似乎工作正常。

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

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

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

call=$(docker-compose --compatibility ps | grep "^$1 ")
a=( $call )

,字符串(health: starting)将被拆分为数组中的 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-friendly output 格式。 如果不是,您必须自己解析 output。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM