在 php 中格式化 linux 命令输出
我有以下脚本可以执行我想要的操作
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
替换显示
How does this work? 的
行 substr($line, 13, 2) 计算“Channel”之后要打印的两位数字,并且 substr($line, 33) 隐藏字符串部分我们不想打印。
如果需要,您可能需要更改数字 13、2 和 33。
Replace the line which says
to
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.
谢谢您,您的解决方案有效。
我只需修改脚本以每行显示一个通道,如下所示:
还有一种方法可以跳过第一行并使用您为其余行提供的代码。
示例:正常输出为:
首选输出应该是:
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:
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:
Preferred output should be:
If possible i would like to skip the first line and show remaining channel info one per line.
我正在查看 php 手册并找到了我想要的。
我通过添加以下代码跳过了数组中的第一行:
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:
echo "Channel ".substr($line, 12, 3)." ".substr($line, 33)." \n";
Thank you sp2hari for your help.