neovox Posted April 30, 2008 Share Posted April 30, 2008 I have a question about handling output from exec(). I'm using exec() to run an expect script against a solaris box, with the output ($result) echoing out to the user's screen. The information that is returned contains about 14 lines of text, however I only want to display one of these lines back to the user. The script I'm using now is <? $result = null; $cmd = "/path/script $ENT"; print"<pre>"; print"\n"; exec($cmd, $result); for($i = 1; $i < (count($result) - 1); $i++) { print $result[$i] . "\n"; } ?> This script returns: Retrieving data... Please wait... Enterprise Name Parameter1: valueX Parameter2: valueY <snip> Parameter14: valueZ What I'd like to do is only display "Parameter2: valueY" for instance, instead of the entire output. Thanks. Link to comment https://forums.phpfreaks.com/topic/103557-handling-exec-output/ Share on other sites More sharing options...
rhodesa Posted April 30, 2008 Share Posted April 30, 2008 try... <?php $result = null; $cmd = "/path/script $ENT"; print"<pre>\n"; exec($cmd, $result); foreach($result as $line) { if(substr($line,0,11) == 'Parameter2:') print "$line\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/103557-handling-exec-output/#findComment-530330 Share on other sites More sharing options...
neovox Posted April 30, 2008 Author Share Posted April 30, 2008 Thanks for the reply. Using the sample you provided isn't returning anything, however, it may be due to the formatting of what is being returned. My code returns: Retrieving data... Please wait... Enterprise Name Call Logging = Disabled Description = Company Name Intra-LATA PIC = NILCAC Inter-LATA PIC = NILCAC International PIC = NILCAC Routing profile = Hosting LCA ID = null Source ID = null SourceidForEAPrefix= null Private policies = {voiceVPN, ExtDialing} 1 entry found. What I'd like it to return is just the entire line "Routing profile = Hosting" Could it be the preceding spaces that are causing the foreach not to return anything? My foreach statement looks like this: foreach($result as $line) { if(substr($line,0,11) == 'Routing profile') print "$line\n"; } I also tried it with the preceding spaces, but still got nothing back. Thanks for the assistance. Link to comment https://forums.phpfreaks.com/topic/103557-handling-exec-output/#findComment-530452 Share on other sites More sharing options...
rhodesa Posted April 30, 2008 Share Posted April 30, 2008 <?php $search = "Routing profile"; //Set this to what you are looking for $result = null; $cmd = "/path/script $ENT"; print"<pre>\n"; exec($cmd, $result); foreach($result as $line) { if(strstr($line,$search) !== FALSE) print "$line\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/103557-handling-exec-output/#findComment-530516 Share on other sites More sharing options...
neovox Posted April 30, 2008 Author Share Posted April 30, 2008 rhodesa, thanks a ton. That worked perfectly. Link to comment https://forums.phpfreaks.com/topic/103557-handling-exec-output/#findComment-530524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.