2

I run a script using a pipe

my $pid = open (OUTPUT, "$my_script") || "";
if (! $pid) {
  die("error");
}
while (<OUTPUT>) {
  print;
}
close (OUTPUT);
my $exit_status = $?>>8;
print "$exit_status";

Some times I get a long exit status: 72057594037927935

What does this mean? What could cause this?

Morad
  • 2,761
  • 21
  • 29
  • 1
    I guess you are using a 64 bit version of Perl. The number you posted is the same as -1 (`0xFFFFFFFFFFFFFFFF`) shifted by 8 bits. I think the 'real' exit status is -1. – Hermann Schachner Apr 28 '15 at 09:28
  • There is something wrong going here as the exit code has to be on the range `0..255`, `-1` is not a valid exit code. Probably, some error checking would help (after `open` and `close`). – salva Apr 28 '15 at 10:04
  • @salva you are right, I'm unable to reproduce this constantly. I tried a script that always exits with exit status -1, but I get 255 instead of this long number... I don't know how to reproduce this issue constantly – Morad Apr 28 '15 at 10:19
  • @Morad, even if you call `exit(-1)` the value is truncated to 1 byte. See [exit(3)](http://man7.org/linux/man-pages/man3/exit.3.html). – salva Apr 28 '15 at 10:22
  • Have you set `$SIG{CHLD} = 'IGNORE'` in some other part of your program? – salva Apr 28 '15 at 10:24
  • @salva No but I'm setting signal handlers for ALRM, TERM, INT and USR2 – Morad Apr 28 '15 at 10:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76435/discussion-between-salva-and-morad). – salva Apr 28 '15 at 10:37

1 Answers1

1

You get a long number because the value of $? is -1. Right shifting it 8 bits yields the large number you get. Try this:

print -1>>8;

$? is -1 because the close() function fails for some reason, and not because the script exits with exit status -1.

When the close() succeeds and the script exits with -1 then the value of $? won't be -1, but the value $?>>8 will be 255 as described here: Why is the exit code 255 instead of -1 in Perl?

Community
  • 1
  • 1