Just one example.
test_error.cpp containing the main function:
#include<iostream>
using namespace std;
int main()
{
cout<<"This is just for an error simulation"<<endl;
return 12;
}
Get an simple a.out, after compiling the above cpp file with g++ test_error.cpp
shell script(test_error.sh):
if ./a.out; then
echo "This program runs successfully."
else
echo "Error number is $?"
fi
In a shell script, 0 means true, while others are false.
Change its mode: chmod +x test_error.sh
Run the shell script: ./test_error.sh
And now you can find the output is
This is just for an error simulation
Error number is 12
Don't forget ';' between 'a.out' and 'then' in test_error.sh, and '?' after '$' can catch the return value of a.out