Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
if pgrep apache; then echo "oliver"; fioliver如果命令pgrep apache不为空, 这将回显。我想做相反的事情。如果命令pgrep apache返回空字符串,请运行命令。
if pgrep apache; then echo "oliver"; fi
oliver
pgrep apache
if ! pgrep apache; then echo "oliver"; fi
尝试这样做:
pgrep &>/dev/null apache || echo "foobar"
或者 :
if ! pgrep &>/dev/null apache; then echo "foobar"; fi
!代表不
!
这不是基于命令的输出,而是基于命令 whatrue或false.
true
false
在shell中,当一个命令的返回码为 时0,就认为是true,大于0就认为是false。您可以使用变量检查此返回码$?,例如:
0
$?
ls /path/to/inexistant/file echo $?
查看真假命令
我不确定上下文,但假设您想要做某事某个特定的过程是或未找到。
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