As the title, the results from "gsutil cp" doesn't redirect to stdout, it always redirect to stderr.
As an example : gsutil cp existed_file.txt . > >(tee -a out.log) 2> >(tee -a error.log >&2) .
In above command, out.log is empty, and error.log has a successful copy result.
Its behavior is wrong, because if above command is correct, its output should return in out.log, not in error.log.
How can i fix it ?
The above command is functioning correctly; the output is actually going to stderr. Gsutil outputs progress information (including progress messages produced by the UI thread, as shown for copy-based commands like cp and mv) to stderr by design (although, due to the nature of a large project and having several people work on different parts, there do exist some places where gsutil violates this principal and prints those things to stdout instead). In contrast, commands that fetch information (like ls) print their output to stdout, as that output isn't necessarily progress-related, but is the data the user wanted to fetch.
Regardless, if you want to know whether the copy operation succeeded, you should be checking whether the command's exit code was equal to 0:
$ gsutil cp srcfile gs://mybucket
Copying file://srcfile [Content-Type=application/json]...
/ [1 files][ 253.0 B/ 253.0 B]
Operation completed over 1 objects/253.0 B.
$ echo $?
0
$ gsutil cp no-file-with-this-name gs://mybucket
CommandException: No URLs matched: no-file-with-this-name
$ echo $?
1
Most helpful comment
The above command is functioning correctly; the output is actually going to stderr. Gsutil outputs progress information (including progress messages produced by the UI thread, as shown for copy-based commands like
cpandmv) to stderr by design (although, due to the nature of a large project and having several people work on different parts, there do exist some places where gsutil violates this principal and prints those things to stdout instead). In contrast, commands that fetch information (likels) print their output to stdout, as that output isn't necessarily progress-related, but is the data the user wanted to fetch.Regardless, if you want to know whether the copy operation succeeded, you should be checking whether the command's exit code was equal to 0: