How to make "if not true condition"?
Solution 1
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
== 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.
Solution 2
I think it can be simplified into:
grep sysa /etc/passwd || {
echo "ERROR - The user sysa could not be looked up"
exit 2
}
or in a single command line
$ grep sysa /etc/passwd || { echo "ERROR - The user sysa could not be looked up"; exit 2; }
Solution 3
This one
if [[ ! $(cat /etc/passwd | grep "sysa") ]]
Then echo " something"
exit 2
fi
Solution 4
What am I doing wrong?
$(...)
holds the value, not the exit status, that is why this approach is wrong. However, in this specific case, it does indeed work because sysa
will be printed which makes the test statement come true. However, if ! [ $(true) ]; then echo false; fi
would always print false
because the true
command does not write anything to stdout (even though the exit code is 0). That is why it needs to be rephrased to if ! grep ...; then
.
An alternative would be cat /etc/passwd | grep "sysa" || echo error
. Edit: As Alex pointed out, cat is useless here: grep "sysa" /etc/passwd || echo error
.
Found the other answers rather confusing, hope this helps someone.
Solution 5
simply:
if ! examplecommand arg1 arg2 ...; then
#code block
fi
without any brackets.
Sandra Schlichting
Updated on July 08, 2022Comments
-
Sandra Schlichting almost 2 years
I would like to have the
echo
command executed whencat /etc/passwd | grep "sysa"
is not true.What am I doing wrong?
if ! [ $(cat /etc/passwd | grep "sysa") ]; then echo "ERROR - The user sysa could not be looked up" exit 2 fi
-
0zkr PM over 9 yearsNice, but I prefer the Mr. shellter answer because is "self documented", is more "readable" the programmer intention.
-
Orwellophile almost 9 yearsIt's all rather silly really, from my answer to a much harder (question)[stackoverflow.com/a/30400327/912236]
grep "^$user:" /etc/passwd
would be the more correct way to search /etc/passwd incidently –grep -v
where -v inverts the search if you wanted to avoid the mess of || -
shellter almost 9 yearsyes, well there's solving a problem most efficiently, and then there is answering specific question. I've tried to answer the specific question. Thanks for your ideas. Good luck to all.
-
Orwellophile almost 9 yearsnot picking on your answers, quite enjoyed them. i just through i would throw in a properly bounded check on username, else if the OP really does search on "sys" or somesuch, he'll get quite the surprise. one more for the road?
(( $( cat file | grep regex | wc -l ) ? 0 : 1 ))
-
Julien over 8 yearsI like this version. What about adding
1>&2
at the end of yourecho
to print onstderr
? -
ceving about 8 years@0zkrPM But the shellter version does not work in Bourne shell. You will get
!: not found
-
tbc0 almost 8 yearsAvoid output redirection when using
'grep
like this.-q
suppresses output. -
Kusalananda over 6 years@SDsolar Your code is probably not executed by
bash
in that case. -
DocWeird over 4 yearsGreat! For some reason reqular "! grep -qs..." did not work with /proc/mounts and trying to find out if a regularly dropping USB-disk was mounted on Raspbian 4.9 kernel. This one did the job perfectly!
-
FluffyKitten over 3 yearsWelcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it improves on the other upvoted answers this question already has, so that it is useful to other users with similar issues.