2

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

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.

EDIT: 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?

TheFatness
  • 349
  • 1
  • 5
  • 12
  • In bash you can `echo $?` to return the exit status from the preceding process call. However, other than zero / not-zero there is not going to be an easy, or even possible, mapping between gcc exit statuses and what you want. Have you looked into SCons? I can't tell you focus, whether it's about bash or compiling a bunch of target files. If you have a project then a DAG-oriented builder is what you want. – JayInNyc Mar 09 '14 at 20:49
  • Would there be a way to say: if gcc file.c has an error then echo Error? And so on, so forth? – TheFatness Mar 09 '14 at 20:53
  • possible duplicate of [How to check if gcc has failed, returned a warning, or succeeded? (in bash)](http://stackoverflow.com/questions/1024525/how-to-check-if-gcc-has-failed-returned-a-warning-or-succeeded-in-bash) – dtmland Aug 27 '15 at 20:10

2 Answers2

6

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
P.P
  • 117,907
  • 20
  • 175
  • 238
  • I like the concept behind this. However I get Ambiguous Redirect errors on: output=$(gcc -c -Wall -o ${F%.c} $F 2>$1). Also, would you mind giving a brief explanation as to what the <<< does exactly? A quick search yielded nothing. – TheFatness Mar 09 '14 at 21:11
  • It's not *2>$1* but `2>&1` ;) That's why you get the error. In bash, `<<<` is to read from a variable as opposed to reading from a file. It's called *here strings*. You can read more here: http://www.gnu.org/software/bash/manual/bashref.html#Here-Strings – P.P Mar 09 '14 at 21:20
  • I can't believe I misread that! But yes that was the result of the error. Also, thank you for the explanation on what <<< does. I appreciate the help! – TheFatness Mar 09 '14 at 21:29
-1

Jeffery,

To build a project you need a build tool that renders a DAG. make is the classic example, SCons is a modern tool.

To address you bash question, the following will report a non-zero exit status:

for F in *.c; do
  gcc -c -Wall -o ${F%.c} $F  && [[ `echo $?` ]] || echo "Error"
done

The && and || tokens are and and or. The [[ echo $? ]] tests the exit status after the gcc call. If zero then quiet, else echo error.

I'm addressing your specific question, so maybe that's useful. But in general compiling .c files in a bash for loop is not good practice.

JayInNyc
  • 2,193
  • 3
  • 20
  • 21