When using fgets, it can block bash scripts if stdin not installed or is not empty, including when using the error control operator @ php .
#!/usr/bin/php <?php $pipe = @trim(fgets(STDIN));
This behavior can be avoided by setting stream_set_blocking in the php header:
#!/usr/bin/php <?php stream_set_blocking(STDIN, 0); $pipe = @trim(fgets(STDIN)); // Script was called with an empty stdin // No errors or warnings, continue echo $pipe . "!";
As an example, it will be called as follows:
echo "Hello world" | ./myPHPscript // Output "Hello world!" ./myPHPscript // Output "!"
Cryptopat Mar 05 '18 at 11:50 2018-03-05 11:50
source share