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?
cat /etc/passwdshows one entry (me) whilegetent passwdshows maybe 30 users. Same with getent group. Probably this means the command line I'm using is actually running in a container? That would fit with the Silverblue theme.getentmust be more globally aware (such as querying the host os?) while/etc/passwdseems to be more local (to the container?). If you're picking a uid for running other containers, getent is what you need to use.