Home > Net >  bad substitution running aws-cli for-loop
bad substitution running aws-cli for-loop

Time:03-30

I'm trying to run the below command but receiving a bad substitution error.

for i in ${aws eks list-clusters --query 'clusters' --output text | tr '\t' '\n' | grep dev-shark}; do aws eks describe-cluster --name $i | jq '.cluster.tags."Dev-Ver"'; done

see error msg below

bash: ${aws eks list-clusters --query 'clusters' --output text | tr '\t' '\n' | grep dev-shark}: bad substitution

Any ideas what maybe causing this?

Thanks.

CodePudding user response:

The last pipe is not setting any action "| dev-shark", are you looking to filter against the text "dev-shark" then the code should be

for i in ${aws eks list-clusters --query 'clusters' --output text | tr '\t' '\n' | grep dev-shark}; do aws eks describe-cluster --name $i | jq '.cluster.tags."Dev-Ver"'; done

CodePudding user response:

Don't use for to read lines.

I'd suggest pipe the output of the aws|tr|grep pipeline into a while read loop: adding line continuations and newlines for readability

aws eks list-clusters --query 'clusters' --output text \
| tr '\t' '\n' \
| grep dev-shark \
| while IFS= read -r line; do
      aws eks describe-cluster --name "$line" \
      | jq '.cluster.tags."Dev-Ver"'
  done
  • Related