首页 > 解决方案 > 如何从 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因为我在两者上都尝试过,结果相同

标签: bashshellcurl

解决方案


我发现这个选项有什么帮助(但我仍然不知道如何将它分配给一个变量,这应该比这更容易......):

   --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>重定向它们


推荐阅读