解析 grep 输出
我正在尝试创建一个服务器管理器,这是迄今为止我所拥有的:
<?php
$COMMAND = shell_exec('ps ax --format command | grep skulltag');
$arr = explode("./",$COMMAND);
$text = shell_exec('pgrep -u doom');
$arrtext = preg_split('/\s+/', $text);
for( $i = 1; $i < count($arr); $i++ ) {
echo $i,". PROCESS ID ",$arrtext[$i]," Command issued: ",$arr[$i];
echo '<br>';
}
?>
如您所见,我用 ./ (文件执行)分隔 $COMMAND 字符串。然而,由于某种原因,在列表的末尾有这样的内容:
sh -c ps ax --format command | grep skulltag grep skulltag
这是供参考的完整输出:
- 进程 ID 4793 发出命令:skulltag-server
- 进程 ID 4956 发出命令:skulltag-server -port 13000
- 进程 ID 4958 发出命令:skulltag-server -port 13001 sh -c ps ax --format 命令| grep 头骨标签 grep 头骨标签
摆脱该行的最简单、最有效的方法是什么?我该怎么做?谢谢。
I'm trying to make a server manager and here is what I have so far:
<?php
$COMMAND = shell_exec('ps ax --format command | grep skulltag');
$arr = explode("./",$COMMAND);
$text = shell_exec('pgrep -u doom');
$arrtext = preg_split('/\s+/', $text);
for( $i = 1; $i < count($arr); $i++ ) {
echo $i,". PROCESS ID ",$arrtext[$i]," Command issued: ",$arr[$i];
echo '<br>';
}
?>
As you can see, I'm separating the $COMMAND string with ./ (file execution). However, for some reason at the end of the list there's this:
sh -c ps ax --format command | grep skulltag grep skulltag
Here is the full output for reference:
- PROCESS ID 4793 Command issued: skulltag-server
- PROCESS ID 4956 Command issued: skulltag-server -port 13000
- PROCESS ID 4958 Command issued: skulltag-server -port 13001 sh -c
ps ax --format command | grep skulltag grep skulltag
What would be the easiest and most effective way to get rid of that line, and how would I do it? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其更改
为: 这样
,grep 命令本身包含字符串“[s]kultag”,该字符串与 grep 正则表达式“[s]kultag”不匹配。
另外,有两个建议: 1. 不能保证您的初始 ps | grep 和你后面的 pgrep 将排队。相反,使用单个 pgrep:
2. for 循环以 1 开头,这将跳过 arr[0] 中的过程。
你的 php 可以重写如下:
Change this:
To this:
That way, the grep command itself contains the string '[s]kultag', which is not matched by the grep regular expression '[s]kultag'.
Also, two suggestions: 1. there's no guarantee that your initial ps | grep and your later pgrep will line up. Instead, use a single pgrep:
And 2. your for loop starts with 1, which will skip the process in arr[0].
Your php could be rewritten something like this:
我的快速而肮脏的解决方案是附加
| grep -v grep
命令。My quick and dirty solution is to append
| grep -v grep
to the command.