0

I am facing one issue while executing the CURL command using shell. The issue is i want to get the status of the curl command execution.

my command is :

    location=curl https://invvvv.usvvv.com/agg/tari?uswg-key=8e3eedhhhh4a9a370605637 --output output.zip --proxy proxy-az.azure.ghop.fguiop.com:8080

The issue is if it is success then i will be getting one location in the ${location} variable, which is perfectly fine.But if the CURL execution is failure, i am not getting any values in the variable (only empty value).

My question is how can i get the status of curl command execution, i have tried with if [ $? -eq 0 ], but for failure also it is showing $? is 0.

How can i get the status of the curl command ? please help

BigD
  • 850
  • 2
  • 17
  • 40

1 Answers1

1

Curl does have different exit status to inform how the program finished. Running your example, I got exit status 5 "Couldn't resolve proxy", and then 6 "Couldn't resolve host". You can see a list of Curl exit status here: https://ec.haxx.se/usingcurl-returns.html

curl https://invvvv.usvvv.com/agg/tari?uswg-key=8e3eedhhhh4a9a370605637 --output output.zip --proxy proxy-az.azure.ghop.fguiop.com:8080 ; echo "RETURN CODE: $?"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (5) Could not resolve proxy: proxy-az.azure.ghop.fguiop.com
RETURN CODE: 5

curl https://invvvv.usvvv.com/agg/tari?uswg-key=8e3eedhhhh4a9a370605637 --output output.zip ; echo "RETURN CODE: $?"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: invvvv.usvvv.com
RETURN CODE: 6

Actually, I'm getting an error running the example line, if you want to capture stdout of a program, you might include it between $(). Although, you are redirecting stdout with --output output.zip.

Here is a example storing curl stdout in a variable.

location=$(curl example.com)
if [ $? -eq 0 ]; then 
    echo "Success" 
else 
    echo "Failure $?" 
fi
echo $location

However, if what you want to test is the HTTP response code, you can use the example below.

curl -s -o response.txt -w "%{http_code}" http://example.com

More details on this here Curl to return http status code along with the response

Marcos
  • 645
  • 3
  • 10
  • There's no reason to use `$?` if you're only making a zero/nonzero determination. `if location=$(curl example.com); then` will test whether `curl` returned a zero exit status, but doesn't run the risk of `$?` being disrupted by extra logging commands, DEBUG traps, etc. See also [BashPitfalls #44](http://mywiki.wooledge.org/BashPitfalls#cmd.3B_.28.28_.21_.24.3F_.29.29_.7C.7C_die). – Charles Duffy Apr 06 '19 at 22:39
  • ...and `echo $location` runs afoul of [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). – Charles Duffy Apr 06 '19 at 22:39