Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle gsutil ls and rm command errors if no files present

Tags:

gsutil

I am running the following command to remove files from a gcs bucket prior to loading new files there.

gsutil -m rm gs://mybucket/subbucket/*

If there are no files in the bucket, it throws the "CommandException: One or more URLs matched no objects".

I would like for it to delete the files if exists without throwing the error.

There is same error with gsutil ls gs://mybucket/subbucket/*

How can I rewrite this without having to handle the exception explicitly? Or, how to best handle these exceptions in batch script?

like image 901
user1311888 Avatar asked Mar 21 '17 16:03

user1311888


2 Answers

You might not want to ignore all errors as it might indicate something different that file not found. With the following script you'll ignore only the 'One or more URLs matched not objects' but will inform you of a different error. And if there is no error it will just delete the file:

gsutil -m rm gs://mybucket/subbucket/* 2> temp
if [ $? == 1 ]; then
    grep 'One or more URLs matched no objects' temp
    if [ $? == 0 ]; then
        echo "no such file"
    else
        echo temp
    fi
fi
rm temp

This will pipe stderr to a temp file and will check the message to decide whether to ignore it or show it.

And it also works for single file deletions. I hope it helps.

Refs:

How to grep standard error stream
Bash Reference Manual - Redirections

like image 101
Javier PR Avatar answered Sep 18 '22 07:09

Javier PR


Try this:

gsutil -m rm gs://mybucket/foo/* 2> /dev/null || true

Or:

gsutil -m ls gs://mybucket/foo/* 2> /dev/null || true

This has the effect of suppressing stderr (it's directed to /dev/null), and returning a success error code even on failure.

like image 20
Elliott Brossard Avatar answered Sep 21 '22 07:09

Elliott Brossard