处理GCC警告和输出bash脚本 [英] Handling gcc warnings and output in a Bash Script
问题描述
所以,我是新来的整个bash脚本编程的场景,但我在一个脚本一起汇编目录中的所有的.c文件,我已经成功地这样做:
So I am new to the whole Bash scripting scene, but I am working on a script to compile all the .c files in a directory which I have succeeded in doing so by:
for F in *.c; do
gcc -c -Wall -o ${F%.c} $F
done
一切工作正常,但我想输出看起来是这样的:
Everything works fine, but I want to the output to look something like:
ex1.c Errors
ex2.c OK
ex3.c Warnings
所以基本上我想有0退出状态(一切正常)为OK,状态1(警告,但没有错误)的警告和状态2(未编译)为错误。
So basically I want to have an exit status of 0 (Everything went fine) for "Ok", status 1 (Warnings, but no error) for "Warnings" and status 2 (Did not compile) for "Errors".
我有一个很难搞清楚如何做这样的事情。我周围中搜索,但没有找到任何东西,我发现是有用的。我很可能虽然忽略了什么。
I am having a hard time figuring out how to do such a thing. I've searched around and couldn't find anything that I have found to be useful. I very well could have overlooked something though.
编辑:会不会有反正说:如果GCC -Wall file.c中有错误,然后只是说错误的完整GCC错误处理的消息呢?同样的,警告和完美的编译?
Would there be anyway to say: if gcc -Wall file.c has errors, then just say Error instead of the full GCC error handling message? Same with warnings and a perfect compile?
推荐答案
GCC
收益 0
退出code表示警告,以及。所以,你要区分自己与这样的:
gcc
returns 0
exit code for warnings as well. So you are going to differentiate that yourself with something like this:
for F in *.c; do
out=$(gcc -c -Wall -o ${F%.c} $F 2>&1)
if [ $? -ne 0 ]; then
echo $F Errors
else
if grep "warning:" <<<"${out}" >/dev/null ; then
echo $F Warnings
else
echo $F OK
fi
fi
done
这篇关于处理GCC警告和输出bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!