I am trying to select the groups(groupsMatch) with the following filter conditions but the dynamic variables(userGivenName and userSurname) are going as empty. Any idea?
Sample code with data
#!/bin/bash
# requires jq: https://stedolan.github.io/jq/
defaultIFS=$IFS
IFS=$'\n'
users='[{"id":"123","givenName":"jose","surname":"sam"},{"id":"456","givenName":"anbu","surname":"velliah"},{"id":"789","givenName":"test","surname":"user"}]'
groups='[{"id":"1","mail":"orphangroup@test.com"},{"id":"2","mail":"josegroup@test.com"},{"id":"3","mail":"grp-velliah@test.com"}]'
for user in `echo $users | jq -c '.[]'`; do
userGivenName=`echo $user | jq '.givenName'`
userSurname=`echo $user | jq '.surname'`
groupsMatch=$(echo $groups | jq -c --arg GivenName "$userGivenName" --arg Surname "$userSurname" 'map(select((.mail|contains($GivenName)) or (.mail|contains($Surname))))')
for group in `echo $groupsMatch | jq -c '.[]'`; do
groupId=`echo $group | jq '.id'`
groupMail=`echo $group | jq '.mail'`
groupObject=$(jq -n -c \
--arg id ${groupId} \
--arg gm ${groupMail} \
--arg gn ${userGivenName} \
--arg sn ${userSurname} \
'{groupId: $id, groupMail: $gm, userGivenName: $gn, userSurname: $sn}')
groupsToFlag+=($groupObject)
done
done
echo ${groupsToFlag[@]} | jq -csr '(.[0] |keys_unsorted | @tsv), (.[]|.|map(.) |@tsv)' | column -s$'\t' -t
IFS=defaultIFS
exit 1