首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shell-Script:获取cURL错误,如果用户出错

Shell-Script:获取cURL错误,如果用户出错
EN

Stack Overflow用户
提问于 2018-11-20 12:38:23
回答 1查看 255关注 0票数 1

我的shell脚本如下所示:

代码语言:javascript
复制
#!/bin/bash
curl -f -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt
res=$?
if test "$res" != 0; then
   echo "the curl command failed with: $res"
else
   echo "Success $res"
fi

我用这个来填充文件..。

现在我的问题是,我不能得到所有的错误。举个例子,如果我输入了一个错误的URL (正确的URL是http://192.168.0.100:5005/home/test.txt),上传失败,但退出代码仍然是0。

下面是带有错误URL的输出:

代码语言:javascript
复制
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
Success 0

我怎样才能得到这些错误呢?

我还对cURL和ftp目标做了同样的尝试,在那里它可以处理所有的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-20 13:30:25

-w 'http_code %{http_code}'将使curl在输出的末尾添加HTTP代码。

也许您可以选择这个新版本,我只对它进行了部分测试:

代码语言:javascript
复制
#!/bin/bash
serverResponse=$(curl -f -w 'http_code %{http_code}' -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt)
res=$?
if test "$res" != 0; then
   printf "the curl command failed with: %s\n" "${res}"
else
   http_code="${serverResponse##*http_code }"
   if [[ ! -z "${http_code}" && "${http_code}" -ne 200 ]] ; then
     printf "Server sent back this http status: %s\n" "${http_code}"
   else
     printf "Success %s\n" "${res}"
   fi
fi
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53393174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档