如果PS Aux | grep'name ofService'' | grep -v grep>/dev/null

发布于 2025-01-24 10:10:21 字数 131 浏览 3 评论 0原文

我是《外壳》脚本的初学者,我对写作后的事情感到困惑。 /dev/null如果

ps aux | grep "nameOfService" | grep -v grep >/dev/null ?

I am beginner in shell scripting and i am confused about the thing that after writing > /dev/null what will be the value of if

ps aux | grep "nameOfService" | grep -v grep >/dev/null ?

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

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

发布评论

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

评论(1

哽咽笑 2025-01-31 10:10:21

默认情况下,管道的返回值是最后一个命令的返回值(在这种情况下,第二个grep命令)。请注意,PipeFail Shell选项会更改此行为。

重定向grep的输出不会更改返回值。

您可能正在寻找类似的内容:

if ps aux | grep -qw 'nameOfServic[e]'; then
    echo service appears to be running
else
    echo does not appear to be running
fi

您应该使用-w匹配整个单词。我将使用awk -v'n = nameOfService''$ 4 == n'而不是grep,但是解析ps ps输出不可靠反正。例如,PS通常将过程名称截断为16个字符。

By default, the return value of a pipeline is the return value of the last command (in this case, the second grep command). Note that the pipefail shell option changes this behaviour.

Redirecting the output of grep doesn't change the return value.

You are probably looking for something like:

if ps aux | grep -qw 'nameOfServic[e]'; then
    echo service appears to be running
else
    echo does not appear to be running
fi

You should use -w to match the whole word. I would use awk -v 'n=nameOfService' '$4==n' instead of grep, however parsing ps output like this not reliable anyway. For example, ps usually truncates process names to 16 characters.

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