Created
May 22, 2023 08:37
-
-
Save mochaaP/d099866e0ef01eda40291d535586688d to your computer and use it in GitHub Desktop.
getent.sh: The world's cheesiest getent implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Copyright 2009 Rob Landley <rob at landley.net>, licensed under GPLv2. | |
isnum() | |
{ | |
[ ! -z "$(echo $1 | grep '^[0-9]*$')" ] | |
} | |
nocomments() | |
{ | |
sed 's/\([^#]*\)#.*/\1/' /etc/$1 | |
} | |
# The world's cheesiest getent implementation | |
case "$1" in | |
passwd|group) | |
isnum "$2" && | |
grep -m 1 "[^:]*:[^:]*:$2:" /etc/$1 || | |
grep -m 1 "^$2:" /etc/$1 | |
;; | |
hosts|networks|protocols) | |
nocomments $1 | grep -m 1 -w "$2" | |
;; | |
services) | |
nocomments $1 | (isnum "$2" && grep -m 1 "[ ]$2/" || grep -m 1 -w "$2") | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment