Issue
I'm calling a shell script through python.
Inside the script I'm using gsutil rm
and gsutil cp
commands.
But whenever any of these 2 commands run on a gcp path, which is not present, it gives error.
For eg: When i try:
gsutil rm -r gs://some-bucket/somepath-not-present/
I get an error :
CommandException: 1 files/objects could not be removed.
And in the end the return code of script is 1. Even though my code would have executed fine. I wanted the script to not give error when path is not found.
Is there any way to bypass this. I have tried the ||
from linux, but it doesnt seem to be working.
Solution
If you use the gsutil
inside another script that may be affected by the exceptions printed by gsutil
, you can simply try redirecting the stderr into /dev/null like this:
gsutil rm -r gs://some-bucket/somepath-not-present/ 2> /dev/null
However, beware that you will start ignoring any errors generated by the command at this point. If you echo $?
, you will still get the error code of 1
which can tell you either the file didn't exist or other exception happened.
EDIT:
If you want to exit from the script with an exit code 0, you could do
gsutil rm -r gs://some-bucket/somepath-not-present/ || exit 0
Or if you just want the command to return 0, you could use
gsutil rm -r gs://some-bucket/somepath-not-present/ || true
Answered By - petomalina Answer Checked By - Timothy Miller (WPSolving Admin)