0

I have a bash script that run a curl request and output the status via jq. I get the status as expected but when I compare the status it's always returning "non-equal" value, even if the value is equal (or seems so).

Here is the script:

status=( $(curl -H "Content-Type: application/json" -H "Authorization: Basic xxx==" -H "Cache-Control: no-cache" --data-binary "@$entry" $2/$3/_bulk_docs --silent | jq '.[0].status'))
echo $status
if [ "$status" = "409" ]
    then
      echo "Conflict"
    else
      echo "No Conflict"
fi

And it print 409 in the console as I expect.

I'm running this on a Windows 10 PC with Cygwin.

Thank's for further help!

  • Does the pipeline result in the string 409 or in "409"? – Kusalananda Jun 15 '17 at 13:56
  • How could I know this? (I'm a very beginner with bash scripts) – MHogge Jun 15 '17 at 13:57
  • 1
    What does echo "<<$status>>" return in your script? Can you tell that. – user218374 Jun 15 '17 at 14:00
  • Running echo "<<$status>>" returns >>409 – MHogge Jun 15 '17 at 14:02
5

Your data has a carriage return at the end:

$ status=$'409\r'
$ echo "<<$status>>"
>>409

To remove it, use tr -d '\r':

status=$( curl ... | jq ... | tr -d '\r' )

Also note that there is no need for status to be an array variable, as in your script, as far as I can see.

  • What do you mean by status is an array variable? status is inside an json object that is inside a json array, is that what you are referring to? – MHogge Jun 15 '17 at 14:09
  • With status=(...) you create an shell array variable, but since you only put a single string into it, you don't need to create the array. Just use status=$(...) instead of status=( $(...) ). – Kusalananda Jun 15 '17 at 14:11
  • True! I didn't see it as I was testing stuffs, etc. Thank's for the help ! – MHogge Jun 15 '17 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.