On the Linux platform, the php Command Line program processes pipeline data, and the linux Pipeline

Source: Internet
Author: User
Tags couchdb

On the Linux platform, the php Command Line program processes pipeline data, and the linux Pipeline

This document describes how to use the php Command Line program on the Linux platform to process pipeline data. We will share this with you for your reference. The details are as follows:

There is a powerful command in linux | (pipeline prompt ). it is used to give the result of the previous command to the next command and use it as the input of the next command. most commands in linux also support this method. however, after writing a php Command Line applet, I am stuck with the problem of obtaining the result of the previous command. does php not support such operations?

So I began to ask Uncle google again. when I look for it, it's all about the php Command Line mode, and there is no information related to the problem I want to know. is it my keyword used incorrectly? Or the legendary RP problem? When google was fruitless, he began to look for help. who of my php friends I know may have run command programs and then asked. I am not familiar with this problem. it's fruitless again, ah, it's so anxious. it seems that I can only rely on myself, so I began to save myself. so I started to study the argc and argv of php. start to calm down and think about how it happened. I vaguely remember that when I used to play perl, I used '<>' to get the input. '<>' indicates the meaning of reading from the standard input. think about the role of a pipe operator instead of entering something. the idea was opened, so we had 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.phpaaaaa

Haha, it's done! However, there is a small problem:

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

Only one row can be read at a time! It seems that fgets is a problem. How can I read all the content at once? When using php, I know that if you want to put the content of a file in a variable rather than an array, use: file_get_contents. but it operates on files and does not know whether it is suitable for standard input. I don't know if I can try it. result:

[rainbird@rainbird-desktop ~]$cat a.php#!/usr/bin/php -q<?phpecho file_get_contents('php://stdin');?>[rainbird@rainbird-desktop ~]$tail /etc/passwd|./a.phpcouchdb:x:106:113:CouchDB Administrator,,,:/var/lib/couchdb:/bin/bashhaldaemon:x:107:114:Hardware abstraction layer,,,:/var/run/hald:/bin/falsespeech-dispatcher:x:108:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/shkernoops:x:109:65534:Kernel Oops Tracking Daemon,,,:/:/bin/falsesaned:x:110:116::/home/saned:/bin/falsepulse:x:111:117:PulseAudio daemon,,,:/var/run/pulse:/bin/falsegdm:x:112:119:Gnome Display Manager:/var/lib/gdm:/bin/falserainbird:x:1000:1000:rainbird,,,:/home/rainbird:/bin/bashsshd:x:113:65534::/var/run/sshd:/usr/sbin/nologinpostfix:x:114:122::/var/spool/postfix:/bin/false

Really handsome! Just a few simple lines, php simply implements the cat function:

[rainbird@rainbird-desktop ~]$./a.php </etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/shsys:x:3:3:sys:/dev:/bin/shsync:x:4:65534:sync:/bin:/bin/sync......

I started my sincere thanks to php. php has also begun to integrate the idea that everything is a file in linux, and can actually process the standard input as a file. think carefully: in fact, this is a simple small problem, just because you have not done such a thing, and your thinking is limited. When you come into contact with such a demand, you don't know how to think. if you use perl, python, and ruby as programs, you may need to know how to deal with them. This small experience once again proves that the program is the expression of an algorithm, and the algorithm is the soul, there is a way out if you have ideas.

Related Article

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.