Search code examples
bashcurleclipse-hawkbithawkbit

How to store curl response in bash script?


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


Solution

  • 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.