script

script

I have a script file to automatically compile a C++ program, then automatically run the executable.

script

g++ -o bin/program main.cpp 
bin/program

The problem is, sometimes the compiler encounters an error, and the executable is not generated. Therefor, the script will be running an outdated version.

How can I detect if g++ was unable to generate the executable, so that I may prevent the script from running the file?

답변1

You can check the exit code of execution of g++ like this:

g++ -o bin/program main.cpp 
if [ "$?" -eq 0 ]
then bin/program
fi

답변2

Check if the file exists first:

test -x foo && ./foo

관련 정보