I have this program in C
#include <stdio.h>
int main() {
char foo[10];
int i;
for(i = 0; i < 20 ; i++) {
foo[i] = 0;
}
return 0;
}
If I run this script
#!/bin/bash
gcc -O3 -o hello hello.c
if [ $? -eq 0 ]
then
echo -e "\033[1;32mcompilation sucess!\033[0m"
else
echo -e "\033[1;31mcompilation error!\033[0m"
fi
It will output
hello.c: In function ‘main’:
hello.c:8:10: warning: iteration 10u invokes undefined behavior [-Waggressive-loop-optimizations]
foo[i] = 0;
^
hello.c:6:2: note: containing loop
for(i = 0; i<20 ;i++)
^
compilation sucess!
That is because gcc didn't considered that an error but still output those warnings on stderr
.
But I still want to detect that within the bash script.
gcc
command and make your testif [ $? -eq 0 -a ! -s err ]
.