In various shell scripts I often see two approaches for getting information from databases supported by Name Service Switch libraries like /etc/group
, /etc/hosts
or /etc/services
. One is getent
utility and other is grep
or some other text processing tool. For example:
root@fw-test:~# getent passwd root
root:x:0:0:root:/root:/bin/bash
root@fw-test:~#
root@fw-test:~# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
root@fw-test:~#
..or:
root@fw-test:~# getent hosts www.blah.com
189.113.174.199 www.blah.com
root@fw-test:~#
root@fw-test:~# host www.blah.com
www.blah.com has address 189.113.174.199
root@fw-test:~#
Which of those two approaches above should be used in scripts? I mean is one of the solutions more elegant or standard than the other?