How to stop the script drush if previous command failed?
Checkout the run_cmd function in this file: base_functions, which wraps any command and terminates the script if it fails.

You should also be able to do something like:

drush cc all || exit;


where exit will only run if the previous command fails, and you could replace exit with a call to a function that outputs a more meaningful error message. You may also add a number between 1 and 255 after exit to have your script emit an exit status that can be used further using $? in any other script calling this one.