Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return value from system command

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
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
 
col2:

I'm not really a "C" expert; Unix is more my game, but this stub:

include <stdio.h>

main()
{

int i;

i=system(&quot;./x.ss&quot;);
printf(&quot;return value is: %d\n&quot;, 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 &quot;C&quot; 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
 
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 &quot;C&quot; expert; Unix is more my game, but this stub:

include <stdio.h>

main()
{

int i;

i=system(&quot;./x.ss&quot;);
printf(&quot;return value is: %d\n&quot;, 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 &quot;C&quot; 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
 
My 2 cents ...

The 'system' call basically forks another shell to execute your script. The parent must 'wait' for its child to finish execution.

NOTE: this is on a solaris machine

Heres how I can determine my exit code for a C program calling a shell script.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

main()
{
int i = 0;

i = system(&quot;temp.sh&quot;)

printf(&quot;Return Code: %d\n&quot;, WEXITSTATUS(i))

return 0;
}

THE SHELL SCRIPT

#!/usr/bin/sh
echo &quot;I'm running a shell script&quot;

# to test add exit X (uncomment to try a non-zero return)
# exit 3

------------------------------

The WEXITSTATUS() macro (defined in sys/wait.h) finds the
status code that was returned by the 'system' call. This
may or may not always be 256*exit_code. (I could be wrong).
You can do a 'man wstat' for more information on the macro.

Hopefully this helps somewhat ... ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top