php cli using STDIN and Piping

http://php.net/manual/en/features.commandline.options.php
------------------------------------------


-r <code>        Run PHP <code> without using script tags <?..?>

例:

1.  cat xxx.log | php -r 'echo stream_get_contents(STDIN)."\n";'

// STDIN is just a shortcut of $fh = fopen("php://stdin", "r"); 

2. cat access.log | php -r 'echo readfile("php://stdin")."\n";'

 

3. read  by line:
cat access.log | php -r 'while (!feof(STDIN)){ echo fgets(STDIN); };'

---------------------------------------

-R <code>        Run PHP <code> for every input line
-R--process-code

PHP code to execute for every input line. Added in PHP 5.

There are two special variables available in this mode: $argn and $argi. $argn will contain the line PHP is processing at that moment, while $argi will contain the line number.

例:

cat /xxx.log | php -R 'echo $argi . ":" . $argn . "\n";'

 

============================

-f <file>        Parse and execute <file>.
-F <file>        Parse and execute <file> for every input line

例:

If you want it to show up in $argv, try this:

echo "Whatever you want" | xargs php script.php

That would covert whatever goes into standard input into command line arguments.

=================================

http://stackoverflow.com/questions/5891888/piping-data-into-command-line-php

转载于:https://my.oschina.net/u/144160/blog/714799

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值