getent 1.1 KB

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