shellscripts.html 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <h1>The Drush Shell Scripts</h1>
  2. <p>
  3. A drush shell script is any Unix shell script file that has
  4. its "execute" bit set (i.e., via `chmod +x myscript.drush`)
  5. and that begins with a specific line:
  6. <pre>
  7. #!/usr/bin/env drush
  8. </pre>
  9. - or -
  10. <pre>
  11. #!/full/path/to/drush
  12. </pre><p>
  13. The former is the usual form, and is more convenient in that
  14. it will allow you to run the script regardless of where drush
  15. has been installed on your system, as long as it appears in
  16. your PATH. The later form allows you to specify the drush
  17. command add options to use, as in:
  18. <pre>
  19. #!/full/path/to/drush php-script --some-option
  20. </pre><p>
  21. Adding specific options is important only in certain cases,
  22. described later; it is usually not necessary.
  23. <p>
  24. Drush scripts do not need to be named "*.drush" or "*.script";
  25. they can be named anything at all. To run them, make sure they
  26. are executable (`chmod +x helloworld.script`) and then run them
  27. from the shell like any other script.
  28. <p>
  29. There are two big advantages to drush scripts over bash scripts:
  30. <ul>
  31. <li>They are written in php
  32. <li>drush can bootstrap your Drupal site before
  33. running your script.
  34. </ul><p>
  35. To bootstrap a Drupal site, provide an alias to the site to
  36. bootstrap as the first commandline argument.
  37. <p>
  38. For example:
  39. <pre>
  40. $ helloworld.script @dev a b c
  41. </pre><p>
  42. If the first argument is a valid site alias, drush will remove
  43. it from the arument list and bootstrap that site, then run
  44. your script. The script itself will not see @dev on its
  45. argument list. If you do not want drush to remove the first
  46. site alias from your scripts argument list (e.g. if your script
  47. wishes to syncronise two sites, specified by the first two
  48. arguments, and does not want to bootstrap either of those
  49. two sites), then fully specify the drush command (php-script)
  50. and options to use, as shown above. By default, if the drush
  51. command is not specified, drush will provide the following default
  52. line:
  53. <pre>
  54. #!/full/path/to/drush php-script --bootstrap-to-first-arg
  55. </pre><p>
  56. It is the option --bootstrap-to-first-arg that causes drush to
  57. pull off the first argument and bootstrap it. The way to get rid
  58. of that option is to specify the php-script line to run, and leave
  59. it off, like so:
  60. <pre>
  61. #!/full/path/to/drush php-script
  62. </pre><p>
  63. Note that 'php-script' is the only built-in drush command that
  64. makes sense to put on the "shebang" ("#!" is pronounced "shebang")
  65. line. However, if you wanted to, you could implement your own
  66. custom version of php-script (e.g. to preprocess the script input,
  67. perhaps), and specify that command on the shebang line.
  68. <p>
  69. Drush scripts can access their arguments via the drush_shift()
  70. function:
  71. <pre>
  72. while ($arg = drush_shift()) {
  73. drush_print($arg);
  74. }
  75. </pre><p>
  76. Options are available via drush_get_option('option-name').
  77. <p>
  78. See the example drush script in `drush topic docs-examplescript`,
  79. and the list of drush error codes in `drush topic docs-errorcodes`.