通过php将stdin导入shell脚本
我们有一个命令行php应用程序,它维护特殊权限,并希望使用它将管道数据中继到shell脚本中 我知道我们可以通过以下方式阅读STDIN:通过php将stdin导入shell脚本,php,pipe,stdin,shell-exec,Php,Pipe,Stdin,Shell Exec,我们有一个命令行php应用程序,它维护特殊权限,并希望使用它将管道数据中继到shell脚本中 我知道我们可以通过以下方式阅读STDIN: while(!feof(STDIN)){ $line = fgets(STDIN); } 但是如何将STDIN重定向到shell脚本中呢 STDIN太大,无法加载到内存中,因此我无法执行以下操作: shell_exec("echo ".STDIN." | script.sh"); 正如@Devon所说,popen/pclose在这里非常有用 $sc
while(!feof(STDIN)){
$line = fgets(STDIN);
}
但是如何将STDIN重定向到shell脚本中呢
STDIN太大,无法加载到内存中,因此我无法执行以下操作:
shell_exec("echo ".STDIN." | script.sh");
正如@Devon所说,
popen
/pclose
在这里非常有用
$scriptHandle = popen("./script.sh","w");
while(($line = fgets(STDIN)) !== false){
fputs($scriptHandle,$line);
}
pclose($scriptHandle);
或者,类似于
fputs($scriptHandle,file\u get\u contents('php://stdin"));对于较小的文件,code>可能会取代逐行方法。正如@Devon所说,popen
/pclose
在这里非常有用
$scriptHandle = popen("./script.sh","w");
while(($line = fgets(STDIN)) !== false){
fputs($scriptHandle,$line);
}
pclose($scriptHandle);
或者,类似于fputs($scriptHandle,file\u get\u contents('php://stdin"));对于较小的文件,code>可能会取代逐行处理的方法。使用xenon对popen的回答似乎可以解决这个问题
// Open the process handle
$ph = popen("./script.sh","w");
// This puts it into the file line by line.
while(($line = fgets(STDIN)) !== false){
// Put in line from STDIN. (Note that you may have to use `$line . '\n'`. I don't know
fputs($ph,$line);
}
pclose($ph);
将氙气的答案用于波本似乎可以达到目的
// Open the process handle
$ph = popen("./script.sh","w");
// This puts it into the file line by line.
while(($line = fgets(STDIN)) !== false){
// Put in line from STDIN. (Note that you may have to use `$line . '\n'`. I don't know
fputs($ph,$line);
}
pclose($ph);
好吧,这应该执行而不是编写,但你的代码给了我一个想法。使用popen而不是fopen似乎可以做到这一点。谢谢。好吧,这应该执行而不是编写,但你的代码给了我一个想法。使用popen而不是fopen似乎可以做到这一点。谢谢