As you can see the outputs are very similar, but not identical, however this is not an article about shell programming in general, it's about putting PHP to some advanced uses. You can see from the examples above that commands are joined with the vertical bar character '|' as this is the pipe character, and to cut this short basically means take the output of one command and feed to the next via the standard input stream.
This means if we read the standard input stream using the php file stream 'php://stdin' then we can use PHP to construct blocks of code that can participate in the filter chain.
Enough talking already, give us some code!!!!
Ok ok, I hear you. So by way of an example to show what IĀm on about, we'll put together a small example under Linux that shows what those file permission flags in a file listing mean.
First, if we do an 'ls -al' we should see something like this:
The first thing we need to do here is to read this into a PHP script.
<code>
<?php
// Open PHP's standard input stream
$input_stream = fopen("php://stdin","r");
// Array to store the received data
$lines = array();
// Loop & process while we receive lines
while($line = fgets($input_stream,4096)) // Note 4k lines, should be ok for most purposes
{
//Store each line in the array, ensuring we chop off the line ends
$lines[] = trim($line);
}
fclose($input_stream);
print_r($lines);
?>
</code>
If you run this from a Linux command line using 'ls -al | php -q fancydir.php' (replace php file name as required), you'll get something like the following:
As you can see, the directory listing is in an array, line by line as sent to the script. We can now use this array to go over each line and redisplay it, something like this: