2013-04-10 105 views
1

我在Code :: Blocks中有一組C++項目和一個構建所有這些項目的腳本。該腳本看起來如下:Linux shell kill命令基於輸出仍然運行

codeblocks --build --target="release32" project1.cbp 
codeblocks --build --target="release32" project2.cbp 
codeblocks --build --target="release32" project3.cbp 
... 

有時候建設項目之一時,代碼:: Blocks的失敗與錯誤完成該過程:*** glibc detected *** codeblocks: corrupted double-linked list。這有沒有關係正在失敗的構建,這僅僅是在代碼塊中的錯誤,在這裏看到: https://bugs.launchpad.net/ubuntu/+source/codeblocks/+bug/764728 或在這裏: http://forums.codeblocks.org/index.php?topic=16883.0

,我想做一個解決方法是解析命令而輸出它仍在運行,找到字符串「codeblocks:損壞的雙鏈表」,並殺死代碼塊讓腳本構建其他項目並完成。

我該怎麼做?

+0

一位來自'代碼塊--build --target =「release32」 project1.cbp'不會造成進一步的命令被跳過,除非你有'設置-e',或'Makefile'運行失敗的命令.. – anishsane 2013-04-10 11:42:44

+1

它不會失敗,它只是打印一個錯誤,不會退出。其他命令不會執行,因爲該命令尚未完成。 – psyched 2013-04-10 12:06:41

回答

2

有點哈克,但會爲你工作(bash)?

這僅僅是模擬二進制代碼塊(./codeblocks):

$ cat codeblocks 
#!/bin/bash 

num=$RANDOM 
((num %= 2)) 

case "$num" in 
     0) 
       echo "codeblocks finished successfully: $*" 
       exit 0 
       ;; 
     1) 
       echo "*** glibc detected *** codeblocks: corrupted double-linked list" 1>&2 
       while true; do 
         sleep 1 
       done 
       ;; 
esac 

這是實際的測試腳本(test.sh):

$ cat test.sh 
#!/bin/bash 

run_codeblocks() 
{ 
     until (
       subshell_pid=$BASHPID 
       echo "trying to run 'codeblocks $*'" 
       ./codeblocks "[email protected]" 2>&1 | while read line; do 
         echo "[${line}]" 
         [[ ${line} == *"*** glibc detected ***"* ]] && kill $subshell_pid 
       done 
       return 0 
     ); do 
       : 
     done 
} 

echo "running codeblocks ..." 
run_codeblocks 1 
run_codeblocks 2 
run_codeblocks 3 
echo "... done" 

您纏繞調用codeblocks在子shell, grep它的輸出,如果一行符合你聲明的錯誤會殺死子shell。基本上正是你所描述的。

$ ./test.sh 
running codeblocks ... 
trying to run 'codeblocks 1' 
[codeblocks finished successfully: 1] 
trying to run 'codeblocks 2' 
[*** glibc detected *** codeblocks: corrupted double-linked list] 
./test.sh: line 4: 29889 Terminated    (subshell_pid=$BASHPID; echo "trying to run 'codeblocks $*'"; ./codeblocks "[email protected]" 2>&1 | while read line; do 
    echo "[${line}]"; [[ ${line} == *"*** glibc detected ***"* ]] && kill $subshell_pid; return 0; 
done) 
trying to run 'codeblocks 2' 
[*** glibc detected *** codeblocks: corrupted double-linked list] 
./test.sh: line 4: 29892 Terminated    (subshell_pid=$BASHPID; echo "trying to run 'codeblocks $*'"; ./codeblocks "[email protected]" 2>&1 | while read line; do 
    echo "[${line}]"; [[ ${line} == *"*** glibc detected ***"* ]] && kill $subshell_pid; return 0; 
done) 
trying to run 'codeblocks 2' 
[codeblocks finished successfully: 2] 
trying to run 'codeblocks 3' 
[*** glibc detected *** codeblocks: corrupted double-linked list] 
./test.sh: line 4: 29903 Terminated    (subshell_pid=$BASHPID; echo "trying to run 'codeblocks $*'"; ./codeblocks "[email protected]" 2>&1 | while read line; do 
    echo "[${line}]"; [[ ${line} == *"*** glibc detected ***"* ]] && kill $subshell_pid; return 0; 
done) 
trying to run 'codeblocks 3' 
[codeblocks finished successfully: 3] 
... done 
+0

這看起來很神奇,它幾乎奏效!幾乎...我不知道爲什麼,但輸出的某些部分是由「while read line」處理的,而其他部分則不是。通過將輸出包裝到方括號中,你做了好事。在這裏,我的輸出如何:http://pastebin.com/nu0PRCqg – psyched 2013-04-10 14:29:31

+0

@ user1113852瘋狂的猜測,一些輸出轉到stderr,它不會被捕獲。嘗試在讀取管道之前將代碼塊的stderr重定向到stdout('2>&1')。 – 2013-04-10 14:36:50

+0

試圖像你建議的重定向,仍然沒有抓到。我也意識到錯誤發生在電話會議之後,而不是最終,就像我想的那樣。因此僅僅殺死代碼塊是不夠的,我還需要重複失敗的命令。好傢伙。 – psyched 2013-04-10 14:56:43