1#!/bin/sh 2# 3# Closely (not perfectly) emulate the behavior of glibc's getent utility 4# 5#passwd|shadow|group|aliases|hosts|networks|ethers|netgroup|protocols|services|rpc 6# only returns the first match (by design) 7# dns based search is not supported (hosts,networks) 8# case-insensitive matches not supported (ethers; others?) 9# may return false-positives (hosts,protocols,rpc,services,ethers) 10 11[ -z "$PATH" ] && PATH="/bin:/usr/bin" || PATH="${PATH}:/bin:/usr/bin" 12export PATH 13 14file="/etc/$1" 15case $1 in 16 passwd|group) 17 match="^$2:\|^[^:]*:[^:]*:$2:" ;; 18 shadow) 19 match="^$2:" ;; 20 networks|netgroup) 21 match="^[[:space:]]*$2\>" ;; 22 hosts|protocols|rpc|services|ethers) 23 match="\<$2\>" ;; 24 aliases) 25 match="^[[:space:]]*$2[[:space:]]*:" ;; 26 ""|-h|--help) 27 echo "USAGE: $0 database [key]" 28 exit 0 ;; 29 *) 30 echo "$0: Unknown database: $1" 1>&2 31 exit 1 ;; 32esac 33 34if [ ! -f "$file" ] ; then 35 echo "$0: Could not find database file for $1" 1>&2 36 exit 1 37fi 38 39if [ $# -eq 1 ] ; then 40 exec cat "$file" 41else 42 sed "s/#.*//; /$match/q; d" "$file" | grep . || exit 2 43fi 44