CIS-2230 Lab #9: AWK

In this lab you will experiment with the AWK scripting language and how it can be combined with shell scripts.

Part 1: Audit of /etc/passwd

  1. Start by writing an AWK script that lists all the normal users of your system. Such users are those whose /etc/passwd entry has a shell field that is not /sbin/nologin or /bin/false. Print the username, userID, and shell program for each such user in a tidy table.

    You will need to set the input file separator, IFS, to the colon character so that AWK will split the lines into fields appropriately. Do this in the action associated with the BEGIN pattern.

  2. Enhance your script so that it lists the home directory for each user as well. After each home directory display either (ok) or (!!) depending on if the home directory exists or not. You can test for the existence of a directory in AWK by calling out to shell code using the system() function. For exmaple:

      dir="/home/alice"
    
      if (system("[ -d \"" dir "\" ]") == 0) {
          print dir, "exists and is a directory."
      }
      else {
          print dir, "does not exist or is not a directory."
      }
    

    Note that the shell code in the above example is running the [ command, which is an alias for test.

Part 2: Associative Arrays

  1. Modify your script above so that at the end of the table it prints a summary of how many times each shell program is used. For example, it might print something like:

      /bin/bash   118
      /bin/zsh    57
      /bin/csh    4
      /bin/ksh    14
    

    To do this, create an associative array (e.g., shells) with one value for each shell encountered. Increment the appropriate array element as each line of the password file is processed. In the action for the END pattern, print the contents of the associative array.

Submission

For this lab, submit a document that shows the commands you used in each part and any relevant observations. This lab is worth 20 points.


Last Revised: 2025-11-06
© Copyright 2025 by Peter Chapin <peter.chapin@vermontstate.edu>