2012-11-25 117 views
0

我遇到了一個問題,即需要在創建的目錄中自動運行命令(確切地說是一個drush配置文件)。我無法在腳本本身中找到很多建議,將它作爲參數運行,我的上級指示的邏輯似乎表明這是最好的方法。從目錄運行bash命令

下面是在bash腳本的作用:

  • 它創建的目錄結構和一個Drupal一些Apache2的安裝文件
  • 它創建了一個基本的Drush配置文件,然後使用該實用程序
  • 通過在包含此配置文件的目錄中運行別名「standardprofile」來自動安裝Drupal和模塊

我用來運行腳本的實際命令牛逼從巴什(以下全腳本)是

cd /var/www/$DOMAINNAME-dev.$URL 
eval "standardprofile" 

以上線不運行腳本(或任何過去的第一個起跑線上,然後再bash腳本恢復)是否有更好的方法來指示慶典腳本從指定的目錄運行命令,然後在前臺完成運行其他任何實用程序時恢復運行?

#!/bin/bash 
# Creates the proper staging and development environment for a site 

# Init 
URL=example.com   #URL used in creating directories 
OMIT_STAGING="n"  #Set to true if omitting staging 
DOMAINNAME="" 

### 
# FUNCTIONS 
### 
function generate_empty_dirs { 
    mkdir /var/www/$1-staging.$URL 
    mkdir /var/www/$1-dev.$URL 
    mkdir /var/www/$1-staging.$URL/logs 
    mkdir /var/www/$1-staging.$URL/public_html 
    mkdir /var/www/$1-dev.$URL/logs 
    mkdir /var/www/$1-dev.$URL/public_html 
} 

function generate_drupal_staging_apacheconf { 
    echo "<VirtualHost *:80> 
      ServerAdmin [email protected] 
      ServerName $1-staging.$URL 

      DocumentRoot /var/www/$1-staging.$URL/public_html 
      <Directory /var/www/$1-staging.$URL/public_html> 
       Options -MultiViews +ExecCGI 
       Order allow,deny 
       Allow from all 

       RewriteEngine On 
       RewriteBase/
       RewriteCond %{REQUEST_FILENAME} !-f 
       RewriteCond %{REQUEST_FILENAME} !-d 
       RewriteRule ^(.*)$ index.php?q=\$1 [L,QSA] 

      </Directory> 

      LogLevel warn 
      ErrorLog /var/www/$1-staging.$URL/logs/error.log 
      CustomLog /var/www/$1-staging.$URL/logs/access.log combined 
      # enable PHP error logging 
      php_flag log_errors on 
      php_value error_log /var/www/$1-staging.$URL/logs/php_errors.log 
      # Possible LogLevel values include: debug, info, notice, warn, error, crit, 
      # alert, emerg. 

    </VirtualHost>" > /etc/apache2/sites-available/$1-staging.$URL 
} 

function generate_drupal_dev_apacheconf { 
    echo "<VirtualHost *:80> 
      ServerAdmin [email protected] 
      ServerName $1-dev.$URL 

      DocumentRoot /var/www/$1-dev.$URL/public_html 
      <Directory /var/www/$1-dev.$URL/public_html> 
       Options -MultiViews +ExecCGI 
       Order allow,deny 
       Allow from all 

       RewriteEngine On 
       RewriteBase/
       RewriteCond %{REQUEST_FILENAME} !-f 
       RewriteCond %{REQUEST_FILENAME} !-d 
       RewriteRule ^(.*)$ index.php?q=\$1 [L,QSA] 

      </Directory> 

      LogLevel warn 
      ErrorLog /var/www/$1-dev.$URL/logs/error.log 
      CustomLog /var/www/$1-dev.$URL/logs/access.log combined 
      # enable PHP error logging 
      php_flag log_errors on 
      php_value error_log /var/www/$1-dev.$URL/logs/php_errors.log 
      # Possible LogLevel values include: debug, info, notice, warn, error, crit, 
      # alert, emerg. 

    </VirtualHost>" > /etc/apache2/sites-available/$1-dev.$URL 
} 

function generate_wordpress_staging_apacheconf { 
    echo "<VirtualHost *:80> 
      ServerAdmin [email protected] 
      ServerName $1-staging.$URL 

      DocumentRoot /var/www/$1-staging.$URL/public_html 
      <Directory /var/www/$1-staging.$URL/public_html> 
       Options -MultiViews +ExecCGI 
       Order allow,deny 
       Allow from all 

       RewriteEngine On 
       RewriteBase/
       RewriteRule ^index\.php$ - [L] 
       RewriteCond %{REQUEST_FILENAME} !-f 
       RewriteCond %{REQUEST_FILENAME} !-d 
       RewriteRule . /index.php [L] 

      </Directory> 

      LogLevel warn 
      ErrorLog /var/www/$1-staging.$URL/logs/error.log 
      CustomLog /var/www/$1-staging.$URL/logs/access.log combined 
      # enable PHP error logging 
      php_flag log_errors on 
      php_value error_log /var/www/$1-staging.$URL/logs/php_errors.log 
      # Possible LogLevel values include: debug, info, notice, warn, error, crit, 
      # alert, emerg. 

    </VirtualHost>" > /etc/apache2/sites-available/$1-staging.$URL 
} 

