Find the answer to your Linux question:
Results 1 to 5 of 5
On a particular linux machine when i run a program in backticks in a perl script it gives me correct output but does not gives me a exit code of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2005
    Posts
    3

    backtick and system return different exit codes


    On a particular linux machine when i run a program in backticks in a
    perl script it gives me correct output but does not gives me a exit
    code of 0(it gives -1 ) .But executing the same program with system
    gives correct ouput and exit code of 0.
    [my program is doing a return 0 in main ]

    uname -a on the linux m/c gives this:
    Linux test 2.6.9-11.ELvsmp #1 SMP Tue Jun 7 14:13:29 BST 2005 x86_64
    x86_64 x86_64 GNU/Linux

    Why would backticks not return correct exit code while system does for
    the same program ?

    Thanks in advance for any help

  2. #2
    Just Joined!
    Join Date
    Dec 2005
    Posts
    3
    Quote Originally Posted by meenal_binwade
    On a particular linux machine when i run a program in backticks in a
    perl script it gives me correct output but does not gives me a exit
    code of 0(it gives -1 ) .But executing the same program with system
    gives correct ouput and exit code of 0.
    [my program is doing a return 0 in main ]

    uname -a on the linux m/c gives this:
    Linux test 2.6.9-11.ELvsmp #1 SMP Tue Jun 7 14:13:29 BST 2005 x86_64
    x86_64 x86_64 GNU/Linux

    Why would backticks not return correct exit code while system does for
    the same program ?

    Thanks in advance for any help
    I tried to futher investigate this problem.
    My program uses pthreads.

    To investigate i wrote 2 sample programs : one in which the main thread does not join on the threads it creates and the other in which it does.

    I saw that at times the first program when executed in backtics gives an exit code of -1 but when executed through system always gives exit code 0.
    This is happening only on one particular linux machine whoes configuration i had mentioned before.

    The second sample program did not have this issue.

    So is a known that if the main thread does not join on its threads , the exit code could be wrong ?

    ( What is very weird is that this happens only at times when the program is executed in backticks and only on one machine .system command gives the correct exit code always .This makes me believe there is nothing wrong with my code)

    Any comments?

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Are you getting the return code from the command you're running in backticks via the $? variable? The relevant code snippet may be helpful.

  4. #4
    Just Joined!
    Join Date
    Dec 2005
    Posts
    3
    Quote Originally Posted by scm
    Are you getting the return code from the command you're running in backticks via the $? variable? The relevant code snippet may be helpful.
    Ya i am getting the return code using $? :
    My perl script does this

    $do = `$cmd`;
    print 'ec = ', $? >> 8, "\n";
    $rc = system($cmd);
    print 'ec = ', $? >> 8, "\n";

    the first print statement at time gives this : ec = 72057594037927935
    and the second always gives this : ec = 0
    [$? for the first print statement is -1 and for the second it is 0]

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    What should the exit status of the $cmd command be? What does $rc contain after you've run system($cmd)? That's where you'll find the exit status of $cmd from that. I don't think you're checking the same thing in the two cases - if I understand Perl $rc will contain the exit status from $cmd, while $? will contain the exit status of the shell that system runs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •