note 29508 added to function.system
| From: | Bill at KilgallonFamily dot com | Date: | Sat, 15 Feb 2003 22:57:52 +0000 |
| Subject: | note 29508 added to function.system | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44033@lists.php.net to get a copy of this message | ||
I am new to PHP, but an old Unix hacker (from back in the days when that meant a good thing), so
sorry if this is old news...
Anyway, I just wanted to call a shell command, and get it's numeric status. Just the way God
intended system calls to work :) If I did not care about status, I would have used the backticks
(
/usr/bin/foo).
Anyway, it does not work (php 4.2.2). I add an argument to system that is supposed to trap the
numeric return status, and it always seems to get 198 on this system with my command, regardless of
the real return value of the command.
So I used an old school Unix hacker trick, coupled with what I think is a part of PHP that does not
seem to work the way I think it ought to, and putting these together gets what I want.
Here is the code that should work:
$resultStatus = system('/bin/ls /foo', $resultStatus);
print "$resultStatus";
But I don't think it does. I don't think the $resultStatus gets the right value.
So as a workaound, use the following code:
$resultStatus = /bin/ls /foo; echo $?;
print "$resultStatus";
$resultStatus then contains the return code of the system command, and it is a little easier then
the system / exec calls, as you get a $PATH env variable and your scripts will be less dependent on
file locations on your particular system.
Bill
--
http://www.php.net/manual/en/function.system.php
http://master.php.net/manage/user-notes.php?action=edit+29508
http://master.php.net/manage/user-notes.php?action=delete+29508
http://master.php.net/manage/user-notes.php?action=reject+29508