当我将服务传输到grep时 - status-all命令时,为什么在输出中显示额外的线?

发布于 2025-02-13 05:52:02 字数 307 浏览 1 评论 0原文

例如,

sudo service --status-all | grep -oE 'php[0-9]+.[0-9]+'

生成以下输出。

 [ ? ]  hwclock.sh
 [ ? ]  networking
php7.0
php7.3

我的目标是提取另一个软件包的版本,并将其放入配置脚本中,以便如果该软件包得到升级或降级,则该脚本不会破坏。如果我对正则表达式和管道操作员的理解是正确的,则前两行甚至不应显示在输出中。

谁能向我解释为什么会发生这种情况?

For example,

sudo service --status-all | grep -oE 'php[0-9]+.[0-9]+'

generates the following output.

 [ ? ]  hwclock.sh
 [ ? ]  networking
php7.0
php7.3

My goal is to extract the version of another software package and put it into a configuration script so that the script won't break if that package gets upgraded or downgraded. If my understanding of regular expressions and the piping operator is correct, the first two lines shouldn't even show up in the output.

Can anyone explain to me why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鸩远一方 2025-02-20 05:52:02

将输出重定向到STDERR消除了不需要的线路。我还将输出输送到 tail -1 仅获取最后一行 - 具有最新版本的一行。

sudo service --status-all 2>/dev/null | grep -oE 'php[0-9]+.[0-9]+' | tail -1

Redirecting the output to stderr eliminated the unwanted lines. I also piped the output to tail -1 to get the last line only--the one with the latest version.

sudo service --status-all 2>/dev/null | grep -oE 'php[0-9]+.[0-9]+' | tail -1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文