【发布时间】:2016-08-08 13:58:14
【问题描述】:
我正在尝试以下示例,例如:
$ php -r 'require_once($argv[1]);' <(echo "hello")
或:
$ php -r 'file_get_contents($argv[1]);' <(echo "hello")
都失败了:
PHP 警告:require_once(/dev/fd/63):打开流失败:第 1 行的命令行代码中没有这样的文件或目录
PHP 警告:file_get_contents(/dev/fd/63):未能打开流:第 1 行的命令行代码中没有此类文件或目录
或:
$ php -r 'file_get_contents($argv[0]);' < <(echo "hello")
失败:
PHP 致命错误:require_once(): Failed opening required '-' (include_path='.:/usr/share/pear:/usr/share/php') 在第 1 行的命令行代码中
以上尝试的灵感来自drush命令,例如:
$ drush --early=<(echo print 123';') ""
[warning] require_once(/dev/fd/63): failed to open stream: No such file or directory preflight.inc:58
我可以从文件描述符中注入动态 PHP 代码(无需每次都创建单独的文件),以便在引导主代码之前执行代码。
其他类似的命令工具也能正常工作:
$ cat <(echo "hello")
hello
或:
$ python -c "import sys; print sys.stdin.readlines()" < <(echo "hello")
['hello\n']
我找到了这个PHP bug 和this one,但这些早就修复了,我使用的是 5.6.22。
当从 CLI 调用时,我有什么方法可以欺骗 PHP 从进程替换中读取数据(从文件描述符中读取,例如/dev/fd),通过使用一些简单的单线?
【问题讨论】:
标签: php shell command-line-interface stdin process-substitution