&quot“ grep -v”怪异的行为

发布于 2025-01-22 02:58:14 字数 740 浏览 3 评论 0原文

我正在尝试确保我的脚本一次只能运行一次,但是我会遇到奇怪的行为。

我的代码:

echo "Current Pid: $$"
# Output "Current Pid: 5387"
echo "Process Count: $(pgrep -c -f "$0")"
# Output: Process Count: 2

if [ "$(pgrep -c -f "$0")" -gt 1 ]
then
    echo All Pids
    pgrep -f "$0"
    # Output "4978, 5387"

    echo Pids Current
    echo "$(pgrep -f "$0" |& grep -F $$)"
    # Output "5387"

    echo Pids Inverted
    echo "$(pgrep -f "$0" |& grep -Fv $$)"
    # Output "4978, 5394"

    # processPidsToKill="$(pgrep -f "$0" |& grep -Fv $$)"

    # for processPid in $processPidsToKill; do
    #     echo "Killing `basename "$0"` $processPid"
    #     kill "$processPid"
    # done
fi

有人可以告诉我我在做什么错吗? 新的PID来自我使用“ GREP -V”时?

I'm trying to make sure my script only runs once at a time, but im running into weird behavior with inverting grep.

My Code:

echo "Current Pid: $"
# Output "Current Pid: 5387"
echo "Process Count: $(pgrep -c -f "$0")"
# Output: Process Count: 2

if [ "$(pgrep -c -f "$0")" -gt 1 ]
then
    echo All Pids
    pgrep -f "$0"
    # Output "4978, 5387"

    echo Pids Current
    echo "$(pgrep -f "$0" |& grep -F $)"
    # Output "5387"

    echo Pids Inverted
    echo "$(pgrep -f "$0" |& grep -Fv $)"
    # Output "4978, 5394"

    # processPidsToKill="$(pgrep -f "$0" |& grep -Fv $)"

    # for processPid in $processPidsToKill; do
    #     echo "Killing `basename "$0"` $processPid"
    #     kill "$processPid"
    # done
fi

Can someone tell me what im doing wrong?
Where does the new Pid come from when im using "grep -v"?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文