coll2 (Visitor) Apr 5, 2002
In my C program I am calling a shell script using the system command.I want to get the return value of the shell script to check if the shell script completed successfully or not.
Can someone help me do that?
Thank you
coll2
olded (Programmer) Apr 6, 2002
col2:
I'm not really a "C" expert; Unix is more my game, but this stub:
include <stdio.h>
main()
{
int i;
i=system("./x.ss"

;
printf("return value is: %d\n", i);
getchar();
}
executes shell script x.ss. Generally, system returns 0 if the shell script execution is successful, or non-zero if it fails. If the shell can't find the script, the "C" system return command is 256 (generally)
When executing a unix command, the exit status ($?) of the unix command is set, and the last status of the last unix command executed is what is returned to the system call.
A programmer can control this using the echo command. In a well-written unix shell script, a successfull termination ends with exit 0. An error condition, exit and echo a non-zero value.
Beware of the system return value. For example, setting exit 3 in the shell script, system return value is 768 (3 X 256). (At least that's what it returns on my Solaris 7 and Red Hat 7.1)
The important thing is, it's non-zero for an error condition.
Regards,
Ed
Schaefer
Hai,
I tried the `echo exitValue` as suggested by Ed
Schaefer. But in my Redhat7.2 I am getting the exit value as exitValue * 256 in the C-file.
I've trapped the exit and echoed the exit value.
If I run the script in my shell(bash) my exit status is proper but when I invoke the script from a c-program,
the out put is 256* exitStatus of the script.
The code snippet is given below
trap 'echo $EXIT_VAL' EXIT
EXIT_VAL=$1
exit $1