"gsutil cp" 的结果总是在 stderr,而不是 stdout

result from "gsutil cp" is always in stderr, not stdout

正如标题,"gsutil cp" 的结果不会重定向到 stdout,它总是重定向到 stderr。

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

它的行为是错误的,因为如果上面的命令是正确的,它的输出应该是 return in out.log,而不是 error.log.

我该如何解决?

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

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

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

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

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

($?) Expands to the exit status of the most recently executed foreground pipeline.

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

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