Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 882
voxter Avatar asked May 17 '18 08:05

voxter


People also ask

What does gsutil CP do?

The gsutil cp command attempts to name objects in ways that are consistent with the Linux cp command. This means that names are constructed depending on whether you're performing a recursive directory copy or copying individually-named objects, or whether you're copying to an existing or non-existent directory.

What is gsutil option?

gsutil is a Python application that lets you access Cloud Storage from the command line. You can use gsutil to do a wide range of bucket and object management tasks, including: Creating and deleting buckets. Uploading, downloading, and deleting objects.

What is the difference between gcloud and gsutil?

"gcloud" can create and manage Google Cloud resources while "gsutil" cannot do so. "gsutil" can manipulate buckets, bucket's objects and bucket ACLs on GCS(Google Cloud Storage) while "gcloud" cannot do so.

Does gsutil CP create folder?

You cannot create a folder with gsutil on GCS. But you can copy an existing folder with gsutil to GCS.


2 Answers

gsutil outputs progress / status info to stderr by design, to keep progress/status separated from data that's output to stdout. For example, if you run:

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

you will see the bucket listing in the file "listing", and any errors or other status info in the file "log".

While gsutil cp output doesn't generate data (only status), we use the above stderr/stdout division for all commands, so gsutil behaves consistently in this regard for all commands.

like image 147
Mike Schwartz Avatar answered Oct 04 '22 21:10

Mike Schwartz


If you want to know if gsutil failed, you can use the special parameter $?. From the documentation:

($?) 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

Note: with powershell, it will display True or False.

like image 38
lordofmax Avatar answered Oct 04 '22 20:10

lordofmax