Answers:
您可以在Drush Shell脚本中转换脚本。
drush shell脚本是任何设置了“执行”位(即,通过
chmod +x myscript.drush
)并以特定行开头的Unix shell脚本文件:#!/usr/bin/env drush
要么
#!/full/path/to/drush
由于以下原因,Drush脚本比Bash脚本更好:
以下是您可以在helloword.script上找到的示例。
#!/usr/bin/env drush
//
// This example demonstrates how to write a drush
// "shebang" script. These scripts start with the
// line "#!/usr/bin/env drush" or "#!/full/path/to/drush".
//
// See `drush topic docs-scripts` for more information.
//
drush_print("Hello world!");
drush_print();
drush_print("The arguments to this command were:");
//
// If called with --everything, use drush_get_arguments
// to print the commandline arguments. Note that this
// call will include 'php-script' (the drush command)
// and the path to this script.
//
if (drush_get_option('everything')) {
drush_print(" " . implode("\n ", drush_get_arguments()));
}
//
// If --everything is not included, then use
// drush_shift to pull off the arguments one at
// a time. drush_shift only returns the user
// commandline arguments, and does not include
// the drush command or the path to this script.
//
else {
while ($arg = drush_shift()) {
drush_print(' ' . $arg);
}
}
drush_print();
您可以使脚本可执行,因此可以使用脚本名称<script file> <parameters>
where <script name>
和<parameters>
传递给脚本的参数来执行脚本。如果脚本不是可执行的,请使用调用它drush <script name> <parameters>
。
使用drush php-eval
,您可以运行脚本,而不必先将其保存到文件中:
drush php-eval '
$uid = 1234;
$query = db_query("SELECT cid FROM {comments} WHERE uid = %d", $uid);
while($cid = db_result($query)) {
comment_delete($cid);
}
'
这使用嵌套的引号,因此为了避免混乱,我建议"
在PHP代码中仅使用双引号。
我们可以用来drush php-script script_name
在Drush中执行php文件。
对于与Drush有关的执行php文件的帮助,Type Drush php-script --help
将列出您的命令
注意:我已经将PHP scirpt放置在Drupal的根文件夹中
在命令行上的任何地方运行:
$ drush --root=/path/to/drupal-installation --uri=youdomain.com scr /path/to/your/script.php
如果您已经在/ path / to / drupal-installation中,请运行:
$ drush --uri=youdomain.com scr /path/to/your/script.php
如果您在/path/to/drupal-installation/sites/youdomain.com上比在运行时更领先:
$ drush scr /path/to/your/script.php
您的script.php文件:
<?php
// Not always needed but sometimes you might have to first login as an administrator.
$admin_uid = 1;
$form_state = array('uid' => $admin_uid);
user_login_submit(array(), $form_state);
// Now the logged in user global $user object become available.
global $user;
print_r($user);
// Do whatever you want here.