LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Exit status of system() call (https://www.linuxquestions.org/questions/linux-newbie-8/exit-status-of-system-call-869183/)

shankar.489 03-17-2011 09:37 AM

Exit status of system() call
 
hi folks,
i need to execute a script from my c program using
system()
or popen()
and i should able to collect its status in my code

i tried with "wait", "WEXITSTATUS" calls but iam not able to get the status correcly(getting junk)
WEXITSTATUS(system("./myscript") );

is there any to collect the status of popen or system in our code if so please let me know

i cannot use pipe for reading the status please suggest me the better possible ways

thanks in adv

~shankar

David1357 03-17-2011 10:49 AM

Quote:

Originally Posted by shankar.489 (Post 4293899)
WEXITSTATUS(system("./myscript"));

You really need to carefully read the man page for system. After that, you should use something like this
Code:

int run_myscript(void)
{
    int result;

    result = system("./myscript");

    if (-1 == result)
    {
        perror("system");
        exit(1);
    }

    if (127 == WEXITSTATUS(result))
    {
        fprintf(stderr, "%s", "ERROR: Could not execute /bin/sh\n");
        exit(2);
    }

    return(WEXITSTATUS(result));
}


shankar.489 03-18-2011 12:54 AM

Hai David,

thaq very much its working fro me

~shankar


All times are GMT -5. The time now is 11:04 AM.