Reply
capturing the STDERR of a system command
Old 08-24-2004, 04:22 PM capturing the STDERR of a system command
mod
mod's Avatar
Novice Talker

Posts: 13
Trades: 0
for example,
PHP Code:
<? $output = `ls adir/`; echo "$output<br>";?>
if `adir' doesn't exist, I would like to see the message saying it doesn't exist, right? I know i can use system() to get the return code, but I'm using so many different commands changing the numbers to messages would be nontrivial.
mod is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 08-25-2004, 04:39 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Try opeining it in a pipe its the best way to get close to your external binary.
When your wishing to communicate with your binaries a simple system or exec will suffice as long as you are not dependant on information returned by that binary.

But say with something like gpg where you could have a process that goes through several stages then opening a pipe to that binary is your best bet, you could then take action on the returned info.

PHP Code:
<?php
/* Add 2>&1 so we can get stderr. */
$handle popen('ls adir/ 2>&1''r');
echo 
"'$handle'; " gettype($handle) . "\n";
while(
$read fread($handle2096)){
  echo 
$read;
}
pclose($handle);
?>
$output = `ls adir/`; echo "$output<br>";

All this will do is display ls adir/ .

Make sure you have permissions to view the dir or it wont work. test on /tmp

Ibbo

Last edited by ibbo; 08-25-2004 at 04:44 AM..
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 08-25-2004, 10:58 AM
mod
mod's Avatar
Novice Talker

Posts: 13
Trades: 0
OK Thanks!

the 2>&1 part was what i was looking for, that idea completely escaped me.

just to be clear:
Quote:
Originally Posted by ibbo
$output = `ls adir/`; echo "$output<br>";

All this will do is display ls adir/ .
around ls adir/ are backticks, not single quotes. so my command would change to:

$output = `ls adir 2>&1`;

and it works fine. the output redirection is a shell function though, will it work cross platform? I've only tested it on a linux server so far.

Last edited by mod; 08-25-2004 at 11:00 AM..
mod is offline
Reply With Quote
View Public Profile
 
Old 08-25-2004, 11:03 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Oh so they are. Still never mind it works.

comes in handy that 2>&1 eh, works wonderfully in shell scripts but alas I dont know if it will do the same thing under windows. I cant confirm nor deny because I dont use it.

Ibbo
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Reply     « Reply to capturing the STDERR of a system command
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.09416 seconds with 13 queries