backtick and system return different exit codes

M

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
 
G

Gunnar Hjalmarsson

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 ?

Where did you get the idea that backticks would return the exit code?
Thought about reading about backticks (and the qx// operator) in
"perldoc perlop"?
 
M

meenal_binwade

Gunnar said:
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 ?

Where did you get the idea that backticks would return the exit code?
Thought about reading about backticks (and the qx// operator) in
"perldoc perlop"?


I think i did not make myself clear.

i am doing something like this in a perl script :

my $do = `$cmd`;
print "dq = $?\n";

Here the exit code (obtained using $? ) is not correct.it is -1
although
the output $do is correct. $cmd is some c program.

But if a do this :
my $rc = system($cmd);
print "rc = $rc\n";

the exit code $rc is 0.
 
M

meenal_binwade

Hi Tim,
Thanks for the reply.

I am sure that the command is executed when i run it in backticks cause
it is returning the correct output.

I am not really interested in knowing the exact error code.i want to
know whether
the command executed successfully. i guess i can safely do this as
follows:

my $do = `$cmd`;
if( $?)
{
print " command failed \n";
print "output is $do\n";
}
else
{
print "output is $do\n";
}

print "\nexecuting system\n";
my $rc = system($cmd);
print "rc = $rc\n";
if($rc)
{
print " command failed \n";
}
else
{
print "command succeeded\n";
}

//end of script


The output looks like this:

command failed
myhost.com

executing system
myhost.com
rc = 0
command succeeded

//end of output


The wierd thing is if i run the same command in a shell or in a perl
script using system
it prints the correct output and also returns me an exit code of 0.

FYI I have done an explicit return 0 at the end of my program ( $cmd in
the example above is a c program).i have printed the return value
before the return statement in my main function .I can see that a 0 is
returned from the program when i run it in backticks but somehow $? is
showing me a value of -1.It should have given me a value of 0 cause
the command executed successfully .
 
G

Gunnar Hjalmarsson

I think i did not make myself clear.

Or I misunderstood you, sorry.
i am doing something like this in a perl script :

my $do = `$cmd`;
print "dq = $?\n";

Try:

print 'dq = ', $? >> 8, "\n";

(See Tim's reply for the reason why.)
 
M

meenal_binwade

Tim said:
You're still not adjusting the value of $? to get the actual
return code from the ``/system() calls. Again, $? contains more
than just the simple value after the "return" keyword of your
C program!


No it doesn't. Where's the "output is ..." information the
if/else structure following the backtick operation?

This code is still not a standalone piece of code which other
people may run to see the output you describe. The code you
provide, in addition to being incomplete, cannot produce the
exact output you describe below.


You have not *once* told us what the value of $cmd is, so we can
hardly reproduce this.


This is simply not true. You have never once, in any code
you've posted here, check that the return value of the external
program was 0. You have only checked that the value of $? is 0.
This is *not* the same thing.

Please respond with actual *full* code which demonstrates the
behavior, according to the posting guidelines. Please post code
that makes correct use of the $? variable. Please post code
that doesn't use the return value of one function to determine
the success of a different function.

Tim Hammerquist

Tim,
I cannot paste the code for $cmd program cause it is confidential and
it links with lots of other libraries in my project.

I modified my perl script to print the exit code as :
$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


so the problem is that when my program is executed in bacticks at times
does not give the correct exit code(i am sure that the command is
executed in bacticks cause i get the correct output ) but when execute
using system never gives incorrect exit code .

And this is happening only on one particular linux machine whoes
configuration i mentioned before.

I am using pthreads in my program.Also my main function does not join
on all the threads it creates.

I wrote 2 sample programs. One in which main thread joins on the
threads it creates and the other in which it does not.

The first one when executed in bacticks gives the same exit code
(72057594037927935)
but gives 0 exit code when executed throught system .This is again
happening only on that particular linux machine.I did not see any exit
code problems on any other machine(solaris , linux.AIX etc)

The second sample program never returned exit code (72057594037927935)
when executed in backticks

I feel there is some problem with that particular linux machine.cause i
dont this happening on any other machine. or it is a known fact that
when main thread does not join on the threads it creates , there could
be something wrong in the exit code of the program( mind you the exit
code is correct always when executed through system).
 
X

xhoster

Gunnar Hjalmarsson said:
Or I misunderstood you, sorry.


Try:

print 'dq = ', $? >> 8, "\n";

(See Tim's reply for the reason why.)

However, in the case that $? is -1, then bit-shifting it is not going
to clarify anything.

Xho
 
X

xhoster

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 )

When $? is -1, what is $! set to?

.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 ?

Can you reproduce this problem without using compiled C code that is not
available to us?

perl_misc]$ perl -le '`perl -e ""` ; print $?'
0
perl_misc]$ perl -le 'system q{perl -e ""} ; print $?'
0


Xho
 
C

chris-usenet

I modified my perl script to print the exit code as :
$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

What do you get for the following two snippets?

perl -e '$a = `true`; print "?=$?, SS=", $? >> 8, "\n"'

perl -e '$a = `false`; print "?=$?, SS=", $? >> 8, "\n"'

On this 32bit linux based system (i686, 2.6.15-rc6), using perl 5.8.7,
I get these two results:

?=0, SS=0

?=256, SS=1

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top