开发者

formatting linux command output in php

开发者 https://www.devze.com 2023-01-29 21:10 出处:网络
I have the following script which does what I want <?php $data = array();// define array exec(\'faxstat -s\', $data, $ret);// execute command, output is array

I have the following script which does what I want

<?php
$data = array();                // define array

exec('faxstat -s', $data, $ret);     // execute command, output is array
echo "<pre>";

if ($ret == 0) {                // check status code. if successful
    foreach ($data  as $line)  {  // process array line by line
    echo "$line \n";

    }
} else {
    echo "Error in command";    // if unsuccessful display error
}

echo "</pre>";
?>

FAXSTAT is a command used in hylafax for monitoring channel status.

Sample output is as follows:

Modem ttyIAX017 (+1.999.555.1212): Running and idle 
Modem ttyIAX018 (+1.999.555.1212): Running and idle 
Modem ttyIAX019 (+1.999.555.1212): Running and idle开发者_开发技巧 
Modem ttyIAX020 (+1.999.555.1212): Running and idle 
Modem ttyIAX021 (+1.999.555.1212): Running and idle 

Now I want to modify Modem ttyIAX017 (+1.999.555.1212) with Channel 17. I want to do the same for all the channels with respective channel number. Is there a way to do this.

I have searched a lot on google but could not find anything relevant.

Please help.


Replace the line which says

echo "$line \n";

to

echo "Channel ".substr($line, 13, 2)." ".substr($line, 33);

How does this work?

substr($line, 13, 2) calculates the two digits to print after "Channel" and substr($line, 33) hides the part of string we don't want to print.

You may want to change the number 13, 2 and 33 if need arises.


Thank you and your solution works. I just had to modify the script to show one channel per line as below:

echo "Channel ".substr($line, 12, 3)." ".substr($line, 33)." \n";

Also is there a way to skip First Line and use the code given by you for the rest of the lines. Example: Normal Output would be:

HylaFAX scheduler on Server.domain.pvt: Running
Modem boston18 (+1.999.555.1212): Running and idle
Modem boston10 (+1.999.555.1212): Running and idle

Preferred output should be:

HylaFAX scheduler on Server.domain.pvt: Running
Channel 01: Running and idle
Channel 02: Running and idle
Channel 03: Running and idle
If possible i would like to skip the first line and show remaining channel info one per line.


I was looking into php manual and found what i want. I have skipped the first line in the array by adding the below code:

    if(!isset($flag_first)) { //skip first line from array
    $flag_first=1;
    continue;}

echo "Channel ".substr($line, 12, 3)." ".substr($line, 33)." \n";

Thank you sp2hari for your help.

0

精彩评论

暂无评论...
验证码 换一张
取 消