From 651c981cad17bf2c296513a21902887d3933e337 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 14 Jan 2023 13:57:32 +1100 Subject: [PATCH] bash loop example --- usage/tips-and-tricks.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/usage/tips-and-tricks.md b/usage/tips-and-tricks.md index d0b71749..8051fe21 100644 --- a/usage/tips-and-tricks.md +++ b/usage/tips-and-tricks.md @@ -42,6 +42,35 @@ You can create a bash array named `actions` by: edit ``` + +## yq in a bash loop + +For a given yaml file like: +``` +identities: +- arn: "arn:aws:iam::ARN1" + group: "system:masters" + user: "user1" +- arn: "arn:aws:iam::ARN2" + group: "system:masters" + user: "user2" +``` + +You can loop over the results in a bash loop like: + +``` +# load array into a bash array +# output each entry as a single line json +readarray identityMappings < <(yq -o=j -I=0 '.identities[]' test.yml ) + +for identityMapping in "${identityMappings[@]}"; do + # identity mapping is a single json snippet representing a single entry + roleArn=$(echo "$identityMapping" | yq '.arn' -) + echo "roleArn: $roleArn" +done + +``` + ## Set contents from another file Use the [load](https://mikefarah.gitbook.io/yq/operators/load) operator to load contents from another file.