我想製作一個構建鏈腳本,並且如果在編譯過程中出現錯誤,我不希望它執行到結尾。此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