Linux shell kill命令在仍然基于输出运行时

时间:2013-04-10 09:42:53

标签: linux parsing shell output terminate

我在Code :: Blocks中有一组C ++项目和一个构建所有这些项目的脚本。该脚本如下所示:

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

有时在构建其中一个项目时,Code :: 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:corrupted double-linked list”,然后终止代码块以让脚本构建其他项目并完成。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

有点hacky,但这对你有用吗(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 "$@" 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"

您将调用包装在子shell中的codeblocks,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 "$@" 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 "$@" 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 "$@" 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