使用命名管道(包括有关php语法错误的命令)

时间:2019-12-19 15:15:58

标签: php bash

$ cat a.php
<?php
system("echo <(ls)");

此脚本。

$ php a.php
sh: 1: Syntax error: "(" unexpected

我执行脚本。 但是出现了语法错误。

$ echo <(ls)
/dev/fd/63

成功获得成功。 我不知道该怎么办。

1 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow!


sh: 1: Syntax error: "(" unexpected

此错误通常是针对不支持<()表达式管道的非bash shell出现的。即使sh是bash,它也可以在没有进程替代的情况下以更大的兼容性运行。

使用bash可以正常工作。

$ echo <(ls)
/dev/fd/63

解决方案

解决方案#1

脚本应始终与带有/bin/bash -c的bash一起运行。

system('/bin/bash -c "echo <(ls)"');

解决方案2

通过可执行脚本。

execute.sh

#!/bin/bash

echo <(ls)

a.php

<?php 

exec('execute.sh');