369

I would like to have the echo command executed when cat /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
codeforester
  • 28,846
  • 11
  • 78
  • 104
Sandra Schlichting
  • 22,478
  • 28
  • 91
  • 145

6 Answers6

519

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.

heemayl
  • 32,535
  • 3
  • 52
  • 57
shellter
  • 33,781
  • 7
  • 75
  • 89
  • 1
    It's all rather silly really, from my answer to a much harder (question)[http://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 || – Orwellophile Jun 01 '15 at 16:59
  • 1
    yes, 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. – shellter Jun 01 '15 at 17:05
  • 2
    not 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 ))` – Orwellophile Jun 01 '15 at 17:12
  • 1
    Great! 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! – DocWeird Nov 27 '19 at 12:51
35

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; }

Rony
  • 1,514
  • 10
  • 10
  • 4
    Nice, but I prefer the Mr. shellter answer because is "self documented", is more "readable" the programmer intention. – 0zkr PM Sep 18 '14 at 22:03
  • 1
    I like this version. What about adding `1>&2` at the end of your `echo` to print on `stderr` ? – Julien Nov 26 '15 at 11:32
  • 3
    @0zkrPM But the shellter version does not work in Bourne shell. You will get `!: not found` – ceving Mar 14 '16 at 13:29
  • 1
    Avoid output redirection when using `'grep` like this. `-q` suppresses output. – tbc0 Jul 26 '16 at 22:04
9

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.

MountainDrew
  • 361
  • 2
  • 16
phil294
  • 8,501
  • 7
  • 55
  • 84
6

This one

if [[ !  $(cat /etc/passwd | grep "sysa") ]]
Then echo " something"
exit 2
fi
Tareq
  • 69
  • 1
  • 2
  • 6
    Welcome 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. – FluffyKitten Sep 17 '20 at 08:32
2

Here is an answer by way of example:

In order to make sure data loggers are online a cron script runs every 15 minutes that looks like this:

#!/bin/bash
#
if ! ping -c 1 SOLAR &>/dev/null
then
  echo "SUBJECT:  SOLAR is not responding to ping" | ssmtp abc@def.com
  echo "SOLAR is not responding to ping" | ssmtp 4151112222@txt.att.com
else
  echo "SOLAR is up"
fi
#
if ! ping -c 1 OUTSIDE &>/dev/null
then
  echo "SUBJECT:  OUTSIDE is not responding to ping" | ssmtp abc@def.com
  echo "OUTSIDE is not responding to ping" | ssmtp 4151112222@txt.att.com
else
  echo "OUTSIDE is up"
fi
#

...and so on for each data logger that you can see in the montage at http://www.SDsolarBlog.com/montage


FYI, using &>/dev/null redirects all output from the command, including errors, to /dev/null

(The conditional only requires the exit status of the ping command)

Also FYI, note that since cron jobs run as root there is no need to use sudo ping in a cron script.

SDsolar
  • 1,781
  • 2
  • 18
  • 29
1

On Unix systems that supports it (not macOS it seems):

if getent passwd "$username" >/dev/null; then
    printf 'User %s exists\n' "$username"
else
    printf 'User %s does not exist\n' "$username"
fi 

This has the advantage that it will query any directory service that may be in use (YP/NIS or LDAP etc.) and the local password database file.


The issue with grep -q "$username" /etc/passwd is that it will give a false positive when there is no such user, but something else matches the pattern. This could happen if there is a partial or exact match somewhere else in the file.

For example, in my passwd file, there is a line saying

build:*:21:21:base and xenocara build:/var/empty:/bin/ksh

This would provoke a valid match on things like cara and enoc etc., even though there are no such users on my system.

For a grep solution to be correct, you will need to properly parse the /etc/passwd file:

if cut -d ':' -f 1 /etc/passwd | grep -qxF "$username"; then
    # found
else
    # not found
fi

... or any other similar test against the first of the :-delimited fields.

Kusalananda
  • 12,623
  • 3
  • 33
  • 46