> From: WC -Sx- Jones [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, 4 March 2004 4:54 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Returning value from system()
> 
> [EMAIL PROTECTED] wrote:
> > Hi all,
> >       I executed the same version of the perl script(code 
> is given below)
> > which use the system() function both in linux and solaris 
> and got exit
> > values($?) as 65280 in solaris and -1 in linux.Any idea of 
> why i am getting
> > two different values in different OS.
> > Thanks & regards,
> > s.viswanathan
> > 
> > system("3.sh");
> > print qq/The value is-->$?/;===>with solaris the value is 65280
> >                                 with linux the value is -1

The return code is in the high 8 bits.  The low 8 bits are system
dependent.

[OSLARIS]
The high 8 bits of '65280' (0xFF00) is '0xFF' or 255, or '-1' as a
signed value.

[LINUX]
The high 8 bits of '-1' (0xFFFF) is '0xFF' or 255, or '-1' as a signed
value.

The low 8 bits for sloaris were '00', the low 8 bits for linux were 0xFF
or 255.  
The meaning is system dependent and should be tested against the various
WIFEXITED() WTERMSIG() etc macros for that system.

Cheers.

PS.  If you read the manual for 'wait(2)' you will see that
'WEXITSTATUS()' is
     documented as 'evaluates to the least significant eight bits of the
return
     code'.  Do not confuse this with the WEXITSTATUS() macro itself
which extract
     this return code from the most significant 8 bits of the result.


> 
> Different systems...
> 
> system PATHNAME LIST
> system LIST
> 
> Simple Example:
> my $erc system("netstat -nr") / 256;
> print "Netstat cmd error $?" if $erc;
> 
> This function executes any program on the system for you and returns 
> that program's exit status--not its output. To capture the 
> output from a 
> command, use backticks or qx// instead. The system function works 
> exactly like exec, except that system does a fork first and 
> then, after 
> the exec, waits for the executed program to complete. That 
> is, it runs 
> the program for you and returns when it's done, whereas execreplaces 
> your running program with the new one, so it never returns if the 
> replacement succeeds.
> 
> Argument processing varies depending on the number of arguments, as 
> described under exec, including determining whether the shell will be 
> called and whether you've lied to the program about its name by 
> specifying a separate PATHNAME.
> 
> Because system and backticks block SIGINT and SIGQUIT, sending one of 
> those signals (such as from a Control-C) to the program being run 
> doesn't interrupt your main program. But the other program you're 
> running does get the signal. Check the return value from 
> system to see 
> whether the program you were running exited properly or not.
> 
> @args = ("command", "arg1", "arg2");
> system(@args) == 0
>       or die "system @args failed: $?"
> 
> The return value is the exit status of the program as 
> returned through 
> the wait(2) syscall. Under traditional semantics, to get the 
> real exit 
> value, divide by 256 or shift right by 8 bits. That's because 
> the lower 
> byte has something else in it. (Two somethings, really.) The lowest 
> seven bits indicate the signal number that killed the process 
> (if any), 
> and the eighth bit indicates whether the process dumped core. You can 
> check all possible failure possibilities, including signals and core 
> dumps, by inspecting $? ($CHILD_ERROR):
> 
> $exit_value  = $? >> 8;
> $signal_num  = $? & 127;    # or 0x7f, or 0177, or 0b0111_1111
> $dumped_core = $? & 128;    # or 0x80, or 0200, or 0b1000_0000
> 
> When the program has been run through the system shell 
> because you had 
> only one argument and that argument had shell metacharacters in it, 
> normal return codes are subject to that shell's additional quirks and 
> capabilities. In other words, under those circumstances, you may be 
> unable to recover the detailed information described earlier.
> 
> 
> If you want to be completely safe:
> 
> $pid = open(KID_TO_WRITE, "|-");
> die "cannot fork: $!" unless defined($pid = open(KID_TO_WRITE, "|-"));
> $SIG{ALRM} = sub { die "whoops, $program pipe broke" };
> 
> if ($pid) {  # parent
>     for (@data) { print KID_TO_WRITE $_ }
>     close(KID_TO_WRITE)              or warn "kid exited $?";
> 
> } else {     # child
>     # reconfigure, then
>     exec($program, @options, @args)  or die "can't exec program: $!";
> }
> 
> 
> -Bill-
> __Sx__________________________________________
> http://youve-reached-the.endoftheinternet.org/
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to