【问题标题】:result from "gsutil cp" is always in stderr, not stdout“gsutil cp”的结果总是在标准错误中,而不是标准输出
【发布时间】:2018-10-27 11:02:54
【问题描述】:

作为标题,“gsutil cp”的结果不会重定向到标准输出,它总是重定向到标准错误。

例如:gsutil cp existed_file.txt . > >(tee -a out.log) 2> >(tee -a error.log >&2)。 上述命令中out.log为空,error.log复制成功。

它的行为是错误的,因为如果上面的命令是正确的,它的输出应该在 out.log 中返回,而不是在 error.log 中。

我该如何解决?

【问题讨论】:

    标签: google-cloud-platform google-cloud-storage google-cloud-functions gsutil


    【解决方案1】:

    gsutil 按照设计将进度/状态信息输出到 stderr,以将进度/状态与输出到 stdout 的数据分开。例如,如果您运行:

    gsutil ls gs://my-bucket > listing 2>&1 log
    

    您将在文件“listing”中看到存储桶列表,并在文件“log”中看到任何错误或其他状态信息。

    虽然 gsutil cp 输出不生成数据(仅生成状态),但我们对所有命令使用上述 stderr/stdout 划分,因此 gsutil 在这方面对所有命令的行为一致。

    【讨论】:

      【解决方案2】:

      如果想知道 gsutil 是否失败,可以使用特殊参数 $?。来自文档:

      ($?) 扩展为最近执行的退出状态 前台管道。

      gsutil cp existed_file.txt .
      echo $? # Will display 0 if OK and something else if not OK
      

      注意:使用 powershell,它会显示 True 或 False。

      【讨论】:

        猜你喜欢
        • 2020-01-21
        • 2014-10-09
        • 2012-09-17
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多