function generate_wordpress_dev_apacheconf { 
    echo "<VirtualHost *:80> 
      ServerAdmin [email protected] 
      ServerName $1-dev.$URL 

      DocumentRoot /var/www/$1-dev.$URL/public_html 
      <Directory /var/www/$1-dev.$URL/public_html> 
       Options -MultiViews +ExecCGI 
       Order allow,deny 
       Allow from all 

       RewriteEngine On 
       RewriteBase/
       RewriteRule ^index\.php$ - [L] 
       RewriteCond %{REQUEST_FILENAME} !-f 
       RewriteCond %{REQUEST_FILENAME} !-d 
       RewriteRule . /index.php [L] 

      </Directory> 

      LogLevel warn 
      ErrorLog /var/www/$1-dev.$URL/logs/error.log 
      CustomLog /var/www/$1-dev.$URL/logs/access.log combined 
      # enable PHP error logging 
      php_flag log_errors on 
      php_value error_log /var/www/$1-dev.$URL/logs/php_errors.log 
      # Possible LogLevel values include: debug, info, notice, warn, error, crit, 
      # alert, emerg. 

    </VirtualHost>" > /etc/apache2/sites-available/$1-dev.$URL 
} 

function generate_drupal_drush_dev_profile { 
    # Dev 
    echo " <?php 
    \$db_engine = 'mysql'; 
    \$db_name = '$1_db'; 
    \$db_user = '$1_user'; 
    \$db_pw = '1password'; 
    \$db_su = 'root'; 
    \$db_su_pw = ''; 
    \$site_name = '$1'; 
    \$account_name = 'admin'; 
    \$account_pw = 'example'; 
    \$account_mail = '[email protected]'; 
    \$site_mail = \$account_mail;" > /var/www/$1-dev.$URL/installsettings.php 
} 

function generate_drupal_drush_staging_profile { 
    #Staging 
    echo " <?php 
    \$db_engine = 'mysql'; 
    \$db_name = '$1_db'; 
    \$db_user = '$1_user'; 
    \$db_pw = '1password'; 
    \$db_su = 'root'; 
    \$db_su_pw = ''; 
    \$site_name = '$1'; 
    \$account_name = 'admin'; 
    \$account_pw = 'example'; 
    \$account_mail = '[email protected]'; 
    \$site_mail = \$account_mail;" > /var/www/$1-staging.$URL/installsettings.php 
} 

function alter_group_owner { 
    chown -R www-data /var/www/$1-staging.$URL 
    chgrp -R www-data /var/www/$1-staging.$URL 
    chown -R www-data /var/www/$1-dev.$URL 
    chgrp -R www-data /var/www/$1-dev.$URL 
    chown -R www-data /etc/apache2/sites-available/$1-staging.$URL 
    chgrp -R www-data /etc/apache2/sites-available/$1-staging.$URL 
    chown -R www-data /etc/apache2/sites-available/$1-dev.$URL 
    chgrp -R www-data /etc/apache2/sites-available/$1-dev.$URL 
} 

### 
# BEGIN MAIN LOGIC 
### 

# Checks to see if user is Root or is using Sudo, otherwise exit 

if [ $(id -u) != 0 ] 
then 
    echo "You must have elevation to run this script." 
    exit 1 
fi 

# Prompts for name of site 
if [ -z "$1" ] 
then 
    read -p "Domain for the new site: " DOMAINNAME 

    if [ ! -z $DOMAINNAME ] 
    then 
     echo "Generating the empty directories now..." 
     generate_empty_dirs $DOMAINNAME 
    else 
     echo "You must provide a valid domain name" 
     exit 1 
    fi 
else 
    generate_empty_dirs $1 
    DOMAINNAME=$1 
fi 

