The goal of this homework assignment is to allow you to practice manipulating files, redirecting I/O, constructing pipelines, and signaling processes in a Unix environment.

For this assignment, record your responses to the following activities in the README.md file in the homework01 folder of your assignments GitLab repository and push your work by 11:59 AM Saturday, January 26.

Activity 0: Branching

As discussed in class, each reading and homework assignment must be completed in its own git branch; this will allow you to separate the work of each assignment and for you to use the merge request workflow describe in Reading 01.

To create a homework01 branch in your local repository, follow the instructions below:

$ cd path/to/cse-20289-sp19-assignments   # Go to assignments repository

$ git checkout master                     # Make sure we are in master branch

$ git pull --rebase                       # Make sure we are up-to-date with GitLab

$ git checkout -b homework01              # Create homework01 branch and check it out

$ cd homework01                           # Go into homework01 folder

Once these commands have been successfully performed, you are now ready to add, commit, and push any work required for this assignment.

Activity 1: File Operations (2 Points)

The /usr/share/pixmaps folder is found on many Linux machines. It usually contains images such as icons and wallpapers used by desktop environments and graphical applications.

On the student machines, you are to perform the following operations while using the time command to measure how long each operation took:

  1. Copy all the contents of the /usr/share/pixmaps folder to a new images folder in your ESC - NFS home directory.

  2. Rename this new images folder in your home directory to pixmaps.

  3. Move this new pixmaps folder to /tmp/$USER-pixmaps (where $USER is your netid)

  4. Remove the /tmp/$USER-pixmaps folder

Record your answers by filling out the following Markdown table:

| Command                             | Elapsed Time  |
|-------------------------------------|---------------|
| cp ...                              | 0 seconds     |
| mv ...                              | 0 seconds     |
| mv ...                              | 0 seconds     |
| rm ...                              | 0 seconds     |

Reading Time

The time command is typically a Unix shell built-in which executes the specified program and outputs something like the following:

$ time sleep 1
real  0m1.001s
user  0m0.000s
sys   0m0.002s

In the example above, the time command is used to execute the sleep command and measure how long it took. Normally, we are interested in the real or wall-clock time, which in this case is 1.001 seconds.

After performing and timing the operations above, answer the following questions:

  1. Both renaming and moving the pixmaps folder use the mv command but the former operation takes significantly less time than the latter. Explain why this is.

  2. Similarly, explain why removing the /tmp/$USER-pixmaps operation is faster than the move operation.

Activity 2: Redirection (3 Points)

The bc command, which was introduced in Reading 01, is another example of an interactive interpreter. In this case, bc processes and executes basic arithmetic operations (ie. it is a basic calculator).

Suppose you had a file named math.txt, with the following contents:

1 + 1
2 * 4
(3 - 7) * 8
2^3
4/0
  1. How would you use I/O redirection to process the math.txt file with bc?

  2. How would you use I/O redirection to process the math.txt file with bc and save the output to results.txt?

  3. How would you use I/O redirection to process the math.txt file with bc, save the output to results.txt, and suppress any errors?

  4. How would you use a pipeline with cat instead of I/O redirection to process the math.txt file with bc?

  5. How would you use a pipeline with tee and I/O redirection to process the math.txt file with bc while saving the output to results.txt, suppressing errors, and showing the results to the screen?

  6. Is a pipeline more or less efficient than I/O redirection for reading input from a file? Explain.

Activity 3: Pipelines (2 Points)

Recall that the Unix Philosophy can be summarized as:

  1. Write programs that do one thing and do it well.

  2. Write programs to work together.

  3. Write programs to handle text streams, because that is a universal interface.

The ultimate example of these tenets in action is the Unix pipeline.

