formatting linux command output in php

时间:2023-01-14 15:56:47

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.

FAXSTAT是用于hylafax监视通道状态的命令。

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.

现在我想用通道17修改调制解调器ttyIAX017(+1.999.555.1212)。我想对具有相应通道号的所有通道执行相同操作。有没有办法做到这一点。

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

我在谷歌搜索了很多但找不到相关的东西。

Please help.

3 个解决方案

#1


0  

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.

substr($ line,13,2)计算在“Channel”之后打印的两个数字,substr($ line,33)隐藏我们不想打印的字符串部分。

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

如果需要,您可能想要更改数字13,2和33。

#2


0  

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.

#3


0  

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:

我正在研究php手册,找到了我想要的东西。我通过添加以下代码跳过了数组中的第一行:

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

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

echo“Channel”.substr($ line,12,3)。“”。sububtr($ line,33)。“\ n”;

Thank you sp2hari for your help.

谢谢sp2hari的帮助。

#1


0  

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.

substr($ line,13,2)计算在“Channel”之后打印的两个数字,substr($ line,33)隐藏我们不想打印的字符串部分。

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

如果需要,您可能想要更改数字13,2和33。

#2


0  

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.

#3


0  

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:

我正在研究php手册,找到了我想要的东西。我通过添加以下代码跳过了数组中的第一行:

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

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

echo“Channel”.substr($ line,12,3)。“”。sububtr($ line,33)。“\ n”;

Thank you sp2hari for your help.

谢谢sp2hari的帮助。