0

So I have this script that changes the color of the text in the console. The problem is that although it accepts a string from both STDIN and the -s argument, it will occasionally leave out STDIN in the result. I have only noticed this when I pipe the script to itself (see below)

php color.php -s Hello, -c green -S underline | php color.php -s " Bob" -c red -S bold

The bash script above would always show " Bob" but would only show "Hello," a few times in a series of tries, without changing any part of the script. As far as I can tell, whether it includes it or not is very random, there is no noticeable pattern.

So my question is, how do I prevent this?

4

1 回答 1

1

读取非阻塞可能还没有数据。如果输入不是交互式终端,另一种选择是仅读取 STDIN:

<?php
$args = getopt('s:');
if(!posix_isatty(STDIN)){
        $stdin = file_get_contents('php://stdin');
} else {
        $stdin ='';
}
echo $stdin.' '.$args['s'];
于 2013-05-01T18:52:33.253 回答