不同语言从shell管道获取数据的方法
shell:
#!/bin/bash #========================================================== # this example show you how to get data from pipe #========================================================== while read line do echo $line done
php:
#!/usr/bin/env php <?php #======================================================== #testing how to get data from pipe #======================================================== $fp=fopen('php://stdin','r'); $result=''; while(!feof($fp)){ $result.=fgets($fp,128); } fclose($fp); echo $result;
python:
#!/usr/bin/env python #coding:utf-8 import sys #line=sys.stdin.readline() #while line: # print line # line=sys.stdin.readline() while True: line=sys.stdin.readline() if not line: break print line