J'ai ce programme en C
#include <stdio.h>
int main() {
    char foo[10];
    int i;
    for(i = 0; i < 20 ; i++) {
        foo[i] = 0;
    }
    return 0;
}Si je lance ce 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"
fiIl va sortir
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!C’est parce que gcc n’a pas considéré cette erreur comme une erreur, mais a quand même émis ces avertissements stderr.
Mais je veux toujours détecter cela dans le script bash.

gcccommande et effectuer votre test if [ $? -eq 0 -a ! -s err ].