header("Content-type: text/plain");
// tell php to automatically flush after every output
// including lines of output produced by shell commands
disable_ob();
$command = 'sudo rsync -arz --progress /home/lokesh/Desktop /home/lokesh/Downloads --stats';
system($command);
function disable_ob() {
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);
// Implicitly flush the buffer(s)
//ini_set('implicit_flush', true);
ob_implicit_flush(true);
// Clear, and turn off output buffering
while (ob_get_level() > 0) {
// Get the curent level
$level = ob_get_level();
// End the buffering
ob_end_clean();
// If the current level has not changed, abort
if (ob_get_level() == $level) break;
}
// Disable apache output buffering/compression
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', '1');
apache_setenv('dont-vary', '1');
}
}
我的代码上面的给了我这样的输出。
输出
桌面/ 桌面/ CloudRDP.bat
336 100% 0.00kB/s 0:00:00
336 100% 0.00kB/s 0:00:00 (xfr#1, ir-chk=1055/1057)
桌面/星期几歌曲 - 一周7天 - 学习站的儿童歌曲.mp4
32,768 0% 888.89kB/s 0:00:22
5,308,416 27% 5.06MB/s 0:00:02
5,636,096 28% 2.41MB/s 0:00:05
10,846,208 55% 3.20MB/s 0:00:02
15,826,944 80% 3.56MB/s 0:00:01
19,611,127 100% 3.75MB/s 0:00:04 (xfr#2, ir-chk=1054/1057)
Desktop / EnjayEsync Ver 2.4.1.rar
32,768 1% 42.61kB/s 0:00:49
1,277,952 59% 1.22MB/s 0:00:00
2,135,855 100% 1.73MB/s 0:00:01 (xfr#3, ir-chk=1053/1057)
桌面/ EnjayOnlineProductRegistration.sql
32,768 13% 181.82kB/s 0:00:01
235,670 100% 1.05MB/s 0:00:00 (xfr#4, ir-chk=1052/1057)
但我想通过搜索“ xfr#”字符串计算文件传输次数。这怎么可能。我试图循环但无法做到这一点。获得此字符串后的意思( xfr#4 ),它显示输出4。
答案 0 :(得分:0)
如果您不仅需要命令输出的最后一行,而不是system — Execute an external program and display the output —使用popen — Opens process file pointer —(逐行阅读)或exec — Execute an external program(以获取一个充满每一行输出的数组。)