1

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!

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

1 Answer 1

6

Your data has a carriage return at the end:

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

To remove it, use tonumber in jq:

status=$( curl ... | jq -r '.[0].status|tonumber' )

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

3
  • 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? Commented Jun 15, 2017 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=( $(...) ). Commented Jun 15, 2017 at 14:11
  • True! I didn't see it as I was testing stuffs, etc. Thank's for the help ! Commented Jun 15, 2017 at 14:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.