I'm launching some drush commands inside a shell script sh. How can i get if the command has terminated with no error? And, in case of error, how can i get the error and present it to the user executing the script?
-
Can you elaborate a bit more? drush pipes to stdout so if the script is executed in a terminal the user will see both the success and error messages. Do you have something else in mind? – skarist Jul 19 '14 at 01:49
-
No, The problem is this. Simple but hard for a Linux semi newbie like me. What do you exactly mean by 'pipe to stdout'? I'm executing a drush command inside an sh script that must be executed on terminal. I need to get when drush is not doing what is supposed to do in order to exit the script and it would be good to explain to The user what is going on – Sasha Grievus Jul 19 '14 at 06:56
-
1Ah... ok I see. Actually I was bit quick in my first comment. drush pipes (writes) success messages to stdout (the console) and error messages to stderr. By default both go to the console, but you can redirect them to differnt targets. Ok I write up a short answer for you. – skarist Jul 19 '14 at 09:09
1 Answers
As I said in my first comment if you put together a shell script with drush commands the user executing the script will see all the messages drush writes to the console. But if you want to write a more complex script, i.e. with checks for errors etc. then here is a short example that should get you started:
msg=$(drush blah 2>&1)
if [[ "$msg" =~ error* ]]
then
echo "we had an error!"
else
echo "success"
fi
msg=$(drush cc all 2>&1)
echo $msg
NOTE: this script assumes that you have a bash shell.
The construct $(command)
is used to pipe stdout to a variable. Since drush pipes error messages to stderr we also need suffix the drush command with 2>&1
which is a command to redirect stderr to stdout. The if
check is then basically a substring check for "error" in the $msg variable.
Here is a good resource for Bash programming:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
Hope this helps.
=========== EDIT =============
if you are in a Bourne shell you would use the following to redirect output to a variable:
msg=`drush cc all 2>&1`
You should be able to see which shell you are using by executing echo $SHELL

- 1,030
- 7
- 9
-
1Just to caution that my second drush command is a cache clear all. Maybe not something you want to run, although I would think it is harmless on a dev/staging machine. – skarist Jul 19 '14 at 10:00
-
Exactly what i was searching for. Thank you so much! Expecially for the useful information about shell scripting! – Sasha Grievus Jul 19 '14 at 15:07