Lab Exercise 8

Building Some Basic Unix Tools


CS 1510
Introduction to Computing


Introduction

Create a directory on your USB device for this lab, say, lab08, and launch IDLE.

At the end of the lab, you will submit a responses.txt file that shows your programs in action. Download this template file and use it to record outputs from running each of your programs.

You will need a text file containing a wide selection of words in order to test your programs in action. Download this file of notes and use it as an input file when running each of your programs.



Unix

Unix is an operating system created in the early 1970s at AT&T Bell Labs. Relatively speaking, not many people use it, but it powers much of the computing world by running on servers and other infrastructure. It even serves as the foundation for Mac OS X.

In Unix, the user has access to an interactive shell much like IDLE. The programming language isn't Python, though; it's one of several shell languages. Most Unix shell languages provide a large number of common commands for manipulating text files. By default, these commands read from standard input, like Python's input(), and write to standard output, like Python's print() without a file= argument.

Today, you will implement three basic Unix commands:

You will also implement a couple of the more advanced features of the cat command.



Task 1: The cat Command

cat is short for 'catenate'. In its simplest form, this command displays the contents of a text file to standard output:

    > cat dirda-excerpts.txt
    From "Book by Book: Notes on reading and Life"
    by Michael Dirda, Henry Holt and Company, 2005

    Some people will never learn anything, for this reason,
    because they understand everything too soon.
    -- Alexander Pope

    ...

    Francois duc de La Rochefoucauld, Maxims
    Ralph Waldo Emerson, Essays / Journals
    John Henry Newman, The Idea of a University

Write a program that behaves like cat. It should take a filename as input, with a prompt of cat:, and print the file's contents to the screen. For example:

    cat: dirda-excerpts.txt
    From "Book by Book: Notes on reading and Life"
    by Michael Dirda, Henry Holt and Company, 2005

    Some people will never learn anything, for this reason,
    [...]
    Niccolo Machiavelli, The Prince
    Francois duc de La Rochefoucauld, Maxims
    Ralph Waldo Emerson, Essays / Journals
    John Henry Newman, The Idea of a University

Name your file cat.py.



Task 2: The more Command

When a text file is large, cat can be rather inconvenient. It displays text so quickly that we miss all but the last screen's worth.

In such cases, we can use the more command. It displays one screen of text at a time. After displaying a screenful of lines, it lets the user decide whether to continue. If the user enters q, then more quits. If the user enters anything else, then more displays another screenful of lines and lets the user decide again. For example:

    > more dirda-excerpts.txt
    From "Book by Book: Notes on reading and Life"
    by Michael Dirda, Henry Holt and Company, 2005

    Some people will never learn anything, for this reason,
    [...]
    women whose examples invite us to love what they love.
    -- Robert Wilken

    The teacher is both the end and the sanction of the
    :_
    education he gives.  This is why it is completely
    reasonable that a student should expect a classicist
    to live classically.  The man who teaches Shakespeare
    or Homer runs the supreme risk.  This is surely as it
    [...]
    [Through nature]
    the heavy and the weary weight
    Of all this unintelligible world
    Is lightened.
    :_q

The lines with :_ are prompts for user input. When the user enters q, the program quits.

Write a program that behaves like more. It should take a filename as input, with a prompt of more:, and print the file's contents to the screen in chunks of ten (10) lines. For example:

    more: dirda-excerpts.txt
    From "Book by Book: Notes on reading and Life"
    by Michael Dirda, Henry Holt and Company, 2005

    Some people will never learn anything, for this reason,
    because they understand everything too soon.
    -- Alexander Pope

    The high-school English teacher will be fulfilling his
    responsibility if he furnishes the student a guided
    opportunity, through the best writing of the past, to
    :_
    come, in time, to an understanding of the best writing
    of the present.  He will teach literature, not social
    studies or little lessons in democracy or the customs
    of many lands.  And if the student find this not to
    his taste?  Well, that is regrettable.  His taste
    should not be consulted; itis being formed.
    -- Flannery O'Connor

    [We learn best by placing our] confidence in men and
    women whose examples invite us to love what they love.
    :_q

Name your file more.py.



Task 3: A cat-To-File Command

The cat can also (a) read multiple files and even (b) write its output to a file, instead of to standard output. It writes the contents of the input files, in order, to the output file.

In Unix, redirecting output requires the use of > as a special character. For example:

    > cat cat.py more.py > cat_more.txt
    > cat cat_more.txt

    filename = input('cat: ')
    textfile = open(filename, 'r')

    [...]

            if should_continue == 'q':
                break

    textfile.close()

Write a program that behaves like this version of the cat command. It should take as input three filenames: two input files and one output file. It should then write the contents of the two input files, in order, to the output file. For example:

    cat: cat.py
    and: more.py
    to : cat_more.py

We don't see any output on the screen, but we should be able display the new file cat_more.py using our original cat.py program!


    filename = input('cat: ')
    textfile = open(filename, 'r')

    [...]

            if should_continue == 'q':
                break

    textfile.close()

Name your file cat_to_file.py.



Task 4: The grep Command

grep is another command with a strange name. In its simplest form, we might call it find, because searches an input file, selects lines that contain a given string:

    > grep William dirda-excerpts.txt
    -- William Arrowsmith
    -- William Wordsworth

These are the only lines in dirda-excerpts.txt that contain the string William.

Write a program that behaves like grep with a couple of extra features. It should take as input a search string and filename, and print the lines in the file that contain the search string. Your version should print the number of the line in the at the beginning of each line of output. For example:

    grep: William
    in  : dirda-excerpts.txt
    32 -- William Arrowsmith
    47 -- William Wordsworth

Make sure that your program matches strings regardless of case. For example:

    grep: BOOK
    in  : dirda-excerpts.txt
    1 From "Book by Book: Notes on reading and Life"
    72 If a book isn't worth reading over and over again,
    77 determine definitely whether a given book is "good"
    92 Gene Wolfe, Book of the New Sun

Name your file grep.py.



Task 5: More Unix-Like Commands

... change your programs so that we type all the arguments on a single line, separated by spaces. It's not too difficult with split(' ').



Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file.

Submit your files for grading on the electronic submission system, at lab08 -- Building Some Basic Unix Tools.

As always, make sure you see the verification screen that says The files listed above were uploaded.

If you need any help, let me know.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... October 15, 2014