Drupal shell: quickly evaluating PHP statements in a Drupal context
I often find myself needing to variable_set something temporarily, just to try things out. The drush module provides a command-line interface that solves this problem with a little more hacking. To minimize the effect on my source tree, I’ve unpacked it into the sites directory for my local installation and enabled it in my test database. After I enabled the main drush module and the related modules, I tweaked drush_tools to include an insecure-but-useful eval command. Here it is:
--- sites/local.example.com/modules/drush/drush_tools.module.orig 2008-08-05 17:18:48.000000000 -0400
+++ sites/local.example.com/modules/drush/drush_tools.module 2008-08-05 17:18:55.000000000 -0400
@@ -46,6 +46,10 @@
'callback' => 'drush_tools_sync',
'description' => 'Rsync the Drupal tree to/from another server using ssh'
);
+ $items['eval'] = array(
+ 'callback' => 'drush_tools_eval',
+ 'description' => 'Evaluate a command',
+ );
return $items;
}
@@ -156,3 +160,6 @@
}
}
+function drush_tools_eval($command) {
+ eval($command);
+}
I also added an alias to my ~/.bashrc along the lines of:
alias drush='php ~/drupal/sites/local.example.com/modules/drush/drush.php -r ~/drupal -l http://local.example.com'
where ~/drupal is my multisite Drupal directory root.
After I loaded the alias with “source ~/.bashrc”, I can now execute PHP statements in my Drupal context with commands like:
drush eval "variable_set('hello', 'world');"
Good stuff!
Save to - del.icio.us - Digg it - reddit - StumbleUpon - Twitter






nothing insecure about it. if you can run a drush command you have command line access and thus can do much nastiness. please submit a patch for drush toolbox. this is worthy.
Patch posted at http://drupal.org/node/292732
Thanks for posting that! =) It took me a while to get work permission to contribute to Drupal, but I now have it – hooray!
[...] written about using drush to evaluate PHP statements in the Drupal context using the command line before, and it turns out that Drush is also quite useful for running Simpletest scripts. Drush [...]