1

I am new to shell scripting, however i was trying to parse a command output and store those id values as an array.,

C:\Users\Shruthi>aws ecs list-tasks --cluster test-cluster --service hello2-service

{
    "taskArns": [
        "arn:aws:ecs:ap-south-1:822063991960:task/6990efe7-4a81-4fe7-8d25-d1ec1898971e",
        "arn:aws:ecs:ap-south-1:822063991960:task/0281508f-4d90-4ebc-afcd-684f58f5405e"
    ]
}

i want to store the above taskArns in to a array variable,

taskArn = (arn:aws:ecs:ap-south-1:822063991960:task/6990efe7-4a81-4fe7-8d25-d1ec1898971e arn:aws:ecs:ap-south-1:822063991960:task/0281508f-4d90-4ebc-afcd-684f58f5405e ..)

and so on, So that i can further use this task id's to describe specific task and find its current status.


i implemented the answered method, however i am not able to use the $line variable value for command substitution,

echo $FOO | while read -r line; do
    name="$line"
    echo "Name read from file - $name"

    #Successfully describes
    val="arn:aws:ecs:ap-southeast-1:296646925901:task/4d2bf627-82f5-45c7-8ad4-60b9e2f1934a"
    aws ecs describe-tasks --cluster mol-dev-cluster --tasks $val

    #Errors out
    aws ecs describe-tasks --cluster mol-dev-cluster --tasks $line
done

results in below error

error   14-Nov-2019 11:25:12    An error occurred (InvalidParameterException) when calling the DescribeTasks operation: taskId length should be one of [32,36]
simple  14-Nov-2019 11:25:12    Failing task since return code of [/tmp/runInDocker4657237329652069100.sh /home/bamboo/bamboo-agent-home/temp/47349848-46924268-46636883-ScriptBuildTask-5580550432412549027.sh] was 255 while expected 0

1 Answer 1

3

I put the output in foo.json for easy access. You could use jq to put those arns in a variable like this:

FOO=$(cat foo.json | jq .taskArns[])                                                                                                                                                                                                                    
-> echo $FOO                                                                                                                                                                                                                                               
"arn:aws:ecs:ap-south-1:822063991960:task/6990efe7-4a81-4fe7-8d25-d1ec1898971e"
"arn:aws:ecs:ap-south-1:822063991960:task/0281508f-4d90-4ebc-afcd-684f58f5405e"

Now you can simply iterate that variable:

echo $FOO | while read line                                                                                                                                                                                                                             
do
 echo $line
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.