2015-04-29 97 views
1

我有兩個bash腳本。一個運行完全正常,直到它接到另一個腳本的調用,然後執行該腳本,但永遠不會在原始腳本中繼續,我不知道該如何解決該問題。腳本調用後Bash不會繼續?

此腳本在特定目錄的每個文件夾中運行測試。每個子文件夾都包含一個phpunit.xml文件,當我們輸入每個文件夾時,我們運行composer installphpunit

如果目錄是Loader,Routes或Loop,我們將輸入它們並在該目錄下運行腳本。

讓我們看看run-tests腳本:

#!/usr/bin/env bash 

set -e 

function run_tests() { 
    if [[ -f "composer.json" ]] && [[ -f "phpunit.xml" ]]; then 
    if [[ -d "vendor" ]]; then 
     composer update 
     phpunit > /dev/null 
     phpunit 
     cd ../ 
    else 
     composer install 
     phpunit > /dev/null 
     phpunit 
     cd ../ 
    fi 
    else 
    cd ../ 
    fi 
} 

for f in *; do 
    if [[ -d $f ]]; then 
    if [[ $f != ".git" ]] && [[ $f != "bin" ]] && [[ $f != "docs" ]]; then 
     cd "$f/" 

     if [[ $f == "Loader" ]]; then 
     if [[ -d "Assets" ]]; then 
      cd Assets/ 
      chmod +x asset-test 
      ./asset-test 
     fi 
     fi 

     if [[ $f == "Loop" ]]; then 
     cd Loop/ 
     chmod +x loop-test 
     ./loop-test 
     cd ../ 
     fi 

     if [[ $f == "Routes" ]]; then 
     cd Routes/ 
     chmod +x routes-test 
     ./routes-test 
     cd ../ 
     fi 

     run_tests 
    fi 
    fi 
done 

# Go Home. 
cd /vagrant/Freya/ 

## Clean up time! 
## Remove all vendor and composer.lock folders - just because. 
for f in *; do 
    if [[ -d $f ]]; then 
    if [[ $f != ".git" ]] && [[ $f != "bin" ]] && [[ $f != "docs" ]]; then 
     cd "$f/" 
     rm -rf vendor composer.lock 
     cd ../ 
    fi 
    fi 
done 

我們可以看到,一旦它進入檢查,看看如果我們在該目錄是「循環」,那麼,我們是否有一個名爲目錄「資產「裏面。假設此通過,我們就輸入一個目錄,然後運行下一個腳本:

#!/usr/bin/env bash 

set -e 

# Run Composer Install if vendor doesnt exist. 
if [[ -d "vendor" ]]; then 
    composer update 
else 
    composer install 
fi 

# Move up to the root directories and then get wordpress 
cd ../../../ 

# Determine if the trunk is checked out and if we have a wp-test-config.php 
# If not we need to create both or one or the other then run the tests. 
# 
# We run phpunit twice because the second time is when it actually runs. 
if [[ -d "trunk" ]]; then 
    cd "trunk/" 
    if [[ -f "wp-tests-config.php" ]]; then 
    cd ../Freya/Loader/Assets 
    phpunit > /dev/null 
    phpunit 
    else 
    cd ../Freya/Loader/Assets 
    cp wp-tests-config.php ../../trunk/ 
    phpunit > /dev/null 
    phpunit 
    fi 
else 
    sudo svn co http://develop.svn.wordpress.org/trunk/ 
    cd Freya/Loader/Assets 
    cp wp-tests-config.php ../../trunk/ 
    phpunit > /dev/null 
    phpunit 
fi 

的問題是,一旦這個劇本完成後,我張貼的第一腳本的其餘部分NEVER繼續。它就像死在這裏一樣。我需要的是「父」腳本完成,它應退出Loader/Assets並繼續下一個檢查。但事實並非如此。該腳本剛剛存在。

爲什麼?

注:中,第二腳本,我們可以假設它進入第一個嵌套的if語句,它會檢查,在一個wp-tests-config.php主幹目錄我們可以假設,因爲下面的輸出,這是從捕獲的這運行一個腳本:

$ bin/run-tests 
Loading composer repositories with package information 
Installing dependencies (including require-dev) 
Nothing to install or update 
Writing lock file 
Generating autoload files 
PHPUnit 4.6.4 by Sebastian Bergmann and contributors. 

Configuration read from /vagrant/Freya/Exceptions/phpunit.xml 

. 

Time: 451 ms, Memory: 13.25Mb 

OK (1 test, 1 assertion) 
Loading composer repositories with package information 
Installing dependencies (including require-dev) 
Nothing to install or update 
Writing lock file 
Generating autoload files 
PHPUnit 4.6.4 by Sebastian Bergmann and contributors. 

Configuration read from /vagrant/Freya/Factory/phpunit.xml 

........... 

Time: 711 ms, Memory: 13.25Mb 

OK (11 tests, 11 assertions) 
Loading composer repositories with package information 
Installing dependencies (including require-dev) 
    - Installing freya/freya-templates (dev-master a33ecdb) 
    Cloning a33ecdb231b06dc8a7eef363edb177b8c134d55b 

Writing lock file 
Generating autoload files 
PHPUnit 4.6.4 by Sebastian Bergmann and contributors. 

Configuration read from /vagrant/Freya/Form/phpunit.xml 

............................................................... 

Time: 647 ms, Memory: 15.25Mb 

OK (63 tests, 63 assertions) 
Loading composer repositories with package information 
Updating dependencies (including require-dev) 
Nothing to install or update 
Generating autoload files 
Installing... 
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml 
Not running ajax tests. To execute these, use --group ajax. 
Not running ms-files tests. To execute these, use --group ms-files. 
Not running external-http tests. To execute these, use --group external-http. 
PHPUnit 4.6.4 by Sebastian Bergmann and contributors. 

Configuration read from /vagrant/Freya/Loader/Assets/phpunit.xml 

........... 

Time: 1.76 seconds, Memory: 38.00Mb 

OK (11 tests, 11 assertions) 
Loading composer repositories with package information 
Updating dependencies (including require-dev) 
Nothing to install or update 
Generating autoload files 
Installing... 
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml 
Not running ajax tests. To execute these, use --group ajax. 
Not running ms-files tests. To execute these, use --group ms-files. 
Not running external-http tests. To execute these, use --group external-http. 
PHPUnit 4.6.4 by Sebastian Bergmann and contributors. 

Configuration read from /vagrant/Freya/Loader/Assets/phpunit.xml 

........... 

Time: 1.79 seconds, Memory: 38.00Mb 

我們可以在這裏看到兩個問題:

  • /vagrant/Freya/Loader/Assets/被運行兩次。
  • 它從來沒有繼續在第一個腳本。
+2

試着發佈一個最簡單的例子,因爲大多數人不會願意通讀所有這些,所以你將不可能得到答案。 –

+0

JID ++。你可能會發現,在你的腳本壓縮成一個最小的例子的過程中,你自己發現了這個問題! – ghoti

回答

2

如果沒有在您的機器上設置任何設置,就很難遠程調試它。要獲得更多有用的答案,可以先嚐試通過連續禁用部分代碼來隔離問題。我可以看到,在你的情況下這可能很難。

這裏僅僅是一些想法進行調試:

  • 兩個腳本與set -e運行,即他們將退出的第一個命令(包括第二腳本)與非零退出代碼終止。嘗試添加set -x以查看最後執行的命令。
  • 你有很多相對的cd調用。嘗試到pwd就在上一步中確定的最後一個命令之前 - 您可能只是在錯誤的目錄中。