For each of the following tasks, construct a pipeline that combines multiple commands to accomplish each task.

  1. Determine how many accounts in the /etc/passwd file have /sbin/nologin as their shell.

    $ grep ... | ...      # Results from student00.cse.nd.edu
    49
    

    Hint: Consider using grep, and wc.

  2. Determine how many instances of bash are running on the current machine (across all users).

    $ ps ... | ...        # This result will vary depending on the number of users at the time
    3
    

    Hint: Consider using grep, ps, wc.

  3. Determine the top 5 largest files or directories in the /etc directory while suppressing any errors.

    $ du ... | ...
    7.1M    /etc/udev
    6.2M    /etc/gconf
    5.6M    /etc/selinux/targeted
    5.6M    /etc/selinux
    3.8M    /etc/brltty
    

    Hint: Consider using du, head, sort, tail.

  4. Determine how many unique users are logged into the current system.

    $ who ... | ...       # This result will vary depending on the number of users at the time
    2
    

    Hint: Consider using awk, cut, sort, uniq, wc, who.

Field Extraction

You can use either awk or cut to extract fields from a line of text. For instance, the following returns the first field separated by spaces of each line in the input file:

cat file | awk '{print $1}'

You can accomplish something similar with cut:

cat file | cut -d ' ' -f 1

This returns the first field separated by a single space. We will learn more about constructing pipelines with text filters in a few weeks.

Activity 4: Processes & Signals (3 Points)

For the last activity, you are to run the following program on the student machines:

/escnfs/home/pbui/pub/bin/TROLL

This is a program that will taunt you and insult you... until you figure out how to terminate the process.

  1. Using a single terminal window, run the TROLL program and figure out a way to terminate it:

    a. Describe what commands or operations failed to terminate the process.

    b. Describe what commands or operations finally succeeded in terminating the process.

  2. Using two terminal windows, run the TROLL program in the first window and figure out a way to terminate it from the second terminal window:

    a. Describe a pipeline that can be used to terminate the process from the second terminal window.

    b. Describe a single command that can be used to terminate the process from the second terminal window.

  3. Explore passing different signals to the TROLL program. Which ones lead to interesting messages that are not taunts or insults? (You should find at least two different signals)

Is It Dead Yet?

To verify that the TROLL is actually terminated and not merely stopped, you can use the following command:

$ pgrep -u $(whoami) TROLL

If any PIDs are displayed, then you still have TROLLs running and thus have not terminated it properly. Ensure that the sequence of commands you figured out will reliably terminate all instances of the TROLL.

Guru Point (1 Extra Credit Point)

For extra credit, read the following chapters from The Linux Command Line:

  1. Chapter 11 - The Environment
  2. Chapter 13 - Customizing the Prompt

Once you have read these chapters, you are to customize your bash shell prompt by adding useful information and colors and setting $EDITOR to your preferred text editor.

Here are some additional resources to help you customize your bash shell:

  1. How to Customize your Bash Prompt on a Linux VPS
  2. How to Customize (and Colorize) Your Bash Prompt
  3. Bash/Prompt customization
  4. EZPrompt: Easy Bash PS1 Generator
  5. In Unix, how do I set my default (preferred) editor?

Verification

To get credit for this Guru Point, show your bash shell customizations to the instructor or a TA to verify. You have up until a week after this assignment is due to verify your Guru Point.

Feedback

If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your README.md.

Submission

To submit your assignment, please commit your work to the homework01 folder of your homework01 branch in your assignments GitLab repository:

$ cd path/to/cse-20289-sp19-assignments   # Go to assignments repository
$ git checkout master                     # Make sure we are in master branch
$ git pull --rebase                       # Make sure we are up-to-date with GitLab
$ git checkout -b homework01              # Create homework01 branch and check it out
$ cd homework01                           # Go to homework01 directory
...
$ $EDITOR README.md                       # Edit appropriate README.md
$ git add README.md                       # Mark changes for commit
$ git commit -m "homework01: complete"    # Record changes
...
$ git push -u origin homework01           # Push branch to GitLab

Remember to create a merge request and assign the appropriate TA from the Reading 01 TA List.