bash比较变量和变量+字符串

时间:2018-04-13 14:21:59

标签: bash if-statement

我有一个小的bash脚本来做一些AWS CLI的东西。我在第18行遇到问题,因为我需要专门比较function1 =! (function2 && string)

if [ "$(state)" ] != [ "$(task_name)" && "RUNNING" ]

见下文;

#!/bin/bash

region="eu-west-1"
cluster="mis-core-dev"

## Get task name, state and check against "RUNNING"
task_name () {
  aws ecs describe-tasks --region ${region} --cluster ${cluster} --tasks "$task" --query 'tasks[].containers[].name' --output text
}
state () {
  aws ecs describe-tasks --region ${region} --cluster ${cluster} --tasks "$task" --query 'tasks[].containers[].[name, lastStatus]' --output text
}

## Get Task names from Cluster
task_status () {
  mapfile -t tasksInCluster < <(aws ecs list-tasks --region ${region} --cluster ${cluster} | jq -r '.taskArns[]')
  for task in "${tasksInCluster[@]}"; do
      if [ "$(state)" ] != [ "$(task_name)" && "RUNNING" ] 
      then
        echo "$(task_name)" "NOT RUNNING!"
        exit 1
      else
        state
      fi
  done
}

task_status

我正在尝试使用jq进行避免,因为我希望将依赖关系保持在最低限度

1 个答案:

答案 0 :(得分:0)

我没有AWS CLI的东西,所以这里是存根的例子:

task_name () {
  echo "$task"
}
state () {
  echo "$task RUNNING"
}

task_status () {
  tasksInCluster=(mis-dev-core mis-dev-other "mis-dev with space")
  for task in "${tasksInCluster[@]}"; do
      if [ "$(state)" != "$(task_name) RUNNING" ] 
      then
        echo "$(task_name)" "NOT RUNNING!"
        exit 1
      else
        state
      fi
  done
}

task_status

更新:如前所述@ charles-duffy,请使用数组代替字符串。