Skip to content

Instantly share code, notes, and snippets.

@tonejito
Last active June 2, 2017 18:39
Show Gist options
  • Select an option

  • Save tonejito/369ac4cb3759bf0b3471be7034a74e99 to your computer and use it in GitHub Desktop.

Select an option

Save tonejito/369ac4cb3759bf0b3471be7034a74e99 to your computer and use it in GitHub Desktop.
Get normal or system user accounts from `getent passwd` with values from login.defs
#!/bin/bash
# get-users
# Get normal or system user accounts from `getent passwd` with values from login.defs
# Andres Hernandez - tonejito
#
# This script is released under the GPL license version 3
#
# Usage:
# get-users [[--]users | [--]system]
AWK=/usr/bin/awk
GREP=/bin/grep
GETENT=/usr/bin/getent
LOGIN_DEFS=/etc/login.defs
UID_MIN="`$GREP ^UID_MIN $LOGIN_DEFS | $AWK '{print $2}'`"
UID_MAX="`$GREP ^UID_MAX $LOGIN_DEFS | $AWK '{print $2}'`"
AWK_FILTER_USERS='{if($3>=UID_MIN&&$3<UID_MAX){print}}'
AWK_FILTER_SYSTEM='{if($3<UID_MIN||$3>=UID_MAX){print}}'
case "${1}"
in
--system|system)
FILTER=$AWK_FILTER_SYSTEM
;;
--users|users|*)
FILTER=$AWK_FILTER_USERS
;;
esac
$GETENT passwd | \
$AWK -F : \
-vUID_MIN="${UID_MIN}" \
-vUID_MAX="${UID_MAX}" \
$FILTER \
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment