Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if gcc has failed, returned a warning, or succeeded in Bash?

Tags:

bash

gcc

How would I go about checking whether gcc has succeeded in compiling a program, failed, or succeeded but with a warning?

#!/bin/sh

string=$(gcc helloworld.c -o helloworld)

if [ string -n ]; then
    echo "Failure"
else
    echo "Success!"
fi

This only checks whether it has succeeded or (failed or compiled with warnings).

-n means "is not null".

Thanks!

EDIT If it's not clear, this isn't working.

like image 272
Tyler Avatar asked Jun 21 '09 18:06

Tyler


1 Answers

Your condition should be:

if [ $? -ne 0 ]

GCC will return zero on success, or something else on failure. That line says "if the last command returned something other than zero."

like image 168
RichieHindle Avatar answered Oct 12 '22 04:10

RichieHindle