有點哈克,但會爲你工作(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
一位來自'代碼塊--build --target =「release32」 project1.cbp'不會造成進一步的命令被跳過,除非你有'設置-e',或'Makefile'運行失敗的命令.. – anishsane 2013-04-10 11:42:44
它不會失敗,它只是打印一個錯誤,不會退出。其他命令不會執行,因爲該命令尚未完成。 – psyched 2013-04-10 12:06:41