-1

I want to construct a command based upon the number of groups in the yaml file shown in the Example..

Example:

I have groups command as below which extracts the groups from the iamIdentityMappings yaml file,

groups=$(yq read -j generated/identity-mapping.yaml "iamIdentityMappings.[0].groups")

iamIdentityMappings yaml file:

iamIdentityMappings:
- groups:
  - Appdeployer
  - Moregroups
  rolearn: arn:aws:iam::12345:role/eks-project-us-east-1-ppdeployer
  username: user1

Since there are two groups in groups array,I need to add two groups in the below command,

 eksctl create iamidentitymapping --cluster "$name" --region "$region" --arn "$rolearn" --group "Appdeployer" -group "Moregroups" --username "$username"

If there are 3 groups,then --group should be repeated 3 times in the command.

Please let me know how to do this in bash

Rad4
  • 1,936
  • 8
  • 30
  • 50
  • Does this answer your question? [Convery yaml array to string array](https://stackoverflow.com/questions/62926327/convery-yaml-array-to-string-array) – user1934428 Jul 16 '20 at 06:07
  • What have you tried? Why/how did it not work? http://idownvotedbecau.se/noattempt/ – Paul Hodges Jul 16 '20 at 13:49

1 Answers1

0

I have figured out..

grouparr=''
    for group in $groups; do
       echo $group
       if [[ ! $group == '-' ]]; then
          grouparr+=" --group $group "
       fi
    done
Rad4
  • 1,936
  • 8
  • 30
  • 50
  • 1
    Always quote used variables. Also `${name}` syntax is recommended – Marcin Orlowski Jul 16 '20 at 04:14
  • 1
    The `for group in $groups` part can fail in weird ways if any group names contain spaces (or other whitespace) or anything that can be mistaken for a shell filename wildcard (that includes square brackets). – Gordon Davisson Jul 16 '20 at 04:39
  • 1
    @Rad4: Instead of putting the values together into a **string** and rely on word splitting to tear them apart (which would fail if you have spaces in group names), I suggest that you use an **array** to collect the paramters. – user1934428 Jul 16 '20 at 06:04