解析 grep 输出

发布于 2024-12-05 23:32:51 字数 840 浏览 0 评论 0原文

我正在尝试创建一个服务器管理器,这是迄今为止我所拥有的:

<?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 

这是供参考的完整输出:

  1. 进程 ID 4793 发出命令:skulltag-server
  2. 进程 ID 4956 发出命令:skulltag-server -port 13000
  3. 进程 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:

  1. PROCESS ID 4793 Command issued: skulltag-server
  2. PROCESS ID 4956 Command issued: skulltag-server -port 13000
  3. 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 技术交流群。

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

发布评论

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

评论(2

忘你却要生生世世 2024-12-12 23:32:51

将其更改

ps ax --format command | grep skulltag

为: 这样

ps ax --format command | grep [s]kulltag

,grep 命令本身包含字符串“[s]kultag”,该字符串与 grep 正则表达式“[s]kultag”不匹配。

另外,有两个建议: 1. 不能保证您的初始 ps | grep 和你后面的 pgrep 将排队。相反,使用单个 pgrep:

pgrep -afl skulltag

2. for 循环以 1 开头,这将跳过 arr[0] 中的过程。

你的 php 可以重写如下:

$processes = explode("\n", shell_exec('pgrep -afl skulltag'));
foreach($processes as $i => $process) {
    ($pid, $command) = explode(' ',$process,2);
    echo $i+1,". PROCESS ID ",$pid," Command issued: ",$command;
    echo '<br>';
}

Change this:

ps ax --format command | grep skulltag

To this:

ps ax --format command | grep [s]kulltag

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:

pgrep -afl skulltag

And 2. your for loop starts with 1, which will skip the process in arr[0].

Your php could be rewritten something like this:

$processes = explode("\n", shell_exec('pgrep -afl skulltag'));
foreach($processes as $i => $process) {
    ($pid, $command) = explode(' ',$process,2);
    echo $i+1,". PROCESS ID ",$pid," Command issued: ",$command;
    echo '<br>';
}
一身软味 2024-12-12 23:32:51

我的快速而肮脏的解决方案是附加 | grep -v grep 命令。

My quick and dirty solution is to append | grep -v grep to the command.

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