shell - Shell-Script:如果用户错误,则获取 cURL 错误
问题描述
我的 shell 脚本如下所示:
#!/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 的输出:
<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 目标尝试了同样的事情,它适用于所有错误。
解决方案
-w 'http_code %{http_code}'
将curl
在输出末尾添加 HTTP 状态代码。
也许你可以选择这个新版本,我只对它进行了部分测试:
#!/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
推荐阅读
- netcdf - 使用具有单个时间片的掩码来掩盖具有多个时间步长的另一个变量?
- rxjs - 如何缓冲 observable
何时计数或最大时间避免发出空数组? - swift - Swift:使用计算的结构属性实现 Comparable
- asp.net - 带有 VB 的 ASP.Net 似乎不起作用:如何使用 '@'
- arrays - 如何检查一个数组是否有 2 个相同的值?- 爪哇
- azure-cosmosdb - 具有复杂结构的 Azure 搜索和 Cosmos DB
- maven - @Unroll 不适用于 Spock 1.2 和 Groovy 2.5 的 maven 测试
- javascript - 历史推送不更新新数据
- javascript - 如何在framework7中加载多个自动完成?
- go - 在 Go 中生成构建时间戳