Chapter 11. Extended Example: Merging User Databases

By now, we’ve come a long way and seen a number of shell scripts. This chapter aims to tie things together by writing shell programs to solve a moderately challenging task.

The Problem

The Unix password file, /etc/passwd, has shown up in several places throughout the book. System administration tasks often revolve around manipulation of the password file (and the corresponding group file, /etc/group). The format is well known:[1]

tolstoy:x:2076:10:Leo Tolstoy:/home/tolstoy:/bin/bash

There are seven fields: username, encrypted password, user ID number (UID), group ID number (GID), full name, home directory, and login shell. It’s a bad idea to leave any field empty: in particular, if the second field is empty, the user can log in without a password, and anyone with access to the system or a terminal on it can log in as that user. If the seventh field (the shell) is left empty, Unix defaults to the Bourne shell, /bin/sh.

As is discussed in detail in Appendix B, it is the user and group ID numbers that Unix uses for permission checking when accessing files. If two users have different names but the same UID number, then as far as Unix knows, they are identical. There are rare occasions when you want such a situation, but usually having two accounts with the same UID number is a mistake. In particular, NFS requires a uniform UID space; user number 2076 on all systems accessing each other via NFS had better be the same user (tolstoy), or ...

Get Classic Shell Scripting now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.