在 bash 中获取 cURL 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7678675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:55:52  来源:igfitidea点击:

Get cURL response in bash

bashcurl

提问by RGilkes

I have a simple bash script that uploads files to an FTP. I was wondering how to get a response from curl that I can record (error or success)?

我有一个简单的 bash 脚本,可以将文件上传到 FTP。我想知道如何从 curl 获得我可以记录的响应(错误或成功)?

eval curl -T "${xmlFolder}"/"${xmlFile}" "${mediaFTP}"

Thanks in advance

提前致谢

回答by Dimitre Radoulov

Given the command provided, this should suffice:

鉴于提供的命令,这应该足够了:

curl -T "$xmlFolder/$xmlFile" "$mediaFTP" || 
  printf '%s\n' $?

Or, if you want to discard the error message:

或者,如果您想丢弃错误消息:

curl -T "$xmlFolder/$xmlFile" "$mediaFTP" >/dev/null || 
  printf '%s\n' $?

回答by jman

The $? bash variable indicates success (val 0) / failure (val non 0) of the previous command. So you could do:

美元?bash 变量表示上一个命令的成功(val 0)/失败(val non 0)。所以你可以这样做:

eval curl -T "${xmlFolder}"/"${xmlFile}" "${mediaFTP}"
err=$?
if [ $err -ne 0 ]
then
    echo "Failed with error code $err"
    exit
fi