Unix OS--How do read the R/C after running a C program

OK. I know this sounds like a really basic question, and I’ve been doing this sort of work for years. But this situation has never come up before because in the past I’ve always been able to rely on other ways to verify program results. But we’re in the process of implementing a new scheduling package so I need to know how to do this.

The platform is Unix V4.0E(Ksh).

If you mean “Result Code” (the value returned to the shell from by the program when it exits), then this bit of perl will do the trick.


$result = system(‘mycprogram’);
print "$result

or you could do whatever you wanted with it.

If that’s not what you’re asking, clarify please.

I think you may mean:

echo ? ksh will store the exit code of a program in the variable called "?". So a "echo ?" immediately after running a program will display it.

The same thing works in bash, sh, etc. I think it’s a kernel thing, not a shell thing.

I don’t know if it applies to other platforms or not though.

No, it’s definitely a shell thing. It’s just that $? is common to many shells. I’m not positive it works that way in csh or tcsh. It might be $status or something “more intuitive” there.

$? works in perl too, btw. :slight_smile:
And it has become sorta standard. Just like `` for forking a shell and executing what’s between them.

Just tried it.
Amusingly, both $? and $status work under tcsh.

perl -e ‘use English;foo;print $CHILD_ERROR’

One of my coworkers came by and told me about the ‘$?’ thing. I knew there was something like that but couldn’t remember.

The perl-related responses are helpful, too, as we will shortly begin using perl.

Another thing to watch out for is that only the least significant 8 (7?) bits of $? (which I believe is equal to the status value returned by wait(2)) are equal to the exit status of the process–the rest tell you what signal caused the process to exit if it was killed somehow. The only way I know of to sort this out is to use the macros from sys/wait.h, which are described in the wait(2) man page.

$status is the (official at least) method in csh