在 php 中格式化 linux 命令输出

发布于 2024-10-07 03:56:36 字数 963 浏览 5 评论 0原文

我有以下脚本可以执行我想要的操作

<?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 是 hylafax 中用于监视通道状态的命令。

示例输出如下:

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 

现在我想用通道 17 修改 Modem ttyIAX017 (+1.999.555.1212)。 我想对具有各自频道编号的所有频道执行相同的操作。有没有办法做到这一点。

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

请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍孤女 2024-10-14 03:56:36

替换显示

echo "$line \n";

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

How does this work? 的

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

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

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.

筱武穆 2024-10-14 03:56:36

谢谢您,您的解决方案有效。
我只需修改脚本以每行显示一个通道,如下所示:

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

还有一种方法可以跳过第一行并使用您为其余行提供的代码。
示例:正常输出为:

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

首选输出应该是:

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.

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.

记忆之渊 2024-10-14 03:56:36

我正在查看 php 手册并找到了我想要的。
我通过添加以下代码跳过了数组中的第一行:

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

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

谢谢 sp2hari 的帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文