2015-05-29 30 views
2

我正在編寫一個bash腳本以從origin.Git中抽取master分支,並在每個應用程序模塊中進行初始化。在bash中的命令之間使用&號(&)時重定向stdout和stderr

我的目錄結構:

.-----bash_script 
|----app1(git init) 
        |...various modules 
        |...error.txt 
        |...output.txt 
|----app2(git init) 
        |...various modules 
        |...error.txt 
        |...output.txt 

在bash_script:$目錄包含(APP1或APP2 ...它在一個循環中被調用)

cd "${directory}" && git checkout master && git pull origin master >> output.txt 2>>error.txt 
cd .. 

以上兩行被稱爲爲每應用程序。 這是我迄今所做

問題:

  1. 我只得到stdout/stderr只爲我的最後一個命令即從原產和結賬不拉。是否可以在每個&&之前得到整個命令的輸出而無需寫入文件名?那麼如何在應用程序目錄內部生成error.txt/output.txt文件

  2. 。我怎樣才能在bash_script目錄中生成它們,即上一層。

  3. 當執行GIT中拉原點主

    From http://gitProjectUrl branch master -> FETCH_HEAD

    上述線在error.txt重定向。

    Already up-to-date.

    而此行是在output.txt的重定向

    爲什麼兩條線不是output.txt的

回答

1

只是它們組合在一起使用{...}

{ cd "${directory}" && git checkout master && git pull origin master; } >>output.txt 2>>error.txt 
cd .. 
+1

我想這是更好地去'{}'過'()'當可能的,但子shell的一個很好的副作用是,有沒有必要之後改變目錄。 –

+0

是的,但通常'cd -'會照顧它並且分殼子有比這更多的副作用。 – anubhava

2

使用子Shell (...)收集所有輸出在一個單一的數據流:

(cd ... && ... && git pull origin master) >> output.txt 2>>error.txt 

這解決了這兩個問題,因爲流在​​cd命令之前製備。

相關問題