Search code examples
jsonbashjqmingw-w64for-in-loop

Bash JQ filter code not run properly between DO -> DONE (WINDOWS + GITBASH)


I am running a piece of JQ filter code. This is been run on a Windows Server 2016 with GIT BASH.

the code looks like the following: (don't mind the echo's and echo $i, this is for verbose reasons)

FILENAME="c:\Test\output4p.json"
for i in $(cat "$FILENAME" | jq -r .[].personnelNumber); do 
  echo $i
  echo
  jq '.[] | select(.personnelNumber=="'$i'")' "$FILENAME"
  echo
   done

it partly seems to run fine but sadly the output is. output1

so I on purpose remove the -r in the top JQ code to have a look what is actually going wrong.

$ "C:\TEST\echo.sh"
"3633014"

jq: error: syntax error, unexpected LITERAL, expecting ';' or ')' (Windows cmd shell quoting issues?) at <top-level>, line 1:
")                                 33014"
jq: 1 compile error



"3634594"

jq: error: syntax error, unexpected LITERAL, expecting ';' or ')' (Windows cmd shell quoting issues?) at <top-level>, line 1:
")                                 34594"
jq: 1 compile error



"3634708"

jq: error: syntax error, unexpected LITERAL, expecting ';' or ')' (Windows cmd shell quoting issues?) at <top-level>, line 1:
")                                 34708"
jq: 1 compile error



"3634847"

jq: error: syntax error, unexpected LITERAL, expecting ';' or ')' (Windows cmd shell quoting issues?) at <top-level>, line 1:
.[] | select(.personnelNumber==""3634847"")
jq: 1 compile error

if there is only 1 person in the json everything works as expected. but when there are more, the code only runs properly on the last person in the "For i in $cat" list.

someone else encounter something like this before?

thank you guys in advance

I am first making making a loop based upon the personnelNumber. Than for each personnelNumber I want to print the full array (which doesn't have an array ID) When this is working fine I'm going to take this code further into a curl command.


Solution

  • The Comment of Gordon Davisson was exacly what was going on.

    That weirdly-formatted error message makes me suspect you've got DOS/Windows line endings somewhere

    Gordon passed me over a link to a earlyer post of something quite similar.

    Are shell scripts sensitive to encoding and line endings?

    I added the this piece of code to the top and i got all the correct outcommes.

    set -o igncr
    

    So conclusion, if you are running GITBASH or any Cygwin BASH make sure the Carriage Return in line endings are correct.