Linux Platform PHP command-line program method for processing pipeline data _php tips

Source: Internet
Author: User
Tags stdin couchdb

This article illustrates how the Linux platform PHP command-line program handles pipeline data. Share to everyone for your reference, specific as follows:

Linux has a powerful command | (Pipeline prompt). Its role is to give the result of the previous command to the latter command and as input to the latter command. Most of the commands under Linux also support this approach. But when I finish writing a PHP command line applet, There was a deadlock over how to get the result of the previous order. Don't PHP support such an operation?

And then began to ask Google uncle. Looking for, is to say that the PHP command line mode is going on, there is no and I want to know the relevant information. Is my key word wrong? Or is the legendary RP problem? began to ask for help. Among the friends I know who do PHP, who might have made the order, and then asked. Ask and ask if they have never played or are unfamiliar. Again no fruit, ah, really nasty dead. It seems that only on their own, and began to save themselves. So start to study PHP argc and argv, try to try not to. Start to calm down and think about what's going on. Vaguely remember when I used to play Perl, want to get input with a ' <> ' to complete. and ' <> ' means reading from the standard input. Think again. The function of the pipe is not to replace the input of the person. The idea opened, so there is the following small program:

[Rainbird@rainbird-desktop ~] $cat a.php
#!/usr/bin/php-q
<?php
$fh = fopen (' Php://stdin ', ' R ');
Echo fgets ($FH);
Fclose ($FH);
? >
[rainbird@rainbird-desktop ~] $echo "AAAAA" |. /a.php
AAAAA

Oh, it's done! But there is one small problem:

[Rainbird@rainbird-desktop ~] $tail/etc/passwd|. /a.php
couchdb:x:106:113:couchdb Administrator,,,:/var/lib/couchdb:/bin/bash

You can read only one line at a time! It seems to be fgets's problem. How can you read all the content at once? In the process of using PHP, I know that if you want to put the contents of a file in a variable instead of an array, use the words: file_get_ Contents. But it is the operation of the file, do not know whether it is suitable for standard input. I don't know, I can try it. See results:

[Rainbird@rainbird-desktop ~] $cat a.php
#!/usr/bin/php-q
<?php
echo file_get_contents (' php:// StdIn ');
? >
[rainbird@rainbird-desktop ~] $tail/etc/passwd|. /a.php
couchdb:x:106:113:couchdb Administrator,,,:/var/lib/couchdb:/bin/bash
haldaemon:x:107:114: Hardware abstraction Layer,,,:/var/run/hald:/bin/false
Speech-dispatcher:x:108:29:speech Dispatcher,,,:/var/ Run/speech-dispatcher:/bin/sh
Kernoops:x:109:65534:kernel Oops tracking Daemon,,,:/:/bin/false
saned:x : 110:116::/home/saned:/bin/false
pulse:x:111:117:pulseaudio daemon,,,:/var/run/pulse:/bin/false
gdm:x : 112:119:gnome Display manager:/var/lib/gdm:/bin/false
rainbird:x:1000:1000:rainbird,,,:/home/rainbird:/bin /bash
sshd:x:113:65534::/var/run/sshd:/usr/sbin/nologin
postfix:x:114:122::/var/spool/postfix:/bin/ False

It's gorgeous! It's a simple line, and PHP simply implements the function of cat:

[Rainbird@rainbird-desktop ~]$./a.php </etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1: Daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
Sync:x:4:65534:sync :/bin:/bin/sync ...


The beginning of a heartfelt admiration for PHP. PHP is also beginning to incorporate the idea of all the files in Linux, so that the standard input can be processed as a file. Think carefully: This is a simple little problem, just because there is no such a thing, thinking is limited, When it comes to such a demand, I don't know what to think. The students with Perl,python,ruby to do the program to meet this need to know how to deal with it: This small experience again confirmed: The program is the expression of the algorithm, the algorithm is the soul, there is a way out.

For more information about PHP interested readers can view the site topics: "Php+mysql database operations to get Started", "PHP basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", " PHP array Operation tips Encyclopedia, PHP string (String) Usage summary and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.