简体   繁体   English

使用 curl 上传文件并打印 curl Z78E6221F6393D1356Z81DB3 时获取 http 代码

[英]Get http code when uploading file using curl and printing curl output

I'm writing a bash script where I want to upload a file using curl.我正在编写一个 bash 脚本,我想在其中使用 curl 上传文件。 I want to print in a log file the output from stdout and stderr (to see if the upload is being completed).我想在日志文件中打印来自 stdout 和 stderr 的 output(查看上传是否完成)。 This is what works so far:到目前为止,这是有效的:

curl -T home/user/mydir/filename <my url>/.../filename >> home/user/mydir/script.log 2>&1

Now, I also want to get the returned http code so I can use it somewhere else in my script.现在,我还想获取返回的 http 代码,以便可以在脚本的其他位置使用它。 The following works as well:以下也有效:

CODE=$(curl -w '%{http_code}' -T home/user/mydir/filename <my url>/.../filename)

How can I combine these two commands?如何结合这两个命令? I want to print the stdout and stderr in the log file (append, to be precise) and also store the returned http code in a variable for later use.我想在日志文件中打印标准输出和标准错误(准确地说是附加),并将返回的 http 代码存储在一个变量中以供以后使用。

Thank you.谢谢你。

Can you do something like this (the first < line is the return code) (from the verbose option).你能做这样的事情吗(第一行是返回码)(来自详细选项)。 The -o can be - (stdout) OR any other file you want?? -o 可以是 - (stdout) 或您想要的任何其他文件?

# curl -v -o /dev/null http://www.google.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 142.250.76.100...
* TCP_NODELAY set
* Connected to www.google.com (142.250.76.100) port 80 (#0)
> GET / HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.64.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 10 Mar 2021 02:37:46 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< Server: gws
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: 1P_JAR=2021-03-10-02; expires=Fri, 09-Apr-2021 02:37:46 GMT; path=/; domain=.google.com; Secure
< Set-Cookie: NID=211=l3bDHkrWaIiQN03V4QI2bXrwHu0ZTMxoQgB1DDhIE2EfomQO6zrAPbu-5h6L6Ru60kQh0vtAog3iykbLvvtv28r8aVYiyMapXQWTexMArSIqRVQrcwDL4PnaoivdpE1_aL0rC6gohDpxFT-yQmk3jkE7EKtdSL_Dh7Z7nsnoqBM; expires=Thu, 09-Sep-2021 02:37:46 GMT; path=/; domain=.google.com; HttpOnly
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< 
{ [686 bytes data]
100 13716    0 13716    0     0  37889      0 --:--:-- --:--:-- --:--:-- 37889
* Connection #0 to host www.google.com left intact
* Closing connection 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM