Negate If Condition in Bash Script

Negate if condition in bash script

You can choose:

if [[ $? -ne 0 ]]; then       # -ne: not equal

if ! [[ $? -eq 0 ]]; then # -eq: equal

if [[ ! $? -eq 0 ]]; then

! inverts the return of the following expression, respectively.

Where to negate logical expression in bash

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.

How to make if not true condition ?

try

if ! grep -q sysa /etc/passwd ; then

grep returns true if it finds the search target, and false if it doesn't.

So NOT false (! false) == true.

if evaluation in shells are designed to be very flexible, and many times doesn't require chains of commands (as you have written).

Also, looking at your code as is, your use of the $( ... ) form of cmd-substitution is to be commended, but think about what is coming out of the process. Try echo $(cat /etc/passwd | grep "sysa") to see what I mean. You can take that further by using the -c (count) option to grep and then do if ! [ $(grep -c "sysa" /etc/passwd) -eq 0 ] ; then which works but is rather old school.

BUT, you could use the newest shell features (arithmetic evaluation) like

if ! (( $(grep -c "sysa" /etc/passwd) == 0 )) ; then ...`

which also gives you the benefit of using the c-lang based comparison operators, ==,<,>,>=,<=,% and maybe a few others.

In this case, per a comment by Orwellophile, the arithmetic evaluation can be pared down even further, like

if ! (( $(grep -c "sysa" /etc/passwd) )) ; then ....

OR

if (( ! $(grep -c "sysa" /etc/passwd) )) ; then ....

Finally, there is an award called the Useless Use of Cat (UUOC). :-) Some people will jump up and down and cry gothca! I'll just say that grep can take a file name on its cmd-line, so why invoke extra processes and pipe constructions when you don't have to? ;-)

I hope this helps.

Negating multiple conditions in Bash

The test shell builtin [ supports the arguments -a and -o which are logical AND and OR respectively.

#!/bin/sh 

if [ -n "$1" -a -n "$2" ]
then echo "both arguments are set!"
fi

Here I use -n to check that the strings are non-zero length instead of -z which shows that they are zero length and therefore had to be negated with a !.

" -n string True if the length of string is nonzero."

"-z string True if the length of string is zero."

If you are using bash it also supports a more powerful type of test [[]] which can use the boolean operators || and &&:

#!/bin/bash 

if [[ -n "$1" && -n "$2" ]]
then echo "both arguments are set!"
fi

In comments it was addressed that none of these samples show how to negate multiple tests in shell, here is an example which does:

#!/bin/sh 

if [ ! -z "$1" -a ! -z "$2" ]
then echo "both arguments are set!"
fi

Negate a command return value condition in bash

Simply use ! to to check if it failed:

if ! pgrep "$NAME" >> /dev/null; then
# stuff
fi

Does the Exclamation point represent negation in Bash IF condition?

As documented in man bash:

If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above.

How to negate a program's error code for an if statement in fish shell?

You're looking for the not keyword:

if not false
echo Foo
end


Related Topics



Leave a reply



Submit