The second input argument($2) is a path to a c program. I need to check if that C program compiles or not.
I believe this is how to compile the C program:
cc $2
How can the program tell if the C program file compiled or not?
Assuming this is a POSIX shell (e.g. Bash), you can either write something like this:
cc "$2"
if [ $? = 0 ] ; then
# . . . commands to run if it compiled O.K. . . .
else
# . . . commands to run if it failed to compile . . .
fi
or a bit more tersely:
if cc "$2" ; then
# . . . commands to run if it compiled O.K. . . .
else
# . . . commands to run if it failed to compile . . .
fi
In the special case that you simply want to run a certain command if compilation failed, e.g. exit 1, you can write something like:
cc "$2" || exit 1
$?is equal to 0?cc $2 || echo "compilation failed", but then the compiler output would be more helpful.