Anonymous User said:
if ( system() ) /* see if a shell exists */
That should be:
if ( system(NULL) ) /* see if a shell exists */
{
int return_code = system("echo hello");
That's fine, but there are one and a half reasons why it isn't doing what
you expect. Firstly, the command (*almost* certainly) is being executed,
but a shell is being created for the purpose, the echo is happening within
that shell, and the shell is then terminating - all too fast for you to
even notice (and there's no requirement on the shell, as far as I know, to
provide a visible terminal session, although on some systems it actually
will do that).
One-and-a-halfthly, and this is more of a heads-up in your case than an
actual problem, the return value is not necessarily the one that the
command returns (alas!), but an implementation-defined value. On my
system, however, the docs say that "The value returned is -1 on error
(e.g. fork failed), and the return status of the command otherwise." ISO C
doesn't guarantee this, but perhaps your implementation does.
std::cout << "return code " << return_code << std::endl;
This, however, will not compile. It's stuffed full with errors.