bash - 如何从 shell 上的 cURL 检索错误代码
问题描述
我知道发布了一个类似的问题,但我无法让它在我的机器上运行。
我尝试了上述问题的第一个答案,即response=$(curl --write-out %{http_code} --silent --output /dev/null servername)
当echo $response
我得到000
[不确定这是否是所需的输出] 时。
但是,当尝试使用我的 cURL 命令执行此操作时,我没有得到任何输出。
这是我的命令:
curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
我用它
x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)
但是当我尝试时,echo $x
我得到的只是换行符......
我知道 cURL 失败了,因为当我运行相同的命令时,没有--silent
,我得到curl: (7) Couldn't connect to server
This Q is tagged with bothsh, bash
因为我在两者上都尝试过,结果相同
解决方案
我发现这个选项有什么帮助(但我仍然不知道如何将它分配给一个变量,这应该比这更容易......):
--stderr <file>
Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.
If this option is used several times, the last one will be used.
当我这样使用它时:
curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
我可以看到该文件中的错误(即curl: (7) Couldn't connect to server
)。
我曾经--silent
抑制所有输出,-S
取消抑制错误,并--stderr <file>
重定向它们
推荐阅读
- java - 在 Gauge 中启动 Eyes Applitools 时出现 NoSuchFieldError EXCLUDE_EMPTY
- aws-lambda - 将 base64 字符串作为应用程序/pdf 处理 API 网关上的单个端点
- docker - 如何将 Docker 日志复制到另一个目录
- error-handling - 有什么方法可以快速查看所有现有的 SSIS 包事件处理程序?
- html - 为什么我的 Bootstrap 示例页面中的轮播不起作用?
- django - 显示内联时更改 Django Admin 的“添加另一个 SomeObject”按钮上的文本
- string - 为什么使用 -n 测试非空字符串总是正确的?
- java - SimpleDateFormat 对象更改时区而不添加/减去正确的小时数
- reactjs - React Jest:如何模拟返回数据的异步 API 调用?
- python - ImportError:无法从“树”导入名称“_assert_shallow_structure”