0

if pgrep apache; then echo "oliver"; fi
oliver如果命令pgrep apache不为空, 这将回显。我想做相反的事情。如果命令pgrep apache返回空字符串,请运行命令。

4

3 回答 3

4
if ! pgrep apache; then echo "oliver"; fi
于 2013-02-18T19:18:15.063 回答
1

尝试这样做:

pgrep &>/dev/null apache || echo "foobar"

或者 :

if ! pgrep &>/dev/null apache; then echo "foobar"; fi

!代表

这不是基于命令的输出,而是基于命令 whatruefalse.

中,当一个命令的返回码为 时0,就认为是true,大于0就认为是false。您可以使用变量检查此返回码$?,例如:

ls /path/to/inexistant/file
echo $?

查看真假命令

于 2013-02-18T19:15:31.170 回答
1

我不确定上下文,但假设您想要做某事某个特定的过程是或未找到。

bash-3.2$ pgrep -q bash &&  echo "bash was found"
bash was found

bash-3.2$ pgrep -q XXXbash ||  echo "XXXbash was NOT was found"
XXXbash was NOT was found
于 2013-02-18T20:41:43.103 回答