Drush isn't bootstrapping drush script I can't get drush to bootstrap my script. I'm using version 5.9.

Below is my test script (test.drush):

#!/usr/bin/env drush

$site = drush_sitealias_get_record('@site1');
_drush_sitealias_print_record($site);

$result=drush_invoke('core-status');
drush_print($result['output']);

while ($arg = drush_shift()) {
  drush_print($arg);
}


And I have the aliases defined in ~/.drush/aliases.drushrc.php (see end of post).

When I run test.drush @site1 it doesn't bootstrap the script with site1. It has access to the alias because it can output the site record. Also it outputs @site1 as an argument. It should remove @site1 from the argument list and bootstrap it but it doesn't.

Below is the output:

$aliases['site1.local'] = array (
  'root' =&gt; '/Users/mark/Documents/drupal6',
  'uri' =&gt; 'http://site1.local',
  '#file' =&gt; '/Users/mark/.drush/aliases.drushrc.php',
);

 PHP configuration     :  /private/etc/php.ini                   
 Drush version         :  5.9                                    
 Drush configuration   :                                         
 Drush alias files     :  /Users/mark/.drush/aliases.drushrc.php 

@site1


BTW running drush @site1 status on the command line works fine

Also, is there any way to change the current site in a script. When I use:

drush_invoke('use', '@site2')
$result=drush_invoke('core-status');
drush_print($result['output']);


it doesn't apply to the subsequent drush commands but it does the next time you run the script.

aliases.drushrc.php:
    

$aliases['site1'] = array(
  'root' =&gt; '/Users/mark/Documents/drupal6',
  'uri' =&gt; 'http://site1.local',
  'db-url' =&gt; 'mysql://user:password@127.0.0.1/site1'
);

$aliases['site2']=array(
  'root'=&gt;'/Users/mark/Documents/drupal7',
  'uri'=&gt;'http://site2.local',
  'db-url'=&gt;'mysql://user:password@127.0.0.1/site2'
);
Drush shell scripts such as the one you show above do not automatically bootstrap to any argument passed to them.  (EDIT: This is due to a bug.)  You have a few options.

1: Force the bootstrap:

$site = drush_shift();
drush_bootstrap_max_to_sitealias($site);
drush_invoke('core-status');


2: Do all of your work in a subshell:

$values = drush_invoke_process($site, 'core-status');

Those two should get you started.  Note that option number two will re-bootstrap Drupal in an isolated subshell every time you call drush_invoke_process.  drush_invoke always works on the currently-bootstrapped site, and you can only bootstrap once.  Therefore, that drush_invoke('use', ...) trick won't work.  'use' is only for use in the shell, not in scripts.

There's another error above.  This code isn't working as you expect it is:

$result=drush_invoke('core-status');
drush_print($result['output']);


What's really happening here is that drush_invoke is printing the output of drush status to stdout, and drush_print is printing nothing.  You'll get $result['output'] if you use drush_invoke_process.

In my somewhat biased opinion, the Drush chapter of the Definitive Guide To Drupal 7 covers these topics pretty well.  It is available for download as a free sample chapter at http://definitivedrupal.org/book/26-drush