FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Pipe the content of a variable to a process
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Pipe the content of a variable to a process [message #173018] Thu, 17 March 2011 19:27 Go to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
Hi,

how can I pipe the content of a variable into a process' standard input and
save its output into another variable?

To make it clear what I want to achieve here's a small bash example that
processes the data »foo« with the process »sed« and writes the output into the
variable OUT.

IN='foo'
OUT=`echo $IN | sed 's/o/a/g'`

I need to do the same in PHP, pipe the data from $IN (a PHP variable) through
a process into $OUT (also a PHP variable).


Marco
Re: Pipe the content of a variable to a process [message #173021 is a reply to message #173018] Thu, 17 March 2011 21:35 Go to previous messageGo to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
On 2011-03-17 Marco <netuse(at)lavabit(dot)com> wrote:

> Hi,
>
> how can I pipe the content of a variable into a process' standard input and
> save its output into another variable?
> [...]

I made some progress. But I still have problems. In the following example I
can use »cat« as process without problems. But gnuplot produces no output, but
a return value of zero, that means it's successfull. But where's the output?

When I call gnuplot with the »passthru« command it works as expected that
means that it is in the PATH and produces output on stdout with the given
input. What's the problem here?

<?php
$stdin_data = 'set terminal svg;plot sin(x)';

$descriptorspec = array(
0 => array( 'pipe', 'r' ), // stdin to read from
1 => array( 'pipe', 'w' ), // stdout to write to
2 => array( 'pipe', 'a' ), // stderr
);

// This works
// $process = proc_open( 'cat', $descriptorspec, $pipes );

// This doesn't produce output
$process = proc_open( 'gnuplot', $descriptorspec, $pipes );

if ( is_resource( $process ) ) {
fwrite( $pipes[0], $stdin_data );
fclose( $pipes[0] );
$content = stream_get_contents( $pipes[1] );
fclose( $pipes[1] );
fclose( $pipes[2] );

$return_value = proc_close( $process );

// cat and gnuplot return 0
echo $return_value . "\n";
echo $content;

// This works
// passthru( "gnuplot -e '$stdin_data'" );
}
?>


Marco
Re: Pipe the content of a variable to a process [message #173024 is a reply to message #173021] Fri, 18 March 2011 02:30 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/17/2011 5:35 PM, Marco wrote:
> On 2011-03-17 Marco<netuse(at)lavabit(dot)com> wrote:
>
>> Hi,
>>
>> how can I pipe the content of a variable into a process' standard input and
>> save its output into another variable?
>> [...]
>
> I made some progress. But I still have problems. In the following example I
> can use »cat« as process without problems. But gnuplot produces no output, but
> a return value of zero, that means it's successfull. But where's the output?
>
> When I call gnuplot with the »passthru« command it works as expected that
> means that it is in the PATH and produces output on stdout with the given
> input. What's the problem here?
>
> <?php
> $stdin_data = 'set terminal svg;plot sin(x)';
>
> $descriptorspec = array(
> 0 => array( 'pipe', 'r' ), // stdin to read from
> 1 => array( 'pipe', 'w' ), // stdout to write to
> 2 => array( 'pipe', 'a' ), // stderr
> );
>
> // This works
> // $process = proc_open( 'cat', $descriptorspec, $pipes );
>
> // This doesn't produce output
> $process = proc_open( 'gnuplot', $descriptorspec, $pipes );
>
> if ( is_resource( $process ) ) {
> fwrite( $pipes[0], $stdin_data );
> fclose( $pipes[0] );
> $content = stream_get_contents( $pipes[1] );
> fclose( $pipes[1] );
> fclose( $pipes[2] );
>
> $return_value = proc_close( $process );
>
> // cat and gnuplot return 0
> echo $return_value . "\n";
> echo $content;
>
> // This works
> // passthru( "gnuplot -e '$stdin_data'" );
> }
> ?>
>
>
> Marco
>

What's in stderr? What happens if you write stdout and stderr to a file
(for testing)?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Pipe the content of a variable to a process [message #173032 is a reply to message #173024] Fri, 18 March 2011 11:42 Go to previous messageGo to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
On 2011-03-17 Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

>> I made some progress. But I still have problems. In the following example
>> I can use »cat« as process without problems. But gnuplot produces no
>> output, but a return value of zero, that means it's successfull. But
>> where's the output?
>>
>> When I call gnuplot with the »passthru« command it works as expected that
>> means that it is in the PATH and produces output on stdout with the given
>> input. What's the problem here?
>>
>> [some code]
>>
>> Marco
>>
>
> What's in stderr? What happens if you write stdout and stderr to a file
> (for testing)?

They are both empty.


Marco
Re: Pipe the content of a variable to a process [message #173034 is a reply to message #173032] Fri, 18 March 2011 12:18 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/18/2011 7:42 AM, Marco wrote:
> On 2011-03-17 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>
>>> I made some progress. But I still have problems. In the following example
>>> I can use »cat« as process without problems. But gnuplot produces no
>>> output, but a return value of zero, that means it's successfull. But
>>> where's the output?
>>>
>>> When I call gnuplot with the »passthru« command it works as expected that
>>> means that it is in the PATH and produces output on stdout with the given
>>> input. What's the problem here?
>>>
>>> [some code]
>>>
>>> Marco
>>>
>>
>> What's in stderr? What happens if you write stdout and stderr to a file
>> (for testing)?
>
> They are both empty.
>
>
> Marco
>


Well, one of the things I see is you're using the -e flag with
passthru() but not with proc_open(). That's a huge difference.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Pipe the content of a variable to a process [message #173035 is a reply to message #173034] Fri, 18 March 2011 13:02 Go to previous messageGo to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
On 2011-03-18 Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

>>> What's in stderr? What happens if you write stdout and stderr to a file
>>> (for testing)?
>>
>> They are both empty.
>>
>>
>> Marco
>>
>
>
> Well, one of the things I see is you're using the -e flag with
> passthru() but not with proc_open(). That's a huge difference.

It's basically the same. Without »-e« the input goes to standard input, with
»-e« it goes as a command line input. I think, the problem here is not the
input, since the return value is zero, it is the missing output.

passthru( "echo 'set terminal svg;plot sin(x)' | gnuplot" );

The line above also produces the desired output.


Marco
Re: Pipe the content of a variable to a process [message #173036 is a reply to message #173035] Fri, 18 March 2011 13:21 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/18/2011 9:02 AM, Marco wrote:
> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>
>>>> What's in stderr? What happens if you write stdout and stderr to a file
>>>> (for testing)?
>>>
>>> They are both empty.
>>>
>>>
>>> Marco
>>>
>>
>>
>> Well, one of the things I see is you're using the -e flag with
>> passthru() but not with proc_open(). That's a huge difference.
>
> It's basically the same. Without »-e« the input goes to standard input, with
> »-e« it goes as a command line input. I think, the problem here is not the
> input, since the return value is zero, it is the missing output.
>
> passthru( "echo 'set terminal svg;plot sin(x)' | gnuplot" );
>
> The line above also produces the desired output.
>
>
> Marco
>

I understand that produces the correct output. But from what I read in
the doc, -e says to use stdin - which is what you're trying to use.
Without -e it goes to the command line, which you are not doing. So
it'd doing exactly what you tell it to do - nothing.

I suggest you ask the gnuplot people. I don't think the problem is the
output stream is failing - I think your commands are incorrect.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Pipe the content of a variable to a process [message #173037 is a reply to message #173036] Fri, 18 March 2011 14:05 Go to previous messageGo to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
On 2011-03-18 Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

> I understand that produces the correct output. But from what I read in
> the doc, -e says to use stdin - which is what you're trying to use.
> Without -e it goes to the command line, which you are not doing. So
> it'd doing exactly what you tell it to do - nothing.

That's incorrect. Without »-e« gnuplot reads from stdin, with »-e« from the
command line. You can check this.

gnuplot -e 'set terminal svg;plot sin(x)'
echo 'set terminal svg;plot sin(x)' | gnuplot

give both the same output.

> I suggest you ask the gnuplot people. I don't think the problem is the
> output stream is failing - I think your commands are incorrect.

I know gnuplot pretty well. Without arguments it can be used directly in a
pipe (with proper input on stdin, of course). It would be difficult to get
help in the gnuplot group, since it works on the command line.


Marco
Re: Pipe the content of a variable to a process [message #173042 is a reply to message #173037] Fri, 18 March 2011 17:08 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/18/2011 10:05 AM, Marco wrote:
> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>
>> I understand that produces the correct output. But from what I read in
>> the doc, -e says to use stdin - which is what you're trying to use.
>> Without -e it goes to the command line, which you are not doing. So
>> it'd doing exactly what you tell it to do - nothing.
>
> That's incorrect. Without »-e« gnuplot reads from stdin, with »-e« from the
> command line. You can check this.
>
> gnuplot -e 'set terminal svg;plot sin(x)'
> echo 'set terminal svg;plot sin(x)' | gnuplot
>
> give both the same output.
>
>> I suggest you ask the gnuplot people. I don't think the problem is the
>> output stream is failing - I think your commands are incorrect.
>
> I know gnuplot pretty well. Without arguments it can be used directly in a
> pipe (with proper input on stdin, of course). It would be difficult to get
> help in the gnuplot group, since it works on the command line.
>
>
> Marco
>

OK, I read that backwards

But the pipes in PHP work fine - I've used them successfully before.

Is this binary data which is being passed back? Also, are you running
this as using CLI or is it part of a web page?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Pipe the content of a variable to a process [message #173044 is a reply to message #173042] Fri, 18 March 2011 17:27 Go to previous messageGo to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
On 2011-03-18 Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

>>> I suggest you ask the gnuplot people. I don't think the problem is the
>>> output stream is failing - I think your commands are incorrect.
>>
>> I know gnuplot pretty well. Without arguments it can be used directly in a
>> pipe (with proper input on stdin, of course). It would be difficult to get
>> help in the gnuplot group, since it works on the command line.
>>
>>
>> Marco
>>
>
> OK, I read that backwards
>
> But the pipes in PHP work fine - I've used them successfully before.
>
> Is this binary data which is being passed back?

No, it's text (svg image).

> Also, are you running this as using CLI or is it part of a web page?

It's part of a web page. However I made a minimal example where I removed all
unnecessary stuff to minimize interference with other code.


Marco
Re: Pipe the content of a variable to a process [message #173045 is a reply to message #173044] Fri, 18 March 2011 18:46 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/18/2011 1:27 PM, Marco wrote:
> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>
>>>> I suggest you ask the gnuplot people. I don't think the problem is the
>>>> output stream is failing - I think your commands are incorrect.
>>>
>>> I know gnuplot pretty well. Without arguments it can be used directly in a
>>> pipe (with proper input on stdin, of course). It would be difficult to get
>>> help in the gnuplot group, since it works on the command line.
>>>
>>>
>>> Marco
>>>
>>
>> OK, I read that backwards
>>
>> But the pipes in PHP work fine - I've used them successfully before.
>>
>> Is this binary data which is being passed back?
>
> No, it's text (svg image).
>
>> Also, are you running this as using CLI or is it part of a web page?
>
> It's part of a web page. However I made a minimal example where I removed all
> unnecessary stuff to minimize interference with other code.
>
>
> Marco
>

Are you running your test from the CLI? Or are you trying to run it as
part of the web page?

If the latter - are you putting out valid html? Have you looked at the
document source to see what's there?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Pipe the content of a variable to a process [message #173046 is a reply to message #173045] Fri, 18 March 2011 19:21 Go to previous messageGo to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
On 2011-03-18 Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

> Are you running your test from the CLI? Or are you trying to run it as
> part of the web page?
>
> If the latter - are you putting out valid html? Have you looked at the
> document source to see what's there?

At first I tried putting it into a (valid) web page. During the creation of
the minimal example I removed all the html stuff. I sent the complete script
in my second post in this thread. When I look directly at the source I just
see the »0« (echo $return_value . "\n";). And when I redirect stdout and
stderr to a file there's a »0« in file_stdout and file_stderr is empty.

I have absolutely no clue what's going on here, since it works with »cat« as a
process and gnuplot works with »passthru«. That »cat« works means that my
workflow with the pipes is right and that gnuplot works with »passthru« means
that gnuplot is working inside PHP.

Thanks for your help, I hope we can get it solved.

Marco
Re: Pipe the content of a variable to a process [message #173047 is a reply to message #173046] Fri, 18 March 2011 19:45 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/18/2011 3:21 PM, Marco wrote:
> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>
>> Are you running your test from the CLI? Or are you trying to run it as
>> part of the web page?
>>
>> If the latter - are you putting out valid html? Have you looked at the
>> document source to see what's there?
>
> At first I tried putting it into a (valid) web page. During the creation of
> the minimal example I removed all the html stuff. I sent the complete script
> in my second post in this thread. When I look directly at the source I just
> see the »0« (echo $return_value . "\n";). And when I redirect stdout and
> stderr to a file there's a »0« in file_stdout and file_stderr is empty.
>
> I have absolutely no clue what's going on here, since it works with »cat« as a
> process and gnuplot works with »passthru«. That »cat« works means that my
> workflow with the pipes is right and that gnuplot works with »passthru« means
> that gnuplot is working inside PHP.
>
> Thanks for your help, I hope we can get it solved.
>
> Marco
>

It sounds like gnuplot isn't getting the input properly then.

What happens if you execute gnuplot from the command line with the input
as you sent it, i.e.

gnuplot
set terminal svg;plot sin(x);

And does gnuplot require a nl ("\n") character at the end of stdin?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Pipe the content of a variable to a process [message #173048 is a reply to message #173046] Fri, 18 March 2011 19:54 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/18/2011 3:21 PM, Marco wrote:
> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>
>> Are you running your test from the CLI? Or are you trying to run it as
>> part of the web page?
>>
>> If the latter - are you putting out valid html? Have you looked at the
>> document source to see what's there?
>
> At first I tried putting it into a (valid) web page. During the creation of
> the minimal example I removed all the html stuff. I sent the complete script
> in my second post in this thread. When I look directly at the source I just
> see the »0« (echo $return_value . "\n";). And when I redirect stdout and
> stderr to a file there's a »0« in file_stdout and file_stderr is empty.
>
> I have absolutely no clue what's going on here, since it works with »cat« as a
> process and gnuplot works with »passthru«. That »cat« works means that my
> workflow with the pipes is right and that gnuplot works with »passthru« means
> that gnuplot is working inside PHP.
>
> Thanks for your help, I hope we can get it solved.
>
> Marco
>

OK, I downloaded and installed gnuplot on my system. Change this line:

$stdin_data = "set terminal svg;plot sin(x)\n";

Note the newline char at the end.

It works for me.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Pipe the content of a variable to a process [message #173049 is a reply to message #173048] Fri, 18 March 2011 20:09 Go to previous messageGo to next message
Marco is currently offline  Marco
Messages: 11
Registered: March 2011
Karma: 0
Junior Member
On 2011-03-18 Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

> On 3/18/2011 3:21 PM, Marco wrote:
>> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>>
>>> Are you running your test from the CLI? Or are you trying to run it as
>>> part of the web page?
>>>
>>> If the latter - are you putting out valid html? Have you looked at the
>>> document source to see what's there?
>>
>> At first I tried putting it into a (valid) web page. During the creation
>> of the minimal example I removed all the html stuff. I sent the complete
>> script in my second post in this thread. When I look directly at the
>> source I just see the »0« (echo $return_value . "\n";). And when I
>> redirect stdout and stderr to a file there's a »0« in file_stdout and
>> file_stderr is empty.
>>
>> I have absolutely no clue what's going on here, since it works with »cat«
>> as a process and gnuplot works with »passthru«. That »cat« works means
>> that my workflow with the pipes is right and that gnuplot works with
>> »passthru« means that gnuplot is working inside PHP.
>>
>> Thanks for your help, I hope we can get it solved.
>>
>> Marco
>>
>
> OK, I downloaded and installed gnuplot on my system. Change this line:
>
> $stdin_data = "set terminal svg;plot sin(x)\n";
>
> Note the newline char at the end.

Thats it!!!!! Million thanks.

How can that be?! In the shell it works without »\n«. Anyway, thanks for the
effort you put into solving my problem.


Marco
Re: Pipe the content of a variable to a process [message #173051 is a reply to message #173049] Fri, 18 March 2011 21:12 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/18/2011 4:09 PM, Marco wrote:
> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>
>> On 3/18/2011 3:21 PM, Marco wrote:
>>> On 2011-03-18 Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>>>
>>>> Are you running your test from the CLI? Or are you trying to run it as
>>>> part of the web page?
>>>>
>>>> If the latter - are you putting out valid html? Have you looked at the
>>>> document source to see what's there?
>>>
>>> At first I tried putting it into a (valid) web page. During the creation
>>> of the minimal example I removed all the html stuff. I sent the complete
>>> script in my second post in this thread. When I look directly at the
>>> source I just see the »0« (echo $return_value . "\n";). And when I
>>> redirect stdout and stderr to a file there's a »0« in file_stdout and
>>> file_stderr is empty.
>>>
>>> I have absolutely no clue what's going on here, since it works with »cat«
>>> as a process and gnuplot works with »passthru«. That »cat« works means
>>> that my workflow with the pipes is right and that gnuplot works with
>>> »passthru« means that gnuplot is working inside PHP.
>>>
>>> Thanks for your help, I hope we can get it solved.
>>>
>>> Marco
>>>
>>
>> OK, I downloaded and installed gnuplot on my system. Change this line:
>>
>> $stdin_data = "set terminal svg;plot sin(x)\n";
>>
>> Note the newline char at the end.
>
> Thats it!!!!! Million thanks.
>
> How can that be?! In the shell it works without »\n«. Anyway, thanks for the
> effort you put into solving my problem.
>
>
> Marco
>

No, when you use the shell, pressing enter sends the newline.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: use of array_key_exists() to prevent duplicates?
Next Topic: $_POST not set on 404
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 15:30:16 GMT 2024

Total time taken to generate the page: 0.02738 seconds