whatthefish
whatthefish

Reputation: 347

Error in checking file downloaded properly through curl

I am trying to run curl command to download a file using sftp and after that checking if that file downloaded or not, but somehow my code is printing "File downloaded successfully" everytime even when the file did not download.

Here is my script:

#!bin/bash
curl -k -u "user:user" -ssl -o file.zip sftp:domain
if [[ $? -ne 0 ]]; then
    echo "Failed to download file"
    exit -1
fi
echo "File downloaded successfully"

Upvotes: 2

Views: 1505

Answers (1)

LMC
LMC

Reputation: 12662

Try using --write-out option as

#!bin/bash
rcode=$(curl --silent --write-out '%{response_code}' -k -u "user:user" -ssl -o file.zip sftp:domain)
if [[ "$rcode" -ne 0 ]]; then
    echo "Failed to download file"
    exit -1
fi
echo "File downloaded successfully"

For a 404 http response

rcode=$curl --silent --write-out '%{response_code})' http://localhost:8080/tyu echo "$rcode" 404

sftp response codes, success is 0.

Upvotes: 1

Related Questions