How to save output of AWS CLI in a variable?

倖福魔咒の 提交于 2021-01-28 04:24:17

问题


I want to save output of an AWS CLI in a variable and use that variable in another AWS CLI, what I did is as follows:

taskarn= aws ecs list-tasks --cluster  mycluster --service-name "myService" --region "eu-west-1" --output text | grep "arn" | tr -d '"'

echo  $taskarn;  //empty
aws ecs stop-task --cluster mycluster --task $taskarn --region "eu-west-1"

when I echo $taskarn, it is empty.

Any help would be appreciated.


回答1:


I used the following command and it works fine:

taskarn=$(aws ecs list-tasks --cluster  mycluster --service-name "myservice" --region "eu-west-1" | grep "arn" | tr -d '"')
echo  $taskarn;

aws ecs stop-task --cluster mycluster --task $taskarn --region "eu-west-1"



回答2:


Use backquote to execute the command and assign the result to the variable.

taskarn=`aws ecs list-tasks --cluster  mycluster --service-name "myService" --region "eu-west-1" --output text | grep "arn" | tr -d '"'`

But the correct way is to use the --query option of the CLI to extract what you want.



来源:https://stackoverflow.com/questions/50533308/how-to-save-output-of-aws-cli-in-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!