2012-04-02 29 views

回答

44

这是我在我的构建脚本的一个用途:

curl "${UPLOAD_URL}" \ 
    --progress-bar \ 
    --verbose \ 
    -F build="${BUILD}" \ 
    -F version="${VERSION}" \ 
    -F ipa="@${IPA};type=application/octet-stream" \ 
    -F assets="@-;type=text/xml" \ 
    -F replace="${REPLACE}" \ 
    -A "${CURL_FAKE_USER_AGENT}" \ 
    <<< "${ASSETS}" \ 
    | tee -a "${LOG_FILE}" ; test ${PIPESTATUS[0]} -eq 0 

-F-A选项可能不会有兴趣的你,但有用的部分是:

curl "${UPLOAD_URL}" --progress-bar 

它告诉curl在上传期间显示进度条(而不是默认的“进度表”),并且:

| tee -a "${LOG_FILE}" ; test ${PIPESTATUS[0]} -eq 0 

它将命令的输出附加到日志文件,并将其回显到stdouttest ${PIPESTATUS[0]} -eq 0部分使得该行(在bash脚本中)的退出状态与curl命令返回的退出代码相同,而不是tee命令的退出状态(因为tee实际上是最后一个命令在这一行执行,而不是curl)。


man curl

PROGRESS METER 
     curl normally displays a progress meter during operations, indicating the amount of transferred data, transfer speeds and estimated time left, etc. 

     curl displays this data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the terminal, it disables the progress meter as otherwise it would mess up the 
     output mixing progress meter and response data. 

     If you want a progress meter for HTTP POST or PUT requests, you need to redirect the response output to a file, using shell redirect (>), -o [file] or similar. 

     It is not the same case for FTP upload as that operation does not spit out any response data to the terminal. 

     If you prefer a progress "bar" instead of the regular meter, -# is your friend. 

OPTIONS 
     -#, --progress-bar 
       Make curl display progress as a simple progress bar instead of the standard, more informational, meter. 
+1

注意!需要“tee”命令才能获取有关下载进度的信息。如果你不想使用'tee',那么只需使用'grep -v'^ uniqueStringNeverHappens $''命令。更多信息:http://stackoverflow.com/a/17178410/751932 – Speakus 2014-09-16 07:44:24

+1

或者:'>/dev/null' – asymmetric 2016-05-18 13:07:15

+0

这似乎不适用于我:( 编辑:没关系,问题是,我想时间需要多长时间,所以我使用'time curl --progress-bar --verbose ...'来防止显示进度条。无需工作即可运行它。 – 2016-07-26 03:56:00

8

我曾与接受答案的命令重定向麻烦,发现-o选项将放在一个文件,该文件允许进度条显示的响应输出。

curl --progress-bar \ 
    -o upload.txt \ 
    -H ${SOME_AUTH_HEADER} \ 
    -T ${SOME_LARGE_FILE} \ 
    "${UPLOAD_URL}" 

只是另一种获得所需结果的选项。


注意:在这条线从该名男子页强调重要的是要了解为什么单单指定--progress-bar当进度条没有显示的根本原因。

 If you want a progress meter for HTTP POST or PUT requests, 
     you need to redirect the response output to a file, 
     using shell redirect (>), -o [file] or similar. 
3

这里的所有其他答案都有问题,它们需要您将curl的原始输出写入(日志)文件。但是,这可能不是所有情况下都需要的。

问题是curl隐藏了进度条/计时器,当预计服务器响应时,它会写入到sdout中。 所以基本上可以将输出重定向到一个文件再次显示栏。但是,我们并不希望出现这种情况,所以/dev/nulltee可以帮助我们在这里:

curl --progress-bar -T "${SOME_LARGE_FILE}" "${UPLOAD_URL}" | tee /dev/null 

卷曲的输出被传递到tee其输出都写入控制台(这是我们希望看到的进度条,也是服务器响应)和一个文件(这是我们不需要的,但因为我们使用/dev/null这并不重要)。

请注意,卷发器当然没有隐藏进度条的乐趣。在这种情况下,您可能无法始终看到服务器结果,或者只能显示几秒钟,但如果您不关心此问题,则该解决方案非常好。