gpt4 book ai didi

shell - 在 if 语句中使用 grep

转载 作者:行者123 更新时间:2023-12-02 18:20:27 25 4
gpt4 key购买 nike

我是 shell 脚本编写的初学者,需要一个脚本来检查进程的状态,并在启动时提供“正在运行”的输出,在关闭时提供“未运行”的输出。

我尝试了以下方法,但没有成功:

if [ -s `/opt/reds/recycle.sh status | grep "pid"` ];
then
echo "running"
else
echo "not running"
fi

有人可以帮我吗?

最佳答案

if 语句更改为:

if /opt/reds/recycle.sh status | grep "pid"
then
...
fi

您可能想要静默 grep:

-q 用于安静操作。
-s 抑制错误消息(这里不需要,因为 grep 正在从 stdin 获取输入)。

一般来说,使用 if 检查命令的退出状态:

if command;then
...
fi

关于shell - 在 if 语句中使用 grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com