-2

I am trying to download file from hawkbit using curl in a bash script. Below is the curl invocation

curl --fail -sS -O -C - --location "${HEADERS[@]}" -w "%{http_code}\n" -X GET "$temp_url"

Now I want to store any error message from curl --fail if there is any to a variable. (example: curl: (22) The requested URL returned error: 416)

#!/bin/bash

response=$(curl --fail -sS -O -C - --location "${HEADERS[@]}" -w "%{http_code}\n" -X GET "$temp_url")
exit_status=$?
echo "exit status is $exit_status"
http_code=${response: -3}

if [ $exit_status -eq "0" ] && [ $http_code -eq "200" ] ; then
    echo "Success"
else
    echo "Failure"
    exit $exit_status
fi

Can anyone please let me know how should I improve the behavior of curl response?

P.S: Please let me know if any info is missing

Preeti
  • 535
  • 1
  • 6
  • 30
  • Does this answer your question? [Capture stdout and stderr into different variables](https://stackoverflow.com/questions/11027679/capture-stdout-and-stderr-into-different-variables) – user1934428 Apr 03 '23 at 06:14

2 Answers2

2

You are storing the "exit_status" before your curl command. This will always set it to the value 0. And your curl command doesn't seem to be complete either, but that's another matter.

Swifty
  • 180
  • 7
  • What do you mean by "curl command doesn't seem to be complete? – Preeti Apr 02 '23 at 12:32
  • Well, nothing special. It just made me curious, because if I try your code as is, I sometimes get a 416... Besides that, have you fixed your code by moving the exit_status after the curl command? – Swifty Apr 02 '23 at 13:05
  • Yes, I have fxed it – Preeti Apr 02 '23 at 13:11
  • `curl` completes. 416 is an HTTP response code that indicates a problem with the request, not that the request hasn't been completed yet. (Oh, I caught the question just after it was edited to set `exit_status` after `curl`.) – chepner Apr 02 '23 at 13:12
0

I think what you're really asking for help with is how to store the error message from a curl failure, if any:

$ curl --fail 'foo:bar'
curl: (3) URL using bad/illegal format or missing URL

$ echo $?
3

curl Fail:

$ { response=$( curl --fail 'foo:bar' 2>&1 >&3 3>&-); } 3>&1

$ echo $?
3

$ echo "$response"
curl: (3) URL using bad/illegal format or missing URL

curl Success:

$ { response=$( curl --fail 'http://example.com' 2>&1 >&3 3>&-); } 3>&1
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

$ echo $?
0

$ echo "$response"

See https://unix.stackexchange.com/a/474195/133219.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185