Reputation: 723
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
Upvotes: -2
Views: 686
Reputation: 203502
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.
Upvotes: 0
Reputation: 189
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.
Upvotes: 2