{"id":1295,"date":"2018-01-19T19:22:40","date_gmt":"2018-01-19T19:22:40","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/php-and-shell-scripting-using-pipes-page-2\/"},"modified":"2018-01-19T19:24:40","modified_gmt":"2018-01-19T19:24:40","slug":"php-and-shell-scripting-using-pipes-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/php-and-shell-scripting-using-pipes-page-2\/","title":{"rendered":"PHP and Shell Scripting: Using Pipes Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Peter Shaw<\/div>\n<div class=\"\">on February 12, 2009<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articlePara\">\nAs you can see the outputs are very similar, but not identical, however this is not an article about shell programming in general, it&#8217;s about putting PHP to some advanced uses.  You can see from the examples above that commands are joined with the vertical bar character  &#8216;|&#8217;  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.\n<\/div>\n<div class=\"articlePara\">\nThis means if we read the standard input stream using the php file stream &#8216;php:\/\/stdin&#8217; then we can use PHP to construct blocks of code that can participate in the filter chain.\n    <\/div>\n<div class=\"articleHeader\">\nEnough talking already, give us some code!!!!\n    <\/div>\n<div class=\"articlePara\">\nOk ok, I hear you.  So by way of an example to show what I??m on about, we&#8217;ll put together a small example under Linux that shows what those file permission flags in a file listing mean.\n    <\/div>\n<div class=\"articlePara\">\nFirst, if we do an &#8216;ls -al&#8217; we should see something like this:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\ndrwx------ \u00a02 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2006-04-10 23:51 .ssh\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0193 2007-10-25 10:43 staaus.cap\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a011627 2007-10-27 14:05 staceymail2\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a018737 2007-10-28 00:24 staceymail3\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a034040 2007-10-28 16:10 staceymail4\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0182891 2007-10-26 09:28 stacymail\n-rw-r--r-- \u00a01 shawty shawty \u00a0\u00a0\u00a0\u00a03642 2008-10-29 17:02 startup\n-rw-r--r-- \u00a01 shawty shawty \u00a0\u00a0\u00a0\u00a04124 2008-10-29 17:03 startup-org\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a055 2006-06-03 01:51 suck.newrc\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a050 2006-06-03 01:47 sucknewsrc\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00 2008-05-15 17:20 .sudo_as_admin_successful\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a023067 2006-10-12 22:34 telnet.pl\n-rw-r--r-- \u00a01 shawty users \u00a0\u00a09231834 2006-04-18 14:21 termine\n&lt;\/code&gt;\n<\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nThe first thing we need to do here is to read this into a PHP script.\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\n&lt;?php\n\n \u00a0\/\/ Open PHP's standard input stream\n \u00a0$input_stream = fopen(\"php:\/\/stdin\",\"r\");\n\n \u00a0\/\/ Array to store the received data\n \u00a0$lines = array();\n\n \u00a0\/\/ Loop &amp; process while we receive lines\n \u00a0while($line = fgets($input_stream,4096)) \/\/ Note 4k lines, should be ok for most purposes\n \u00a0{\n \u00a0\u00a0\u00a0\/\/Store each line in the array, ensuring we chop off the line ends\n \u00a0\u00a0\u00a0$lines[] = trim($line);\n \u00a0}\n\n \u00a0fclose($input_stream);\n\n \u00a0print_r($lines);\n\n?&gt;\n&lt;\/code&gt;\n    <\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nIf you run this from a Linux command line using &#8216;ls -al | php -q fancydir.php&#8217; (replace php file name as required), you&#8217;ll get something like the following:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\nshawty@poweredge:~\/Articles_PHP$ ls -al ..\/ | php -q fancydir.php\nArray\n(\n \u00a0\u00a0\u00a0[0] =&gt; total 54676\n \u00a0\u00a0\u00a0[1] =&gt; drwxr-xr-x 46 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2009-01-29 20:01 .\n \u00a0\u00a0\u00a0[2] =&gt; drwxr-xr-x 14 root \u00a0\u00a0root \u00a0\u00a0\u00a0\u00a0\u00a0\u00a04096 2009-01-13 17:17 ..\n \u00a0\u00a0\u00a0[3] =&gt; -rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01 2006-11-15 02:24 2\n \u00a0\u00a0\u00a0[4] =&gt; drwxr-xr-x 10 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2008-05-14 17:11 20gdrive\n \u00a0\u00a0\u00a0[5] =&gt; drwxr-x--- \u00a06 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2007-04-30 16:14 4grec1\n \u00a0\u00a0\u00a0[6] =&gt; drwx------ \u00a02 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2006-05-14 17:31 a.b.p.v.files\n \u00a0\u00a0\u00a0[7] =&gt; -rw-r--r-- \u00a01 shawty users \u00a0\u00a01863991 2006-03-06 14:24 abs-guide.pdf\n \u00a0\u00a0\u00a0[8] =&gt; -rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00 2006-05-23 22:42 .addressbook\n \u00a0\u00a0\u00a0[9] =&gt; -rw------- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a02285 2006-05-23 22:42 .addressbook.lu\n \u00a0\u00a0\u00a0[10] =&gt; drwx------ \u00a02 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2008-05-15 17:42 .aptitude\n \u00a0\u00a0\u00a0[11] =&gt; drwxr-xr-x \u00a02 shawty shawty \u00a0\u00a0\u00a0\u00a04096 2009-01-29 20:01 Articles_PHP\n \u00a0\u00a0\u00a0[12] =&gt; drwxr-xr-x \u00a03 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2006-06-11 01:34 baks\n \u00a0\u00a0\u00a0[13] =&gt; -rw------- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a015190 2009-01-29 12:56 .bash_history\n \u00a0\u00a0\u00a0[14] =&gt; -rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0220 2008-05-15 16:41 .bash_logout\n \u00a0\u00a0\u00a0[15] =&gt; -rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a02298 2008-05-15 16:41 .bashrc\n \u00a0\u00a0\u00a0[16] =&gt; drwxr-xr-x \u00a02 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2006-08-07 14:39 belkinbt\n \u00a0\u00a0\u00a0[17] =&gt; drwx------ \u00a02 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2006-08-22 21:16 .cedit\n \u00a0==SNIP==\n&lt;\/code&gt;\n    <\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nAs 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:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\n$count = 1;\nforeach($lines as $line)\n{\n \u00a0print $count . \" : \" . $line . \"n\";\n \u00a0$count++;\n}\n&lt;\/code&gt;\n    <\/pre>\n<p>    <\/code>\n    <\/div>\n<div class=\"articlePara\">\nThis would give you a number listing something like the following:\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;code&gt;\n1 : drwxr-xr-x 46 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2009-01-29 20:01 .\n2 : drwxr-xr-x 14 root \u00a0\u00a0root \u00a0\u00a0\u00a0\u00a0\u00a0\u00a04096 2009-01-13 17:17 ..\n3 : -rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01 2006-11-15 02:24 2\n4 : drwxr-xr-x 10 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2008-05-14 17:11 20gdrive\n5 : drwxr-x--- \u00a06 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2007-04-30 16:14 4grec1\n6 : drwx------ \u00a02 shawty users \u00a0\u00a0\u00a0\u00a0\u00a04096 2006-05-14 17:31 a.b.p.v.files\n7 : -rw-r--r-- \u00a01 shawty users \u00a0\u00a01863991 2006-03-06 14:24 abs-guide.pdf\n8 : -rw-r--r-- \u00a01 shawty users \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00 2006-05-23 22:42 .addressbook\n&lt;\/code&gt;\n    <\/pre>\n<p>    <\/code>\n    <\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"peter_shaw20090211.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"peter_shaw20090211.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"peter_shaw200902119ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"peter_shaw200902119ba9.html?page=3\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Following Darrell Brogdon&#8217;s previous article on using PHP as a shell scripting language, this article covers some advanced<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1295","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1295"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1295\/revisions"}],"predecessor-version":[{"id":3187,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1295\/revisions\/3187"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}