Skip to content
Advertisement

Process “aws ecs list-tasks” JSON output with “aws ecs execute-command”?

For example do this command.

JavaScript

then it returns below

JavaScript

then do next command using the ae340032378f4155bd2d0eb4ee60b5c7 of return sentence.

JavaScript

I want to do this thing in one sentence, or shell script. Is it possible?

I googled around the regular expression, but still unclear.

JavaScript

Could you give some help?

Advertisement

Answer

Manipulate JSON with jq

jq is the best tool for manipulating JSON in shell scripts. It has a pretty sophisticated query language.

Here’s how you could use it to extract the string you need. I’ve shown the query I’ve built piece by piece so you can see the what’s happening a step at a time:

JavaScript

Capture output with $(…)

The next step is to add -r so it prints the string raw without quotes, and use $(...) to capture the output so we can reuse it in a second command.

JavaScript

Or use xargs

Another way to write this is with xargs, which takes the output of one command and passes it as an argument to the next.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement