bash - Prevent grep returning an error when input doesn't match -
i want write in bash script piece of code checks if program running. have following in order search whether bar running
foo=`ps -ef | grep bar | grep -v grep`
the
grep -v grep
part ensure "grep bar" not taken account in ps results
when bar isn't running, foo correctly empty. problem lies in fact tha script has
set -e
which flag terminate script if command returns error. turns out when bar isn't running, "grep -v grep" doesn't match , grep returns error. tried using -q or -s no avail.
is there solution that? thx
sure:
ps -ef | grep bar | { grep -v grep || true; }
or even:
ps -ef | grep bar | grep -v grep | cat
Comments
Post a Comment