Handling gcc warnings and outputting in Bash Script
So, I'm new to all Bash scripting scripting, but I'm working on a script to compile all .c files in a directory that I managed to do:
for F in *.c; do
gcc -c -Wall -o ${F%.c} $F
done
Everything works fine, but I want the result to look something like this:
ex1.c Errors
ex2.c OK
ex3.c Warnings
So basically I want to have an exit status of 0 (everything went well) for "OK", status 1 (warnings but no errors) for "Warnings" and status 2 (not compiled) for "Errors".
I find it difficult to figure out how to do this. I have searched around and could not find anything that I found useful. I could very well be missing something.
EDIT: Would it be all the same to say: if gcc -Wall file.c has errors, then just say "Error" instead of the full GCC error handling message? Is it the same with warnings and a great compiler?
source to share
gcc
returns 0
the exit code for warnings. Thus, you are going to distinguish it from 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
source to share
Jeffrey,
To create a project, you will need a build tool that displays the DAG. make is a classic example, SCons is a modern tool.
To answer bash's question, the following message will report the non-zero exit status:
for F in *.c; do
gcc -c -Wall -o ${F%.c} $F && [[ `echo $?` ]] || echo "Error"
done
The signs &&
and ||
are both and and. [[ echo $?
]] checks the exit status after calling gcc. If zero, then quiet, otherwise echo error.
I am referring to your specific question, so maybe this is helpful. But in general, compiling .c files in a bash for loop is not good practice.
source to share