# Determine the kind of site being generated 
read -p "Is this a (D)rupal or a (W)ordPress site? " SITETYPE 
read -p "Do you need both the dev and staging sites? [Y/N] (case sensitive): " NEEDDIRS 

    if [ -n "$SITETYPE" ] 
    then 
     if [ "$SITETYPE" == "D" ] 
     then 
      generate_drupal_dev_apacheconf $DOMAINNAME 
      generate_drupal_drush_dev_profile $DOMAINNAME 

      rm /var/www/$DOMAINNAME-dev.$URL/public_html 
      rm /var/www/$DOMAINNAME-dev.$URL/logs 

      cd /var/www/$DOMAINNAME-dev.$URL 
      eval "standardprofile" 

      if [ "$NEEDDIRS" == "Y" ] 
      then 
       generate_drupal_staging_apacheconf $DOMAINNAME 
       generate_drupal_drush_staging_profile $DOMAINNAME 
       cd /var/www/$DOMAINNAME-staging.$URL 
       eval "standardprofile" 
       OMIT_STAGING="n" 
      else 
       OMIT_STAGING="y" 
      fi 
     elif [ "$SITETYPE" == "W" ] 
     then 
      generate_wordpress_dev_apacheconf $DOMAINNAME 
      if [ "$NEEDDIRS" == "Y" ] 
      then 
       generate_wordpress_staging_apacheconf $DOMAINNAME 
       OMIT_STAGING="n" 
      else 
       OMIT_STAGING="y" 
      fi 
     else 
      echo "Invalid option provided" 
      exit 1 
     fi 
    else 
     echo "No option provided" 
     exit 1 
    fi 

alter_group_owner $DOMAINNAME 

# Load the new confs into Apache 

if [ "$OMIT_STAGING" == "y" ] 
then 
    a2ensite $DOMAINNAME-dev.$URL 
    rm -rf /var/www/$DOMAINNAME-staging.$URL 
else 
    a2ensite $DOMAINNAME-dev.$URL 
    a2ensite $DOMAINNAME-staging.$URL 
fi 

# Reload apache 
service apache2 reload 

# All done 

echo "Sites created at domains:" 
if [ "$OMIT_STAGING" == "y" ] 
then 
    echo $DOMAINNAME-dev.$URL 
else 
    echo $DOMAINNAME-dev.$URL 
    echo $DOMAINNAME-staging.$URL 
fi 

exit 
#End of script 

而且drush腳本

#!/usr/bin/env drush 
$a = drush_get_arguments(); 
$current_directory = getcwd(); 
$profiles = substr($a[1], 0, strlen($a[1])-15) . 'standard.make'; 
if(file_exists($current_directory . '/installsettings.php')) { 
    require_once($current_directory . '/installsettings.php'); 
} 
drush_print("Time to prepare our site install..."); 

if(!file_exists('logs')) { 
    drush_op_system('mkdir logs'); 
} 

if(!file_exists('public_html')) { 
    $prev = drush_get_context('DRUSH_AFFIRMATIVE'); 
    drush_set_context('DRUSH_AFFIRMATIVE', TRUE); 
    drush_invoke('make', array($profiles, 'public_html')); 
    drush_op_system('cp public_html/sites/all/modules/services/servers/rest_server/lib/spyc/spyc.php public_html/sites/all/modules/services/servers/rest_server/lib/spyc.php'); 
    #drush_shell_exec('cd public_html'); 
    #shell_exec('cd public_html'); 
    chdir('public_html'); 
    drush_invoke_process("@self","site-install",null,array(
    'db-url' => $db_engine . "://" . $db_user . ":" . $db_pw . "@localhost/" . $db_name, 
    'account-name' => $account_name, 
    'account-pass' => $account_pw, 
    'account-email' => $account_mail, 
    'db-su' => $db_su, 
    'db-su-pw' => $db_su_pw, 
    'site-mail' => $site_mail, 
    'site-name' => $site_name, 
    'clean-url' => FALSE, 
)); 
    drush_invoke_process("@self","pm-enable", 
     array('ctools', 'views', 'views_ui', 'features', 'strongarm', 'fe_block', 
      'entity', 'token', 'module_filter', 'pathauto', 'devel', 'simplehtmldom', 
      'services', 'rest_server', 'entity_token', 'date', 'date_api', 'date_tools', 
      'date_views', 'date_popup','rules', 'rules_admin', 'views_slideshow', 
      'views_slideshow_cycle', 'strongarm', 'diff', 'auto_nodetitle', 'libraries', 
      'realname', 'views_php'), 
     array('root' => $current_directory . '/public_html')); 
    drush_set_context('DRUSH_AFFIRMATIVE', $prev); 
} 

回答

3

有沒有更好的方法來指示bash腳本從指定的目錄中運行一個命令,然後一旦任何其他實用程序在前臺運行完成後恢復?

肯定的:

(cd $dir && cmd) 

將運行從指定的目錄CMD。由於這兩個命令都在子shell中運行,因此當子shell完成時,腳本將在當前目錄中恢復。

1

這應該工作:

cd /var/www/$DOMAINNAME-dev.$URL 
/usr/bin/env drush "standardprofile"