[英]formatting linux command output in php
我有以下腳本可以滿足我的需求
<?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)
。我想對具有相應通道號的所有通道執行相同的操作。 有沒有辦法做到這一點。
我在Google上進行了很多搜索,但找不到任何相關內容。
請幫忙。
替換顯示以下內容的行
echo "$line \n";
至
echo "Channel ".substr($line, 13, 2)." ".substr($line, 33);
這是如何運作的?
substr($ line,13,2)計算“ Channel”后要打印的兩位數,而substr($ line,33)隱藏了我們不想打印的字符串部分。
如果需要,您可能需要更改數字13、2和33。
謝謝您,您的解決方案有效。 我只需要修改腳本以顯示每行一個頻道,如下所示:
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如果可能的話,我想跳過第一行,每行顯示剩余的頻道信息。
我一直在尋找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的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.