0

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 
  • See [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/a/285557/14122) on [meta]. – Charles Duffy Feb 14 '20 at 02:22
  • The generic bash end of this issue is that single quotes make all data literal, including data that might be treated as a parameter expansion if they were instead in a double-quoted context. Just changing your quoting type isn't the right answer, though, as doing so introduces all the bugs common to using string concatenation to substitute data into code; the right answer is to use the `--arg` option to jq, as shown in the linked duplicate. – Charles Duffy Feb 14 '20 at 02:25
  • I am unable to paste the code properly here. Please check the code here https://github.com/sprider/sample/blob/master/script.sh –  Feb 14 '20 at 05:27
  • See the code pasted in the question and displayed properly. That said, I do believe the linked duplicate already provides an adequate answer to the question originally asked. If you have a new and different question, please [edit] to be asking it clearly and explicitly, with a [mre] explicitly showing both actual and desired behavior, and everything not essential to understanding (and testing proposed answers to) that new question removed. – Charles Duffy Feb 14 '20 at 05:40
  • ```groupsMatch=`echo $groups | jq -c --arg GivenName $userGivenName --arg Surname $userSurname 'map(select((.mail|contains($GivenName)) or (.mail|contains($Surname))))'```` I tried to change the code as shown here but groupsMatch is empty where as if i pass the static values it is returning the expected values –  Feb 14 '20 at 05:56
  • If you run your script with `set -x` to enable logging, how does it say that command is actually being executed? – Charles Duffy Feb 14 '20 at 13:39
  • (BTW, again, relying on `IFS=$'\n'` to make use of unquoted expansions safe is a *really* bad idea; there's less to go wrong if you make it `groupsMatch=$(jq -c --arg GivenName "$userGivenName" --arg Surname "$userSurname" 'map(select((.mail|contains($GivenName)) or (.mail|contains($Surname))))'` -- if that still doesn't work, I really would need to either need input data to use -- without which the code doesn't constitute a [mre] as only you have access to your o365 data so nobody else can test whether the problem is fixed -- or `set -x` logs). – Charles Duffy Feb 14 '20 at 13:40
  • Hi Charles, I tried with argument option but it is not working. Please check the sample code in my first post. –  Feb 18 '20 at 04:42
  • Looks good if we change code as shown here userGivenName=`echo $user | jq -r '.givenName'` userSurname=`echo $user | jq -r '.surname'` –  Feb 18 '20 at 05:58

0 Answers0