1

I have a small bash script to do some AWS CLI stuff. I am having issues with line 18 as I need to compare function1 =! (function2 && string) specifically

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

see below;

#!/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

I am trying to aviod using jq as I want to keep dependencies to a minimum

eekfonky
  • 819
  • 4
  • 16
  • 33
  • This should be something like this (depending on functions result format): `if [ "$(state)" != "$(task_name) RUNNING" ]` – SergA Apr 13 '18 at 14:28
  • @anubhava `task_name` returns the name of an AWS Task. e.g. `mis-dev-core`. `state` returns the same string with it's state. e..g `mis-dev-core RUNNING`. @SergA I tried your command and get; `./check-status.sh: line 18: [: missing `]'` – eekfonky Apr 13 '18 at 14:35
  • PS: You still using `jq` while parsing task list. – SergA Apr 13 '18 at 15:18
  • `!=` is an argument to the command `[`. It doesn't make any sense outside of a `[`, and when you have `&&`, that's a command separator -- it ends one command and starts a new one. When you put one in the middle of a `[` command, then, you're truncating that command (and putting everything after it in a new one). – Charles Duffy Apr 13 '18 at 15:21
  • A single string that adds the word RUNNING after your task name is `"$(task_name) RUNNING"` -- there's no `&&` involved. – Charles Duffy Apr 13 '18 at 15:23

1 Answers1

0

I have no AWS CLI stuff, so here is example with stubs:

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

UPDATE: As noted @charles-duffy, use array instead string.

SergA
  • 1,097
  • 13
  • 21