if statement query user id in etc/passwd?
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
else
echo LT $user
fi
done < /etc/passwd
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}'
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.