0

In my Perl, I want to use linux merge command and print its exit code:

system "merge $A $old $new";
print "exit code= $?\n";

And in linux manual, the exit code for merge command should be:

Exit status is 0 for no conflicts, 1 for some conflicts, 2 for trouble.

But in fact my result is :

merge: warning: conflicts during merge

exit code= 256

it looks like the exit code is "256" not "1". Why cause this situation? and how can I get the correct exit code? Thanks!

blueFish
  • 69
  • 3
  • 2
    Why not look at the return value of `system()` itself? – tadman Nov 18 '20 at 06:15
  • @tadman, how to get return value? – blueFish Nov 18 '20 at 06:19
  • Like `my $result = system(...)` presumably, just as you'd do with any variable. – tadman Nov 18 '20 at 06:19
  • 1
    The value `256` corresponds to hex 0x0100, and the upper byte is the exit status (in this case, status 1) and a non-zero lower byte indicates 'death from a signal' (the value indicating which signal and whether there was a core dump). This is the way the `wait()` system call (and its relatives) gets the encoded exit status from a program on Unix-like systems. – Jonathan Leffler Nov 18 '20 at 06:19
  • 1
    See, for example, [Why does `wait()` set exit status to 256 instead of the exit -1 status of the forked process?](https://stackoverflow.com/q/3659616/15168) — a title which is slightly incoherent as 256 corresponds to exit status of +1, not -1. – Jonathan Leffler Nov 18 '20 at 06:25
  • oh OK!!!!! I can understand now! thank you guys! – blueFish Nov 18 '20 at 06:28
  • 2
    Notice that [`perldoc perlvar`](https://perldoc.perl.org/perlvar) says of `$?`: _Finally, `$?` may be set to a non-0 value if the external program … fails. The upper eight bits reflect specific error conditions encountered by the program (the program's `exit()` value). The lower eight bits reflect mode of failure, like signal death and core dump information. See_ wait(2) _for details._ And [perldoc perlfunc](https://perldoc.perl.org/perlfunc#system-LIST) says similarly: _The return value [of `system`] is the exit status of the program as returned by the wait call._ – Jonathan Leffler Nov 18 '20 at 06:28
  • I think this question may help you: https://stackoverflow.com/questions/7080434/getting-perl-to-return-the-correct-exit-code – Miguel Prz Nov 18 '20 at 07:14

0 Answers0