Romas Augustinavičius
Romas Augustinavičius

Reputation: 443

"curl" does not work in script

I wrote curl which returns only http status code :

curl --write-out %{http_code} \n
     --silent \
     --output /dev/null \ 
      $URL

It works fine if I execute this from console. But after I have puted it into script, like this:

HTTP_STATUS=$(curl --write-out %{http_code} \n
            --silent \
            --output /dev/null \ 
            $URL)

And try to echo $HTTP_STATUS, result is 200000000000000000000000000000000000000000000000000

How can I fix it?

Upvotes: 1

Views: 2096

Answers (1)

sjsam
sjsam

Reputation: 21965

I wrote curl which returns only http status code

There are couple of issues with your script.

  • Your use of UPPERCASE variables might override shell environment variables.
  • The --write-out argument of the curl can ideally be within double quotes.

status=$(curl --write-out "%{http_code}" --silent --output /dev/null "$url")
echo "$status" # Would give you just the status

Note: As pointed out in this comment you don't need the newline too since you're assigning the value to a variable.

Upvotes: 2

Related Questions