Reputation: 38207
How do you negate a test in bash if you want to combine multiple tests?
The code is
if ! [ $(pgrep Xvfb) ] || [ ! -v DISPLAY ]; then
echo starting xvfb
mkdir -p /tmp/xvfb
Xvfb :1 -fbdir /tmp/xvfb > /tmp/xvfb_output 2>&1 &
export DISPLAY=:1
fi
It is supposed to be sourced and to start Xvfb if not already running.
Previously, it lacked the || [ ! -v DISPLAY ]
part to check the existence of said variable.
To negate a test, you could do either ! [ ... ]
or [ ! ... ]
, which both seem to work.
Is the reasoning correct that you should use [ ! ...]
because it is inside the test and thus clearer (and a bit more efficient)?
Upvotes: 3
Views: 5267
Reputation: 1844
I'm pretty sure it doesn't matter where you put the negation in terms of efficiency. As for readability, you could write your if
statement like this:
if ! (test "$(pgrep Xvfb)" -a -n "${DISPLAY:+1}"); then
This way you have only one negation on one test.
I agree with you that
if ! [ $(pgrep Xvfb) ] || [ ! -v DISPLAY ]; then
is ambiguous and
if [ ! $(pgrep Xvfb) ] || [ ! -v DISPLAY ]; then
is not.
Upvotes: 6