Note that, for PIPE
being any command or sequence of commands, then:
if PIPE ; then
# do one thing if PIPE returned with zero status ($?=0)
else
# do another thing if PIPE returned with non-zero status ($?!=0), e.g. error
fi
For the record, [ expr ]
is a shell builtin† shorthand for test expr
.
Since grep
returns with status 0 in case of a match, and non-zero status in case of no matches, you can use:
if grep -lq '^MYSQL_ROLE=master' ; then
# do one thing
else
# do another thing
fi
Note the use of -l
which only cares about the file having at least one match (so that grep
returns as soon as it finds one match, without needlessly continuing to parse the input file.)
†on some platforms [ expr ]
is not a builtin, but an actual executable /bin/[
(whose last argument will be ]
), which is why [ expr ]
should contain blanks around the square brackets, and why it must be followed by one of the command list separators (;
, &&
, ||
, |
, &
, newline)