in reply to gzip command always returns -1

From the perlvar manpage:

 $?      The status returned by the last pipe close, backtick (``) com-
           mand, successful call to wait() or waitpid(), or from the sys-
           tem() operator.  This is just the 16-bit status word returned
           by the wait() system call (or else is made up to look like it).
           Thus, the exit value of the subprocess is really ("$? >> 8"),
           and "$? & 127" gives which signal, if any, the process died
           from, and "$? & 128" reports whether there was a core dump.
            (Mnemonic: similar to sh and ksh.)

So you need to shift $? to get the return value of the command.
-derby

Replies are listed 'Best First'.
Re^2: gzip command always returns -1
by jyoshv (Novice) on Jan 17, 2005 at 16:52 UTC
    Hi Derby, Thanks for your reply but doing below is giving me the following result and I am not sure if I am doing right. Please advice. Thanks, J.
    `gzip -9 $file`; my $res1 = $? >> 8; IN child gzip for t2 Command Result == 16777215
      Well 16 777 215 in decimal is 0x00FF 0xFFFF in hex which at first looks like -1 (in 2's complement form) (if you have 24 bit words) but I assume you probably have 32 bit words. Anyway the exit code should only be 8 bits long (in Linux anyway, your system *might* be different) so I don't know where the number 16777215 is coming from offhand.

      Update: added the hex printf lines to show how the the 32-bit -1 get's shifted 8 bits and becomes a 24-bit -1.

      Try this program out and see what you get:
      #!/usr/bin/perl -w use strict; my $raw_ret; print "'true':\n"; `true`; $raw_ret = $?; printf("0x%08X\n", $raw_ret); print $raw_ret; print "\n"; print $raw_ret >> 8; print "\n"; print (($raw_ret >> 8) & 0xFF); print "\n"; print "\n"; print "'false':\n"; `false`; $raw_ret = $?; printf("0x%08X\n", $raw_ret); print $raw_ret; print "\n"; print $raw_ret >> 8; print "\n"; print (($raw_ret >> 8) & 0xFF); print "\n"; print "\n"; print "'nosuchprogramname':\n"; `nosuchprogramname`; $raw_ret = $?; printf("0x%08X\n", $raw_ret); print $raw_ret; print "\n"; print $raw_ret >> 8; print "\n"; print (($raw_ret >> 8) & 0xFF); print "\n"; print "\n";
      I get this output:
      [frink@truth ~/code/perl]$./ret-code.pl 'true': 0x00000000 0 0 0 'false': 0x00000100 256 1 1 'nosuchprogramname': Can't exec "nosuchprogramname": No such file or directory at ./ret-cod +e.pl line 26. 0xFFFFFFFF -1 16777215 255
      I don't think your system can find "gzip" in it's search path. Either that or there is some other reason it can't execut "gzip". Try using the full path to the gzip program in your command. (To find the path use "which gzip" or "whereis gzip" or even "locate gzip" from a command line.)