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

Home » Imported messages » comp.lang.php » redirect stdout and stderr to PHP variables?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
redirect stdout and stderr to PHP variables? [message #176799] Wed, 25 January 2012 19:47 Go to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
I'm sure there's a way to do this, probably some simple syntax I've not
run into and am too stupid to find in the manual or through google-fu or
otherwise.

I'd like to issue an arbitrary shell command in a subroutine and have it
return an array that contains one element representing stdout and
another element representing stderr.

For example, using tar to deal with a bazillion files, error messages
might not be seen when specifying verbose output; I'd like to collect
them and display after stdout.

I've not found any syntax, yet, for redirecting to a PHP variable rather
than some file.

tia.
Re: redirect stdout and stderr to PHP variables? [message #176800 is a reply to message #176799] Wed, 25 January 2012 21:42 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 25.01.2012 20:47, schrieb crankypuss:
> I'm sure there's a way to do this, probably some simple syntax I've not run into and
> am too stupid to find in the manual or through google-fu or otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and have it return an
> array that contains one element representing stdout and another element representing
> stderr.
>
> For example, using tar to deal with a bazillion files, error messages might not be
> seen when specifying verbose output; I'd like to collect them and display after stdout.
>
> I've not found any syntax, yet, for redirecting to a PHP variable rather than some file.
>
> tia.

No problem here:

php > $res = shell_exec('rm this-does-really-not-exist.php 2>&1');
php > echo $res;
rm: Entfernen von „this-does-really-not-exist.php“ nicht möglich: Datei oder
Verzeichnis nicht gefunden
php >

Maybe you drop this requirement "array of...".

/Str.
Re: redirect stdout and stderr to PHP variables? [message #176801 is a reply to message #176799] Wed, 25 January 2012 21:46 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 25.01.2012 20:47, schrieb crankypuss:
> I'm sure there's a way to do this, probably some simple syntax I've not run into and
> am too stupid to find in the manual or through google-fu or otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and have it return an
> array that contains one element representing stdout and another element representing
> stderr.
>
> For example, using tar to deal with a bazillion files, error messages might not be
> seen when specifying verbose output; I'd like to collect them and display after stdout.
>
> I've not found any syntax, yet, for redirecting to a PHP variable rather than some file.
>
> tia.

Or like that, if you are fond of arrays:

php > $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
php > echo $rc;
1
php > print_r($a);
Array
(
[0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht möglich: Datei
oder Verzeichnis nicht gefunden
)
php >

/Str.
Re: redirect stdout and stderr to PHP variables? [message #176802 is a reply to message #176801] Wed, 25 January 2012 22:53 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/25/2012 02:46 PM, M. Strobel wrote:
> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
> php> echo $rc;
> 1
> php> print_r($a);
> Array
> (
> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht möglich: Datei
> oder Verzeichnis nicht gefunden
> )
> php>

Thanks, will try these in the morning.
Re: redirect stdout and stderr to PHP variables? [message #176803 is a reply to message #176802] Thu, 26 January 2012 08:33 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/25/2012 03:53 PM, crankypuss wrote:
> On 01/25/2012 02:46 PM, M. Strobel wrote:
>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
>> php> echo $rc;
>> 1
>> php> print_r($a);
>> Array
>> (
>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>> möglich: Datei
>> oder Verzeichnis nicht gefunden
>> )
>> php>
>
> Thanks, will try these in the morning.

BTW, where is this syntax documented? I'd prefer understanding it to
using magic.
Re: redirect stdout and stderr to PHP variables? [message #176804 is a reply to message #176803] Thu, 26 January 2012 08:46 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 26.01.2012 09:33, schrieb crankypuss:
> On 01/25/2012 03:53 PM, crankypuss wrote:
>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
>>> php> echo $rc;
>>> 1
>>> php> print_r($a);
>>> Array
>>> (
>>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>> möglich: Datei
>>> oder Verzeichnis nicht gefunden
>>> )
>>> php>
>>
>> Thanks, will try these in the morning.
>
> BTW, where is this syntax documented? I'd prefer understanding it to using magic.

Hey, I just booted my system in the morning, and checked for news...

see here: http://www.gnu.org/software/bash/manual/bashref.html#Redirections

The windows cmd.exe works quite the same in this point.

/Str.
Re: redirect stdout and stderr to PHP variables? [message #176805 is a reply to message #176803] Thu, 26 January 2012 09:00 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <jfr340125sq(at)news4(dot)newsguy(dot)com>,
crankypuss <no(at)email(dot)thanks> wrote:

> On 01/25/2012 03:53 PM, crankypuss wrote:
>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
>>> php> echo $rc;
>>> 1
>>> php> print_r($a);
>>> Array
>>> (
>>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>> möglich: Datei
>>> oder Verzeichnis nicht gefunden
>>> )
>>> php>
>>
>> Thanks, will try these in the morning.
>
> BTW, where is this syntax documented? I'd prefer understanding it to
> using magic.

Which syntax, exactly? exec is documented in the PHP online docs.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: redirect stdout and stderr to PHP variables? [message #176806 is a reply to message #176804] Thu, 26 January 2012 09:23 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/26/2012 01:46 AM, M. Strobel wrote:
> Am 26.01.2012 09:33, schrieb crankypuss:
>> On 01/25/2012 03:53 PM, crankypuss wrote:
>>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
>>>> php> echo $rc;
>>>> 1
>>>> php> print_r($a);
>>>> Array
>>>> (
>>>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> möglich: Datei
>>>> oder Verzeichnis nicht gefunden
>>>> )
>>>> php>
>>>
>>> Thanks, will try these in the morning.
>>
>> BTW, where is this syntax documented? I'd prefer understanding it to using magic.
>
> Hey, I just booted my system in the morning, and checked for news...
>
> see here: http://www.gnu.org/software/bash/manual/bashref.html#Redirections
>
> The windows cmd.exe works quite the same in this point.
>
> /Str.

Sorry, I might be more dense than I realized, but what does bash have to
do with php here? I might not (and often will not) be running a cli
script but rather running commands as part of generating an html page
for a web server.
Re: redirect stdout and stderr to PHP variables? [message #176807 is a reply to message #176805] Thu, 26 January 2012 09:27 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/26/2012 02:00 AM, Tim Streater wrote:
> In article <jfr340125sq(at)news4(dot)newsguy(dot)com>,
> crankypuss <no(at)email(dot)thanks> wrote:
>
>> On 01/25/2012 03:53 PM, crankypuss wrote:
>>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
>>>> php> echo $rc;
>>>> 1
>>>> php> print_r($a);
>>>> Array
>>>> (
>>>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> möglich: Datei
>>>> oder Verzeichnis nicht gefunden
>>>> )
>>>> php>
>>>
>>> Thanks, will try these in the morning.
>>
>> BTW, where is this syntax documented? I'd prefer understanding it to
>> using magic.
>
> Which syntax, exactly? exec is documented in the PHP online docs.

Sorry, I've been unable to find the bit about redirecting stdout and
stderr to PHP variables, can you point me to that? Yes, exec() provides
an optional &$output argument which in effect redirects stdout to the
varialble, but I see no way to obtain stderr separately. Thanks.
Re: redirect stdout and stderr to PHP variables? [message #176808 is a reply to message #176807] Thu, 26 January 2012 10:43 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 1/26/2012 10:27 AM, crankypuss wrote:
> On 01/26/2012 02:00 AM, Tim Streater wrote:
>> In article <jfr340125sq(at)news4(dot)newsguy(dot)com>,
>> crankypuss <no(at)email(dot)thanks> wrote:
>>
>>> On 01/25/2012 03:53 PM, crankypuss wrote:
>>>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> > php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a, $rc);
>>>> > php> echo $rc;
>>>> > 1
>>>> > php> print_r($a);
>>>> > Array
>>>> > (
>>>> > [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> > möglich: Datei
>>>> > oder Verzeichnis nicht gefunden
>>>> > )
>>>> > php>
>>>>
>>>> Thanks, will try these in the morning.
>>>
>>> BTW, where is this syntax documented? I'd prefer understanding it to
>>> using magic.
>>
>> Which syntax, exactly? exec is documented in the PHP online docs.
>
> Sorry, I've been unable to find the bit about redirecting stdout and
> stderr to PHP variables, can you point me to that? Yes, exec() provides
> an optional &$output argument which in effect redirects stdout to the
> varialble, but I see no way to obtain stderr separately. Thanks.

Hi crankypuss,

You only see this redirecting in functions that handle with STDERR and
STDOUT, functions like exec() you already use.
When you are working within 'normal' PHP (as in "No external processes
invoked by user", so no exec() and the like) you don't have the concept
of STDERR directly, nor do you need it.
Of course: errors can still occur, but PHP will handle them. You can
modify HOW you want PHP to handle them (logging into file/errorlog, put
them on screen, invoke some custom-errorhandler/etc).

To put it very simple (and probably incorrect) to get the general idea:
You can think of PHP in relation with STDIN and STDOUT and STDERR like this:

STDIN for PHP is what is feeded to PHP (via Apache, or commandline)
So that consists typically of things like (part of) URL, cookies,
referrer, etc.

STDOUT is what PHP produces and is returned to the client (most of the
time with a webserver inbetween).

STDERR is where PHP directs its erorrs to. (Think logfile, and/or to
STDOUT: this is all configurable.)

But you don't see STDIN/STDOUT/STDERR when you work with PHP without
using exec().

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: redirect stdout and stderr to PHP variables? [message #176809 is a reply to message #176808] Thu, 26 January 2012 11:14 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/26/2012 03:43 AM, Erwin Moller wrote:
> On 1/26/2012 10:27 AM, crankypuss wrote:
>> On 01/26/2012 02:00 AM, Tim Streater wrote:
>>> In article <jfr340125sq(at)news4(dot)newsguy(dot)com>,
>>> crankypuss <no(at)email(dot)thanks> wrote:
>>>
>>>> On 01/25/2012 03:53 PM, crankypuss wrote:
>>>> > On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> >> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a,
>>>> $rc);
>>>> >> php> echo $rc;
>>>> >> 1
>>>> >> php> print_r($a);
>>>> >> Array
>>>> >> (
>>>> >> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> >> möglich: Datei
>>>> >> oder Verzeichnis nicht gefunden
>>>> >> )
>>>> >> php>
>>>> >
>>>> > Thanks, will try these in the morning.
>>>>
>>>> BTW, where is this syntax documented? I'd prefer understanding it to
>>>> using magic.
>>>
>>> Which syntax, exactly? exec is documented in the PHP online docs.
>>
>> Sorry, I've been unable to find the bit about redirecting stdout and
>> stderr to PHP variables, can you point me to that? Yes, exec() provides
>> an optional &$output argument which in effect redirects stdout to the
>> varialble, but I see no way to obtain stderr separately. Thanks.
>
> Hi crankypuss,
>
> You only see this redirecting in functions that handle with STDERR and
> STDOUT, functions like exec() you already use.
> When you are working within 'normal' PHP (as in "No external processes
> invoked by user", so no exec() and the like) you don't have the concept
> of STDERR directly, nor do you need it.
> Of course: errors can still occur, but PHP will handle them. You can
> modify HOW you want PHP to handle them (logging into file/errorlog, put
> them on screen, invoke some custom-errorhandler/etc).
>
> To put it very simple (and probably incorrect) to get the general idea:
> You can think of PHP in relation with STDIN and STDOUT and STDERR like
> this:
>
> STDIN for PHP is what is feeded to PHP (via Apache, or commandline)
> So that consists typically of things like (part of) URL, cookies,
> referrer, etc.
>
> STDOUT is what PHP produces and is returned to the client (most of the
> time with a webserver inbetween).
>
> STDERR is where PHP directs its erorrs to. (Think logfile, and/or to
> STDOUT: this is all configurable.)
>
> But you don't see STDIN/STDOUT/STDERR when you work with PHP without
> using exec().
>
> Regards,
> Erwin Moller

So how do you capture only STDERR when using exec() ?
Re: redirect stdout and stderr to PHP variables? [message #176811 is a reply to message #176809] Thu, 26 January 2012 12:25 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 26.01.2012 12:14, schrieb crankypuss:
> On 01/26/2012 03:43 AM, Erwin Moller wrote:
>> On 1/26/2012 10:27 AM, crankypuss wrote:
>>> On 01/26/2012 02:00 AM, Tim Streater wrote:
>>>> In article <jfr340125sq(at)news4(dot)newsguy(dot)com>,
>>>> crankypuss <no(at)email(dot)thanks> wrote:
>>>>
>>>> > On 01/25/2012 03:53 PM, crankypuss wrote:
>>>> > > On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> > >> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a,
>>>> > $rc);
>>>> > >> php> echo $rc;
>>>> > >> 1
>>>> > >> php> print_r($a);
>>>> > >> Array
>>>> > >> (
>>>> > >> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> > >> möglich: Datei
>>>> > >> oder Verzeichnis nicht gefunden
>>>> > >> )
>>>> > >> php>
>>>> > >
>>>> > > Thanks, will try these in the morning.
>>>> >
>>>> > BTW, where is this syntax documented? I'd prefer understanding it to
>>>> > using magic.
>>>>
>>>> Which syntax, exactly? exec is documented in the PHP online docs.
>>>
>>> Sorry, I've been unable to find the bit about redirecting stdout and
>>> stderr to PHP variables, can you point me to that? Yes, exec() provides
>>> an optional &$output argument which in effect redirects stdout to the
>>> varialble, but I see no way to obtain stderr separately. Thanks.
>>
>> Hi crankypuss,
>>
>> You only see this redirecting in functions that handle with STDERR and
>> STDOUT, functions like exec() you already use.
>> When you are working within 'normal' PHP (as in "No external processes
>> invoked by user", so no exec() and the like) you don't have the concept
>> of STDERR directly, nor do you need it.
>> Of course: errors can still occur, but PHP will handle them. You can
>> modify HOW you want PHP to handle them (logging into file/errorlog, put
>> them on screen, invoke some custom-errorhandler/etc).
>>
>> To put it very simple (and probably incorrect) to get the general idea:
>> You can think of PHP in relation with STDIN and STDOUT and STDERR like
>> this:
>>
>> STDIN for PHP is what is feeded to PHP (via Apache, or commandline)
>> So that consists typically of things like (part of) URL, cookies,
>> referrer, etc.
>>
>> STDOUT is what PHP produces and is returned to the client (most of the
>> time with a webserver inbetween).
>>
>> STDERR is where PHP directs its erorrs to. (Think logfile, and/or to
>> STDOUT: this is all configurable.)
>>
>> But you don't see STDIN/STDOUT/STDERR when you work with PHP without
>> using exec().
>>
>> Regards,
>> Erwin Moller
>
> So how do you capture only STDERR when using exec() ?

You can not capture only stderr, because exec returns only stdout. So you only can
redirect stderr to stdout.

You can do it with http://es.php.net/manual/en/function.proc-open.php

Maybe you should explain what your basic problem is. Mentioning the environment so we
do not have to guess.

/Str.
Re: redirect stdout and stderr to PHP variables? [message #176812 is a reply to message #176806] Thu, 26 January 2012 13:35 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(crankypuss)

> Sorry, I might be more dense than I realized, but what does bash have to
> do with php here?

Running exec() and the like invokes a shell, which then executes the
given command. Redirecting STDERR ist part of that shell, not PHP.

Micha

--
http://mfesser.de/blickwinkel
Re: redirect stdout and stderr to PHP variables? [message #176813 is a reply to message #176811] Thu, 26 January 2012 14:11 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/26/2012 05:25 AM, M. Strobel wrote:
> Am 26.01.2012 12:14, schrieb crankypuss:
>> On 01/26/2012 03:43 AM, Erwin Moller wrote:
>>> On 1/26/2012 10:27 AM, crankypuss wrote:
>>>> On 01/26/2012 02:00 AM, Tim Streater wrote:
>>>> > In article<jfr340125sq(at)news4(dot)newsguy(dot)com>,
>>>> > crankypuss<no(at)email(dot)thanks> wrote:
>>>> >
>>>> >> On 01/25/2012 03:53 PM, crankypuss wrote:
>>>> >>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> >>>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a,
>>>> >> $rc);
>>>> >>>> php> echo $rc;
>>>> >>>> 1
>>>> >>>> php> print_r($a);
>>>> >>>> Array
>>>> >>>> (
>>>> >>>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> >>>> möglich: Datei
>>>> >>>> oder Verzeichnis nicht gefunden
>>>> >>>> )
>>>> >>>> php>
>>>> >>>
>>>> >>> Thanks, will try these in the morning.
>>>> >>
>>>> >> BTW, where is this syntax documented? I'd prefer understanding it to
>>>> >> using magic.
>>>> >
>>>> > Which syntax, exactly? exec is documented in the PHP online docs.
>>>>
>>>> Sorry, I've been unable to find the bit about redirecting stdout and
>>>> stderr to PHP variables, can you point me to that? Yes, exec() provides
>>>> an optional&$output argument which in effect redirects stdout to the
>>>> varialble, but I see no way to obtain stderr separately. Thanks.
>>>
>>> Hi crankypuss,
>>>
>>> You only see this redirecting in functions that handle with STDERR and
>>> STDOUT, functions like exec() you already use.
>>> When you are working within 'normal' PHP (as in "No external processes
>>> invoked by user", so no exec() and the like) you don't have the concept
>>> of STDERR directly, nor do you need it.
>>> Of course: errors can still occur, but PHP will handle them. You can
>>> modify HOW you want PHP to handle them (logging into file/errorlog, put
>>> them on screen, invoke some custom-errorhandler/etc).
>>>
>>> To put it very simple (and probably incorrect) to get the general idea:
>>> You can think of PHP in relation with STDIN and STDOUT and STDERR like
>>> this:
>>>
>>> STDIN for PHP is what is feeded to PHP (via Apache, or commandline)
>>> So that consists typically of things like (part of) URL, cookies,
>>> referrer, etc.
>>>
>>> STDOUT is what PHP produces and is returned to the client (most of the
>>> time with a webserver inbetween).
>>>
>>> STDERR is where PHP directs its erorrs to. (Think logfile, and/or to
>>> STDOUT: this is all configurable.)
>>>
>>> But you don't see STDIN/STDOUT/STDERR when you work with PHP without
>>> using exec().
>>>
>>> Regards,
>>> Erwin Moller
>>
>> So how do you capture only STDERR when using exec() ?
>
> You can not capture only stderr, because exec returns only stdout. So you only can
> redirect stderr to stdout.

As far as I'm concerned that's pretty useless.

> You can do it with http://es.php.net/manual/en/function.proc-open.php
> Maybe you should explain what your basic problem is.

I have. Lots of stdout lines with a few stderr lines, I want to output
the stderr lines *after* stdout instead of interspersed.

> Mentioning the environment so we
> do not have to guess.
>
> /Str.
>
Re: redirect stdout and stderr to PHP variables? [message #176815 is a reply to message #176813] Thu, 26 January 2012 15:18 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 1/26/2012 3:11 PM, crankypuss wrote:
> On 01/26/2012 05:25 AM, M. Strobel wrote:
>> Am 26.01.2012 12:14, schrieb crankypuss:
>>> On 01/26/2012 03:43 AM, Erwin Moller wrote:
>>>> On 1/26/2012 10:27 AM, crankypuss wrote:
>>>> > On 01/26/2012 02:00 AM, Tim Streater wrote:
>>>> >> In article<jfr340125sq(at)news4(dot)newsguy(dot)com>,
>>>> >> crankypuss<no(at)email(dot)thanks> wrote:
>>>> >>
>>>> >>> On 01/25/2012 03:53 PM, crankypuss wrote:
>>>> >>>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> >>>>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a,
>>>> >>> $rc);
>>>> >>>>> php> echo $rc;
>>>> >>>>> 1
>>>> >>>>> php> print_r($a);
>>>> >>>>> Array
>>>> >>>>> (
>>>> >>>>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> >>>>> möglich: Datei
>>>> >>>>> oder Verzeichnis nicht gefunden
>>>> >>>>> )
>>>> >>>>> php>
>>>> >>>>
>>>> >>>> Thanks, will try these in the morning.
>>>> >>>
>>>> >>> BTW, where is this syntax documented? I'd prefer understanding it to
>>>> >>> using magic.
>>>> >>
>>>> >> Which syntax, exactly? exec is documented in the PHP online docs.
>>>> >
>>>> > Sorry, I've been unable to find the bit about redirecting stdout and
>>>> > stderr to PHP variables, can you point me to that? Yes, exec()
>>>> > provides
>>>> > an optional&$output argument which in effect redirects stdout to the
>>>> > varialble, but I see no way to obtain stderr separately. Thanks.
>>>>
>>>> Hi crankypuss,
>>>>
>>>> You only see this redirecting in functions that handle with STDERR and
>>>> STDOUT, functions like exec() you already use.
>>>> When you are working within 'normal' PHP (as in "No external processes
>>>> invoked by user", so no exec() and the like) you don't have the concept
>>>> of STDERR directly, nor do you need it.
>>>> Of course: errors can still occur, but PHP will handle them. You can
>>>> modify HOW you want PHP to handle them (logging into file/errorlog, put
>>>> them on screen, invoke some custom-errorhandler/etc).
>>>>
>>>> To put it very simple (and probably incorrect) to get the general idea:
>>>> You can think of PHP in relation with STDIN and STDOUT and STDERR like
>>>> this:
>>>>
>>>> STDIN for PHP is what is feeded to PHP (via Apache, or commandline)
>>>> So that consists typically of things like (part of) URL, cookies,
>>>> referrer, etc.
>>>>
>>>> STDOUT is what PHP produces and is returned to the client (most of the
>>>> time with a webserver inbetween).
>>>>
>>>> STDERR is where PHP directs its erorrs to. (Think logfile, and/or to
>>>> STDOUT: this is all configurable.)
>>>>
>>>> But you don't see STDIN/STDOUT/STDERR when you work with PHP without
>>>> using exec().
>>>>
>>>> Regards,
>>>> Erwin Moller
>>>
>>> So how do you capture only STDERR when using exec() ?
>>
>> You can not capture only stderr, because exec returns only stdout. So
>> you only can
>> redirect stderr to stdout.
>
> As far as I'm concerned that's pretty useless.
>
>> You can do it with http://es.php.net/manual/en/function.proc-open.php
>> Maybe you should explain what your basic problem is.
>
> I have. Lots of stdout lines with a few stderr lines, I want to output
> the stderr lines *after* stdout instead of interspersed.
>


Hi Crankypuss,

Read this:
http://nl.php.net/manual/en/function.proc-open.php

It is a little more complicated, but you can capture what you need
(STDERR in this case).

Very off topic:
Concerning your wish to intergrate PHP with a windowing environment, I
found this:

http://en.wikipedia.org/wiki/PHP-Qt

I haven't used it myself, only with C++ so I have no clue how good it
all is, but QT is solid.


Regards,
Erwin Moller


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: redirect stdout and stderr to PHP variables? [message #176816 is a reply to message #176799] Wed, 25 January 2012 22:25 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/25/2012 2:47 PM, crankypuss wrote:
> I'm sure there's a way to do this, probably some simple syntax I've not
> run into and am too stupid to find in the manual or through google-fu or
> otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and have it
> return an array that contains one element representing stdout and
> another element representing stderr.
>
> For example, using tar to deal with a bazillion files, error messages
> might not be seen when specifying verbose output; I'd like to collect
> them and display after stdout.
>
> I've not found any syntax, yet, for redirecting to a PHP variable rather
> than some file.
>
> tia.

Take a look at the proc_open(). The examples show how do return stdout
and stderr in separate streams.

It may be more than you need (this is made to be interactive between the
PHP script and the process), but you don't HAVE to send data to the process.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: redirect stdout and stderr to PHP variables? [message #176817 is a reply to message #176799] Wed, 25 January 2012 22:52 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Wed, 25 Jan 2012 12:47:11 -0700, crankypuss wrote:
> I'm sure there's a way to do this, probably some simple syntax I've not
> run into and am too stupid to find in the manual or through google-fu or
> otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and have it
> return an array that contains one element representing stdout and
> another element representing stderr.
>
> For example, using tar to deal with a bazillion files, error messages
> might not be seen when specifying verbose output; I'd like to collect
> them and display after stdout.
>
> I've not found any syntax, yet, for redirecting to a PHP variable rather
> than some file.

Sorry, you have to use the file, then read the file if you want to parse
back in what went out on stderr.

--
85. I will not use any plan in which the final step is horribly
complicated, e.g. "Align the 12 Stones of Power on the sacred altar
then activate the medallion at the moment of total eclipse."
Instead it will be more along the lines of "Push the button."
Re: redirect stdout and stderr to PHP variables? [message #176818 is a reply to message #176799] Wed, 25 January 2012 22:56 Go to previous messageGo to next message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
crankypuss <no(at)email(dot)thanks> wrote:

> I'm sure there's a way to do this, probably some simple syntax
> I've not run into and am too stupid to find in the manual or
> through google-fu or otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and
> have it return an array that contains one element representing
> stdout and another element representing stderr.

Have a look at `proc_open()' for more fine-grained control of a
process:

<http://php.net/proc_open>


<snip>

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
Re: redirect stdout and stderr to PHP variables? [message #176820 is a reply to message #176806] Thu, 26 January 2012 13:49 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Thu, 26 Jan 2012 02:23:07 -0700, crankypuss wrote:

> On 01/26/2012 01:46 AM, M. Strobel wrote:
>
>> Am 26.01.2012 09:33, schrieb crankypuss:
>>
>>> BTW, where is this syntax documented? I'd prefer understanding it to
>>> using magic.
>>
>> Hey, I just booted my system in the morning, and checked for news...
>>
>> see here:
>> http://www.gnu.org/software/bash/manual/bashref.html#Redirections
>>
>> The windows cmd.exe works quite the same in this point.
>>
>> /Str.
>
> Sorry, I might be more dense than I realized, but what does bash have
> to do with php here? I might not (and often will not) be running a cli
> script but rather running commands as part of generating an html page
> for a web server.

That, or some other shell depending (IIRC) what shell the user ID that
the script is running under, is what is going to parse the command you
pass to exec(). That, too, is not handled by magic.

--
Babbage pioneered IT management: his project took vast sums of cash, the spec
kept changing, and it ultimately failed to deliver. Turing scavenged supplies
and knocked together something that worked and solved the problem - reward?
Arrest and castration. Which one feels most like your career? -- Peter Corlett
Re: redirect stdout and stderr to PHP variables? [message #176821 is a reply to message #176815] Fri, 27 January 2012 10:11 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/26/2012 08:18 AM, Erwin Moller wrote:
> On 1/26/2012 3:11 PM, crankypuss wrote:
>> On 01/26/2012 05:25 AM, M. Strobel wrote:
>>> Am 26.01.2012 12:14, schrieb crankypuss:
>>>> On 01/26/2012 03:43 AM, Erwin Moller wrote:
>>>> > On 1/26/2012 10:27 AM, crankypuss wrote:
>>>> >> On 01/26/2012 02:00 AM, Tim Streater wrote:
>>>> >>> In article<jfr340125sq(at)news4(dot)newsguy(dot)com>,
>>>> >>> crankypuss<no(at)email(dot)thanks> wrote:
>>>> >>>
>>>> >>>> On 01/25/2012 03:53 PM, crankypuss wrote:
>>>> >>>>> On 01/25/2012 02:46 PM, M. Strobel wrote:
>>>> >>>>>> php> $res1 = exec('rm this-does-really-not-exist.php 2>&1', $a,
>>>> >>>> $rc);
>>>> >>>>>> php> echo $rc;
>>>> >>>>>> 1
>>>> >>>>>> php> print_r($a);
>>>> >>>>>> Array
>>>> >>>>>> (
>>>> >>>>>> [0] => rm: Entfernen von „this-does-really-not-exist.php“ nicht
>>>> >>>>>> möglich: Datei
>>>> >>>>>> oder Verzeichnis nicht gefunden
>>>> >>>>>> )
>>>> >>>>>> php>
>>>> >>>>>
>>>> >>>>> Thanks, will try these in the morning.
>>>> >>>>
>>>> >>>> BTW, where is this syntax documented? I'd prefer understanding
>>>> >>>> it to
>>>> >>>> using magic.
>>>> >>>
>>>> >>> Which syntax, exactly? exec is documented in the PHP online docs.
>>>> >>
>>>> >> Sorry, I've been unable to find the bit about redirecting stdout and
>>>> >> stderr to PHP variables, can you point me to that? Yes, exec()
>>>> >> provides
>>>> >> an optional&$output argument which in effect redirects stdout to the
>>>> >> varialble, but I see no way to obtain stderr separately. Thanks.
>>>> >
>>>> > Hi crankypuss,
>>>> >
>>>> > You only see this redirecting in functions that handle with STDERR and
>>>> > STDOUT, functions like exec() you already use.
>>>> > When you are working within 'normal' PHP (as in "No external processes
>>>> > invoked by user", so no exec() and the like) you don't have the
>>>> > concept
>>>> > of STDERR directly, nor do you need it.
>>>> > Of course: errors can still occur, but PHP will handle them. You can
>>>> > modify HOW you want PHP to handle them (logging into file/errorlog,
>>>> > put
>>>> > them on screen, invoke some custom-errorhandler/etc).
>>>> >
>>>> > To put it very simple (and probably incorrect) to get the general
>>>> > idea:
>>>> > You can think of PHP in relation with STDIN and STDOUT and STDERR like
>>>> > this:
>>>> >
>>>> > STDIN for PHP is what is feeded to PHP (via Apache, or commandline)
>>>> > So that consists typically of things like (part of) URL, cookies,
>>>> > referrer, etc.
>>>> >
>>>> > STDOUT is what PHP produces and is returned to the client (most of the
>>>> > time with a webserver inbetween).
>>>> >
>>>> > STDERR is where PHP directs its erorrs to. (Think logfile, and/or to
>>>> > STDOUT: this is all configurable.)
>>>> >
>>>> > But you don't see STDIN/STDOUT/STDERR when you work with PHP without
>>>> > using exec().
>>>> >
>>>> > Regards,
>>>> > Erwin Moller
>>>>
>>>> So how do you capture only STDERR when using exec() ?
>>>
>>> You can not capture only stderr, because exec returns only stdout. So
>>> you only can
>>> redirect stderr to stdout.
>>
>> As far as I'm concerned that's pretty useless.
>>
>>> You can do it with http://es.php.net/manual/en/function.proc-open.php
>>> Maybe you should explain what your basic problem is.
>>
>> I have. Lots of stdout lines with a few stderr lines, I want to output
>> the stderr lines *after* stdout instead of interspersed.
>>
>
>
> Hi Crankypuss,
>
> Read this:
> http://nl.php.net/manual/en/function.proc-open.php
>
> It is a little more complicated, but you can capture what you need
> (STDERR in this case).

It looks like it might be what I'm looking for, but it will be a while
before I can get around to studying/trying it; thanks very much.

> Very off topic:
> Concerning your wish to intergrate PHP with a windowing environment, I
> found this:
>
> http://en.wikipedia.org/wiki/PHP-Qt
>
> I haven't used it myself, only with C++ so I have no clue how good it
> all is, but QT is solid.

Yes, I found that several days ago. It's object oriented, which is
rather a turnoff for me since all my PHP is currently strictly
procedural (mostly because eventually I'll port it to C, not C++, and
will probably do the bulk of that with a conversion program), there's no
repository that looks clearly trustworthy, and I don't care to build it
from source (because at this point in time I am too stoopid to do that
and have too much other stuff going on).

I'm keeping my eyes open but my current thinking is to either build a
pseudo-browser (based on gekko?) or modify Firefox about the head and
shoulders until it knows how to invoke PHP the way apache invokes it.

Thanks much.
Re: redirect stdout and stderr to PHP variables? [message #176822 is a reply to message #176816] Fri, 27 January 2012 10:16 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/25/2012 03:25 PM, Jerry Stuckle wrote:
> On 1/25/2012 2:47 PM, crankypuss wrote:
>> I'm sure there's a way to do this, probably some simple syntax I've not
>> run into and am too stupid to find in the manual or through google-fu or
>> otherwise.
>>
>> I'd like to issue an arbitrary shell command in a subroutine and have it
>> return an array that contains one element representing stdout and
>> another element representing stderr.
>>
>> For example, using tar to deal with a bazillion files, error messages
>> might not be seen when specifying verbose output; I'd like to collect
>> them and display after stdout.
>>
>> I've not found any syntax, yet, for redirecting to a PHP variable rather
>> than some file.
>>
>> tia.
>
> Take a look at the proc_open(). The examples show how do return stdout
> and stderr in separate streams.

Thanks Jerry, someone else mentioned that too, and it looks like it will
probably do the trick though I'm not sure how soon I'll be able to play
with it.

> It may be more than you need (this is made to be interactive between the
> PHP script and the process), but you don't HAVE to send data to the
> process.

I've never minded putting large amounts of effort into building
something with a simple general purpose interface, those efforts tend to
repay tenfold.

(On a personal note, haven't you retired yet? Or are you like me,
addicted to code for life?)
Re: redirect stdout and stderr to PHP variables? [message #176823 is a reply to message #176817] Fri, 27 January 2012 10:22 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/25/2012 03:52 PM, Peter H. Coffin wrote:
> On Wed, 25 Jan 2012 12:47:11 -0700, crankypuss wrote:
>> I'm sure there's a way to do this, probably some simple syntax I've not
>> run into and am too stupid to find in the manual or through google-fu or
>> otherwise.
>>
>> I'd like to issue an arbitrary shell command in a subroutine and have it
>> return an array that contains one element representing stdout and
>> another element representing stderr.
>>
>> For example, using tar to deal with a bazillion files, error messages
>> might not be seen when specifying verbose output; I'd like to collect
>> them and display after stdout.
>>
>> I've not found any syntax, yet, for redirecting to a PHP variable rather
>> than some file.
>
> Sorry, you have to use the file, then read the file if you want to parse
> back in what went out on stderr.

Actually no, I do not have to use no steenking files, I might have to
start writing something from scratch in freaking assembler, but I don't
have to use no steenking files to do something that hasn't the least
thing to do with files.

Thank you though, and apologies for any crankiness that has leaked into
my post... I'm going through the transition from many years of Windows
to using Linux, and although I mostly like it, I find the tendency for
scripts to rely on command output run through pipes to be abhorrent
since the whole concept of validity checking goes out the window when
you swallow that paradigm, and writing a file when it isn't necessary to
the actual process but rather is a matter of conformance to sloppiness
in the environment is at least as bad imo.
Re: redirect stdout and stderr to PHP variables? [message #176824 is a reply to message #176818] Fri, 27 January 2012 10:23 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/25/2012 03:56 PM, Curtis Dyer wrote:
> crankypuss<no(at)email(dot)thanks> wrote:
>
>> I'm sure there's a way to do this, probably some simple syntax
>> I've not run into and am too stupid to find in the manual or
>> through google-fu or otherwise.
>>
>> I'd like to issue an arbitrary shell command in a subroutine and
>> have it return an array that contains one element representing
>> stdout and another element representing stderr.
>
> Have a look at `proc_open()' for more fine-grained control of a
> process:
>
> <http://php.net/proc_open>
>
>
> <snip>

Third recommendation for proc_open() this morning, you guys looking at
each others' notes? <g> Thanks.
Re: redirect stdout and stderr to PHP variables? [message #176825 is a reply to message #176824] Fri, 27 January 2012 10:42 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 1/27/2012 11:23 AM, crankypuss wrote:
> On 01/25/2012 03:56 PM, Curtis Dyer wrote:
>> crankypuss<no(at)email(dot)thanks> wrote:
>>
>>> I'm sure there's a way to do this, probably some simple syntax
>>> I've not run into and am too stupid to find in the manual or
>>> through google-fu or otherwise.
>>>
>>> I'd like to issue an arbitrary shell command in a subroutine and
>>> have it return an array that contains one element representing
>>> stdout and another element representing stderr.
>>
>> Have a look at `proc_open()' for more fine-grained control of a
>> process:
>>
>> <http://php.net/proc_open>
>>
>>
>> <snip>
>
> Third recommendation for proc_open() this morning, you guys looking at
> each others' notes? <g> Thanks.

I was wondering that too.
When I posted my suggestion, I didn't see any others suggesting it.
But today I saw that Jerry and Curtis wrote it one day earlier.
I can imagine I had some mental black-out, but this looks like a mondial
one. ;-)

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: redirect stdout and stderr to PHP variables? [message #176826 is a reply to message #176823] Fri, 27 January 2012 11:25 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 27.01.2012 11:22, schrieb crankypuss:
> On 01/25/2012 03:52 PM, Peter H. Coffin wrote:
>> On Wed, 25 Jan 2012 12:47:11 -0700, crankypuss wrote:
>>> I'm sure there's a way to do this, probably some simple syntax I've not
>>> run into and am too stupid to find in the manual or through google-fu or
>>> otherwise.
>>>
>>> I'd like to issue an arbitrary shell command in a subroutine and have it
>>> return an array that contains one element representing stdout and
>>> another element representing stderr.
>>>
>>> For example, using tar to deal with a bazillion files, error messages
>>> might not be seen when specifying verbose output; I'd like to collect
>>> them and display after stdout.
>>>
>>> I've not found any syntax, yet, for redirecting to a PHP variable rather
>>> than some file.
>>
>> Sorry, you have to use the file, then read the file if you want to parse
>> back in what went out on stderr.
>
> Actually no, I do not have to use no steenking files, I might have to start writing
> something from scratch in freaking assembler, but I don't have to use no steenking
> files to do something that hasn't the least thing to do with files.
>
> Thank you though, and apologies for any crankiness that has leaked into my post...
> I'm going through the transition from many years of Windows to using Linux, and
> although I mostly like it, I find the tendency for scripts to rely on command output
> run through pipes to be abhorrent since the whole concept of validity checking goes
> out the window when you swallow that paradigm, and writing a file when it isn't

Can you expand on that? There must be a misunderstanding, the command you run does
all validity checking, me thinks.

/Str.

> necessary to the actual process but rather is a matter of conformance to sloppiness
> in the environment is at least as bad imo.
Re: redirect stdout and stderr to PHP variables? [message #176827 is a reply to message #176822] Fri, 27 January 2012 13:49 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/27/2012 5:16 AM, crankypuss wrote:
> On 01/25/2012 03:25 PM, Jerry Stuckle wrote:
>> On 1/25/2012 2:47 PM, crankypuss wrote:
>>> I'm sure there's a way to do this, probably some simple syntax I've not
>>> run into and am too stupid to find in the manual or through google-fu or
>>> otherwise.
>>>
>>> I'd like to issue an arbitrary shell command in a subroutine and have it
>>> return an array that contains one element representing stdout and
>>> another element representing stderr.
>>>
>>> For example, using tar to deal with a bazillion files, error messages
>>> might not be seen when specifying verbose output; I'd like to collect
>>> them and display after stdout.
>>>
>>> I've not found any syntax, yet, for redirecting to a PHP variable rather
>>> than some file.
>>>
>>> tia.
>>
>> Take a look at the proc_open(). The examples show how do return stdout
>> and stderr in separate streams.
>
> Thanks Jerry, someone else mentioned that too, and it looks like it will
> probably do the trick though I'm not sure how soon I'll be able to play
> with it.
>
>> It may be more than you need (this is made to be interactive between the
>> PHP script and the process), but you don't HAVE to send data to the
>> process.
>
> I've never minded putting large amounts of effort into building
> something with a simple general purpose interface, those efforts tend to
> repay tenfold.
>
> (On a personal note, haven't you retired yet? Or are you like me,
> addicted to code for life?)

Nope, still too young to retire. I don't know what I'd do with myself :)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: redirect stdout and stderr to PHP variables? [message #176828 is a reply to message #176827] Fri, 27 January 2012 15:00 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 1/27/2012 2:49 PM, Jerry Stuckle wrote:
> On 1/27/2012 5:16 AM, crankypuss wrote:
>> On 01/25/2012 03:25 PM, Jerry Stuckle wrote:
>>> On 1/25/2012 2:47 PM, crankypuss wrote:
>>>> I'm sure there's a way to do this, probably some simple syntax I've not
>>>> run into and am too stupid to find in the manual or through
>>>> google-fu or
>>>> otherwise.
>>>>
>>>> I'd like to issue an arbitrary shell command in a subroutine and
>>>> have it
>>>> return an array that contains one element representing stdout and
>>>> another element representing stderr.
>>>>
>>>> For example, using tar to deal with a bazillion files, error messages
>>>> might not be seen when specifying verbose output; I'd like to collect
>>>> them and display after stdout.
>>>>
>>>> I've not found any syntax, yet, for redirecting to a PHP variable
>>>> rather
>>>> than some file.
>>>>
>>>> tia.
>>>
>>> Take a look at the proc_open(). The examples show how do return stdout
>>> and stderr in separate streams.
>>
>> Thanks Jerry, someone else mentioned that too, and it looks like it will
>> probably do the trick though I'm not sure how soon I'll be able to play
>> with it.
>>
>>> It may be more than you need (this is made to be interactive between the
>>> PHP script and the process), but you don't HAVE to send data to the
>>> process.
>>
>> I've never minded putting large amounts of effort into building
>> something with a simple general purpose interface, those efforts tend to
>> repay tenfold.
>>
>> (On a personal note, haven't you retired yet? Or are you like me,
>> addicted to code for life?)
>
> Nope, still too young to retire. I don't know what I'd do with myself :)
>

Get a hobby, like computer programming. ;-)

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: redirect stdout and stderr to PHP variables? [message #176829 is a reply to message #176828] Fri, 27 January 2012 15:12 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/27/2012 10:00 AM, Erwin Moller wrote:
> On 1/27/2012 2:49 PM, Jerry Stuckle wrote:
>> On 1/27/2012 5:16 AM, crankypuss wrote:
>>> On 01/25/2012 03:25 PM, Jerry Stuckle wrote:
>>>> On 1/25/2012 2:47 PM, crankypuss wrote:
>>>> > I'm sure there's a way to do this, probably some simple syntax I've
>>>> > not
>>>> > run into and am too stupid to find in the manual or through
>>>> > google-fu or
>>>> > otherwise.
>>>> >
>>>> > I'd like to issue an arbitrary shell command in a subroutine and
>>>> > have it
>>>> > return an array that contains one element representing stdout and
>>>> > another element representing stderr.
>>>> >
>>>> > For example, using tar to deal with a bazillion files, error messages
>>>> > might not be seen when specifying verbose output; I'd like to collect
>>>> > them and display after stdout.
>>>> >
>>>> > I've not found any syntax, yet, for redirecting to a PHP variable
>>>> > rather
>>>> > than some file.
>>>> >
>>>> > tia.
>>>>
>>>> Take a look at the proc_open(). The examples show how do return stdout
>>>> and stderr in separate streams.
>>>
>>> Thanks Jerry, someone else mentioned that too, and it looks like it will
>>> probably do the trick though I'm not sure how soon I'll be able to play
>>> with it.
>>>
>>>> It may be more than you need (this is made to be interactive between
>>>> the
>>>> PHP script and the process), but you don't HAVE to send data to the
>>>> process.
>>>
>>> I've never minded putting large amounts of effort into building
>>> something with a simple general purpose interface, those efforts tend to
>>> repay tenfold.
>>>
>>> (On a personal note, haven't you retired yet? Or are you like me,
>>> addicted to code for life?)
>>
>> Nope, still too young to retire. I don't know what I'd do with myself :)
>>
>
> Get a hobby, like computer programming. ;-)
>
> Regards,
> Erwin Moller
>

ROFL!

Ah, that's part of the problem, Erwin. I have a hobby. But around
here, airplane rental is $140/hr and up.

Maybe what I need is a cheaper hobby :)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: redirect stdout and stderr to PHP variables? [message #176830 is a reply to message #176829] Fri, 27 January 2012 16:37 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Fri, 27 Jan 2012 10:12:08 -0500, Jerry Stuckle wrote:
> On 1/27/2012 10:00 AM, Erwin Moller wrote:
>> Get a hobby, like computer programming. ;-)
>>
>> Regards,
>> Erwin Moller
>>
>
> ROFL!
>
> Ah, that's part of the problem, Erwin. I have a hobby. But around
> here, airplane rental is $140/hr and up.
>
> Maybe what I need is a cheaper hobby :)

100LL ain't cheap either.

--
73. I will not agree to let the heroes go free if they win a rigged
contest, even though my advisors assure me it is impossible for
them to win.
--Peter Anspach's list of things to do as an Evil Overlord
Re: redirect stdout and stderr to PHP variables? [message #176841 is a reply to message #176829] Sat, 28 January 2012 12:00 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/27/2012 08:12 AM, Jerry Stuckle wrote:
> On 1/27/2012 10:00 AM, Erwin Moller wrote:
>> On 1/27/2012 2:49 PM, Jerry Stuckle wrote:
>>> On 1/27/2012 5:16 AM, crankypuss wrote:
>>>> On 01/25/2012 03:25 PM, Jerry Stuckle wrote:
>>>> > On 1/25/2012 2:47 PM, crankypuss wrote:
>>>> >> I'm sure there's a way to do this, probably some simple syntax I've
>>>> >> not
>>>> >> run into and am too stupid to find in the manual or through
>>>> >> google-fu or
>>>> >> otherwise.
>>>> >>
>>>> >> I'd like to issue an arbitrary shell command in a subroutine and
>>>> >> have it
>>>> >> return an array that contains one element representing stdout and
>>>> >> another element representing stderr.
>>>> >>
>>>> >> For example, using tar to deal with a bazillion files, error messages
>>>> >> might not be seen when specifying verbose output; I'd like to collect
>>>> >> them and display after stdout.
>>>> >>
>>>> >> I've not found any syntax, yet, for redirecting to a PHP variable
>>>> >> rather
>>>> >> than some file.
>>>> >>
>>>> >> tia.
>>>> >
>>>> > Take a look at the proc_open(). The examples show how do return stdout
>>>> > and stderr in separate streams.
>>>>
>>>> Thanks Jerry, someone else mentioned that too, and it looks like it
>>>> will
>>>> probably do the trick though I'm not sure how soon I'll be able to play
>>>> with it.
>>>>
>>>> > It may be more than you need (this is made to be interactive between
>>>> > the
>>>> > PHP script and the process), but you don't HAVE to send data to the
>>>> > process.
>>>>
>>>> I've never minded putting large amounts of effort into building
>>>> something with a simple general purpose interface, those efforts
>>>> tend to
>>>> repay tenfold.
>>>>
>>>> (On a personal note, haven't you retired yet? Or are you like me,
>>>> addicted to code for life?)
>>>
>>> Nope, still too young to retire. I don't know what I'd do with myself :)
>>>
>>
>> Get a hobby, like computer programming. ;-)
>>
>> Regards,
>> Erwin Moller
>>
>
> ROFL!
>
> Ah, that's part of the problem, Erwin. I have a hobby. But around here,
> airplane rental is $140/hr and up.
>
> Maybe what I need is a cheaper hobby :)

LOL, back in college I had a couple roommates who were heavily into
learning to fly. I decided that flying was probably a lot like heroin,
"too good to try". <g>
Re: redirect stdout and stderr to PHP variables? [message #176842 is a reply to message #176826] Sat, 28 January 2012 12:03 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/27/2012 04:25 AM, M. Strobel wrote:
> Am 27.01.2012 11:22, schrieb crankypuss:
>> On 01/25/2012 03:52 PM, Peter H. Coffin wrote:
>>> On Wed, 25 Jan 2012 12:47:11 -0700, crankypuss wrote:
>>>> I'm sure there's a way to do this, probably some simple syntax I've not
>>>> run into and am too stupid to find in the manual or through google-fu or
>>>> otherwise.
>>>>
>>>> I'd like to issue an arbitrary shell command in a subroutine and have it
>>>> return an array that contains one element representing stdout and
>>>> another element representing stderr.
>>>>
>>>> For example, using tar to deal with a bazillion files, error messages
>>>> might not be seen when specifying verbose output; I'd like to collect
>>>> them and display after stdout.
>>>>
>>>> I've not found any syntax, yet, for redirecting to a PHP variable rather
>>>> than some file.
>>>
>>> Sorry, you have to use the file, then read the file if you want to parse
>>> back in what went out on stderr.
>>
>> Actually no, I do not have to use no steenking files, I might have to start writing
>> something from scratch in freaking assembler, but I don't have to use no steenking
>> files to do something that hasn't the least thing to do with files.
>>
>> Thank you though, and apologies for any crankiness that has leaked into my post...
>> I'm going through the transition from many years of Windows to using Linux, and
>> although I mostly like it, I find the tendency for scripts to rely on command output
>> run through pipes to be abhorrent since the whole concept of validity checking goes
>> out the window when you swallow that paradigm, and writing a file when it isn't
>
> Can you expand on that? There must be a misunderstanding, the command you run does
> all validity checking, me thinks.

Look at any bash script longer than about 4 lines. You'll find it
issuing some command and running it through sed or whatever and then
through the next stage of the pipe and basically *assuming* that command
output formats never change and nothing can go wrong go wrong.
Re: redirect stdout and stderr to PHP variables? [message #176845 is a reply to message #176842] Sat, 28 January 2012 17:40 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Sat, 28 Jan 2012 05:03:14 -0700, crankypuss wrote:

> On 01/27/2012 04:25 AM, M. Strobel wrote:
>
>> Am 27.01.2012 11:22, schrieb crankypuss:
>>
>>> I'm going through the transition from many years of Windows to
>>> using Linux, and although I mostly like it, I find the tendency for
>>> scripts to rely on command output run through pipes to be abhorrent
>>> since the whole concept of validity checking goes out the window
>>> when you swallow that paradigm, and writing a file when it isn't
>>
>> Can you expand on that? There must be a misunderstanding, the command
>> you run does all validity checking, me thinks.
>
> Look at any bash script longer than about 4 lines. You'll find it
> issuing some command and running it through sed or whatever and then
> through the next stage of the pipe and basically *assuming* that
> command output formats never change and nothing can go wrong go wrong.

That's just how good unix utilties do their work. They never change
output formats except by some kind of request, usually a command option.
The defaults freeze at release, and if there's a different default
output, the utility name will change as well.

--
"Every new technology carries with it an opportunity to invent a new
crime." --Laurence Urgenson (an assistant chief US attorney),
speaking in 1987 about the first arrests for what was later called
cellphone cloning.
Re: redirect stdout and stderr to PHP variables? [message #176854 is a reply to message #176842] Sat, 28 January 2012 23:40 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 28.01.2012 13:03, schrieb crankypuss:
> On 01/27/2012 04:25 AM, M. Strobel wrote:
>> Am 27.01.2012 11:22, schrieb crankypuss:
>>> On 01/25/2012 03:52 PM, Peter H. Coffin wrote:
>>>> On Wed, 25 Jan 2012 12:47:11 -0700, crankypuss wrote:
>>>> > I'm sure there's a way to do this, probably some simple syntax I've not
>>>> > run into and am too stupid to find in the manual or through google-fu or
>>>> > otherwise.
>>>> >
>>>> > I'd like to issue an arbitrary shell command in a subroutine and have it
>>>> > return an array that contains one element representing stdout and
>>>> > another element representing stderr.
>>>> >
>>>> > For example, using tar to deal with a bazillion files, error messages
>>>> > might not be seen when specifying verbose output; I'd like to collect
>>>> > them and display after stdout.
>>>> >
>>>> > I've not found any syntax, yet, for redirecting to a PHP variable rather
>>>> > than some file.
>>>>
>>>> Sorry, you have to use the file, then read the file if you want to parse
>>>> back in what went out on stderr.
>>>
>>> Actually no, I do not have to use no steenking files, I might have to start writing
>>> something from scratch in freaking assembler, but I don't have to use no steenking
>>> files to do something that hasn't the least thing to do with files.
>>>
>>> Thank you though, and apologies for any crankiness that has leaked into my post...
>>> I'm going through the transition from many years of Windows to using Linux, and
>>> although I mostly like it, I find the tendency for scripts to rely on command output
>>> run through pipes to be abhorrent since the whole concept of validity checking goes
>>> out the window when you swallow that paradigm, and writing a file when it isn't
>>
>> Can you expand on that? There must be a misunderstanding, the command you run does
>> all validity checking, me thinks.
>
> Look at any bash script longer than about 4 lines. You'll find it issuing some
> command and running it through sed or whatever and then through the next stage of the
> pipe and basically *assuming* that command output formats never change and nothing
> can go wrong go wrong.
>

It just fits. And no magic, only some basic principles.

/Str.
Re: redirect stdout and stderr to PHP variables? [message #176856 is a reply to message #176825] Sun, 29 January 2012 04:56 Go to previous messageGo to next message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
Erwin Moller <erwinmollerusenet(at)xs4all(dot)nl> wrote:

> On 1/27/2012 11:23 AM, crankypuss wrote:
>> On 01/25/2012 03:56 PM, Curtis Dyer wrote:
>>> crankypuss<no(at)email(dot)thanks> wrote:

<snip>

>>>> I'd like to issue an arbitrary shell command in a subroutine
>>>> and have it return an array that contains one element
>>>> representing stdout and another element representing stderr.
>>>
>>> Have a look at `proc_open()' for more fine-grained control of
>>> a process:
>>>
>>> <http://php.net/proc_open>
>>>
>>>
>>> <snip>
>>
>> Third recommendation for proc_open() this morning, you guys
>> looking at each others' notes? <g> Thanks.
>
> I was wondering that too.
> When I posted my suggestion, I didn't see any others suggesting
> it. But today I saw that Jerry and Curtis wrote it one day
> earlier. I can imagine I had some mental black-out, but this
> looks like a mondial one. ;-)

In my case, I often get distracted before finishing a post and
usually end up forgetting to fetch new headers. I suppose it's a
good sign for the OP when several people agree on an approach.

<snip>

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
Re: redirect stdout and stderr to PHP variables? [message #176875 is a reply to message #176830] Tue, 31 January 2012 02:37 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/27/2012 11:37 AM, Peter H. Coffin wrote:
> On Fri, 27 Jan 2012 10:12:08 -0500, Jerry Stuckle wrote:
>> On 1/27/2012 10:00 AM, Erwin Moller wrote:
>>> Get a hobby, like computer programming. ;-)
>>>
>>> Regards,
>>> Erwin Moller
>>>
>>
>> ROFL!
>>
>> Ah, that's part of the problem, Erwin. I have a hobby. But around
>> here, airplane rental is $140/hr and up.
>>
>> Maybe what I need is a cheaper hobby :)
>
> 100LL ain't cheap either.
>

Around $5.30/gal, which is pretty good. It used to be about 2.5 or 3x
87 octane unleaded (auto).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: redirect stdout and stderr to PHP variables? [solved] [message #176890 is a reply to message #176799] Wed, 01 February 2012 12:22 Go to previous message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/25/2012 12:47 PM, crankypuss wrote:
> I'm sure there's a way to do this, probably some simple syntax I've not
> run into and am too stupid to find in the manual or through google-fu or
> otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and have it
> return an array that contains one element representing stdout and
> another element representing stderr.
>
> For example, using tar to deal with a bazillion files, error messages
> might not be seen when specifying verbose output; I'd like to collect
> them and display after stdout.
>
> I've not found any syntax, yet, for redirecting to a PHP variable rather
> than some file.
>
> tia.

Thanks for your help, folks. This seems to do the trick:


// executes $cmd as a separate process and returns the following:
// $result = null if unable to start process, else:
// $result["exit_code"] = exit code returned by $cmd
// $result["stdout"] = stdout written by $cmd
// $result["stderr"] = stderr written by $cmd
function sysCmd($cmd)
{
$result = array();
$descr = array();
$descr[0] = array("pipe", "r"); // stdin
$descr[1] = array("pipe", "w"); // stdout
$descr[2] = array("pipe", "w"); // stderr
$proc = proc_open($cmd, $descr, $pipes);
if (is_resource($proc))
{
fclose($pipes[0]); // no stdin data
$result["stdout"] = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$result["stderr"] = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$result["exit_code"] = proc_close($proc);
}
return $result;
}
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: enter search term via code?
Next Topic: FounderDating - Helping entrepreneurs find their co-founder & start companies!
Goto Forum:
  

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

Current Time: Thu Sep 19 23:52:07 GMT 2024

Total time taken to generate the page: 0.04116 seconds