我想制作一个构建链脚本,并且如果在编译过程中出现错误,我不希望它执行到结尾。此bash脚本中的错误
这是我第一次写的比较“论述了”在bash脚本,它只是不工作:
- 它没有回音ERROR虽然我有它 字错误线
- 无论是testError的值,该脚本只是挂在行
这是代码:
testError=false
output=$(scons)
while read -r line; do
if [[ $line == .*[eE]rror.* ]] ; then echo 'ERROR' ; $testError = true ; fi #$testError = true fi
done
echo $testError
if $testError ; then exit ; fi;
... other commands
编辑:以下所有海报答案和Bash setting a global variable inside a loop and retaining its value -- Or process substituion for dummies和How do I use regular expressions in bash scripts?, 这是代码的最终版本。 它的工作原理:
testError=false
shopt -s lastpipe
scons | while read -r line; do
if [[ $line =~ .*[eE]rror.* ]] ; then
echo -e 'ERROR'
testError=true
fi
echo -e '.'
done
if $testError ; then
set -e
fi
如果有版本4.2.24,可能会这样做。 – 2013-02-14 00:19:46