检查通过curl正确下载的文件时出错

时间:2018-04-25 18:41:08

标签: bash curl

我正在尝试运行curl命令以使用sftp下载文件,然后检查该文件是否已下载,但不知何故我的代码每次都打印“文件已成功下载”,即使文件未下载也是如此。

这是我的剧本:

#!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"

1 个答案:

答案 0 :(得分:0)

尝试使用--write-out选项作为

#!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"

对于404 http响应

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

sftp response codes,成功为0。