Using bash I am trying to query /etc/passwd for any users with an id over 1000. If they exist do something, else do something else. I'm stumped. Any help is appreciated.

if [ "$(id -u)" -gt "1000" </etc/passwd]; then
    do something
    do something else
  • 1
    Does this have to be in bash (shell)? – Stephen Rauch Mar 26 '17 at 15:20
  • yes, sorry. using bash – user3239222 Mar 26 '17 at 15:23

Try this:

if grep -E '^[^:]*:[^:]*:[^:]{4}' /etc/passwd | grep -Evq '^[^:]*:[^:]*:1000:'

The first grep searches passwd for lines with a uid of four or more digits. The second grep filters out the line with uid 1000. The exit status will be 0 if any lines remain, 1 if not.

| improve this answer | |
  • i get syntax error: unexpected end of file – user3239222 Mar 26 '17 at 15:31
  • You have to follow it with a then line, etc. – Tom Zych Mar 26 '17 at 15:32
  • 1
    I forgot; make the second grep -Evq to suppress its output. – Tom Zych Mar 26 '17 at 15:36
  • 4
    From the look of it, this looks for UID fields which are at least 4 characters long, which works here but isn't very useful if the cutoff value is something other than a power of ten. Also the results may be confusing if some of the numbers get leading zeroes for some reason. – ilkkachu Mar 26 '17 at 16:36
  • 1
    If you use YP/NIS or LDAP, then parse the output of getent passwd instead of the /etc/passwd file. – Kusalananda Mar 26 '17 at 16:36

To do that in a bash loop, I suggest using read and IFS like:

#!/usr/bin/env bash
while IFS=':' read -r user passwd uid gid comment home shell;  do
    if [ "$uid" -gt 1000 ] ; then
        echo GT $user
        echo LT $user
done < /etc/passwd
| improve this answer | |
  • tried that but it outputs all users LT and GT – user3239222 Mar 26 '17 at 15:49
  • 1
    It outputs some users as LT, and the rest as GT. Which is what I thought you needed. – Stephen Rauch Mar 26 '17 at 15:50
  • i only want to check etc/password to see if any UIDs greater than 1000 exist and do something if true, do something else if false.. – user3239222 Mar 26 '17 at 16:16
  • 4
    The key words in your comment is if any, the question said for and they. The plural they indicated to me that you intended to process each user. But three other answers here did not read it that way, so.... – Stephen Rauch Mar 26 '17 at 16:20

Instead of reading /etc/passwd directly, you should use getent passwd, that will also work if some of your users are saved in something like LDAP or such. awk should be well-suited for the colon-separated output format.

This would print the usernames of all users with UID > 1000:

getent passwd | awk -F: '$3 > 1000 {print $1}'

And this would just print found if at least one such is found:

getent passwd | awk -F: '$3 > 1000 {print "found"; exit}'
| improve this answer | |
  • how would i use this with an if statement though? if $3 > 1000, do something – user3239222 Mar 26 '17 at 23:33
  • I modified this a bit, because the first desktop user equals 1000, and there are other types of users like "nobody", so we also need an upper limit (which I've just arbitrarily set to 2000): getent passwd | awk -F: '$3 >= 1000 && $3 < 2000 {print $1}' – benjaoming Jun 26 '18 at 11:10
  • @benjaoming, err, yes, >= 1000 would indeed seem to make sense. The question had > 1000 for some reason though. nobody seems to be commonly 6553x or 3276x, so you can probably raise that upper limit a bit. – ilkkachu Jun 26 '18 at 13:54

This construct doesn't make much sense:

"$(id -u)" -gt "1000" </etc/passwd

The id command works regardless of the input redirection. The redirection doesn't make sense here anyhow.

You can use this pipeline in your test comparison. It will show you the highest user id on the machine:

cat /etc/passwd | awk -F: '{print $3}' | sort -n | tail -n1

You may want to tweak it for some systems as there may be the nobody user with UID 65534 present.

| improve this answer | |

Using Tom's code this worked for me..

if grep -E '^[^:]*:[^:]*:[^:]{4}' /etc/passwd | grep -Evq '^[^:]*:[^:]*:1000:'
    echo "exists"
    echo "not exists"
| improve this answer | |
  • i created a user mrBlonde:x:1001:1001::/home/mrBlonde:/bin/bash. when i run the code it says exists, when i delete the user, it says not exists. L) – user3239222 Mar 26 '17 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.