Return to labs index
Assignment #1 - document index

Shell Scripting
Due: Thursday, September 9 at 11:59PM


This assignment asks you to implement a short shell script. Given a list of files and a list of keywords, the script should produce an organized index that shows each reference to each keyword within the specified files. Basically, this script makes it easy to find a reference within a file by keyword.


The script accepts two arguments, each of which is a quoted list. The first argument is the list of key words. The second argument is the list of files to search.

Should the user invoke the script with other than two (2) arguments, it should display its usage help, as below:

  Usage: index "keywords" "file list"

The output should be neat and readable. In the section below you can see an example of my solution's function. Please try to match this format as closely as possible.

Sample Interaction

  > ./index "know need" "samples/*"
                  samples/linkers.txt:            1 4 
                  samples/linkers.txt:            4 


Implementation Hints and Strategy

"grep" can be used to find lines that contain matching patterns, such as words. If used with the "-n" option, it'll print out line numbers in addition to the single line of context. "cut" can be used to select the necessary information from the grep output. For example, "cut -d: -f3" selects the third field from the input. "tr", short for "translate" can be used to substitute one character for another, or, with the -d option, to delete one character, outright.

My solution first checked the argument count. If it wasn't right, it printed the usage and exited. Then, it entered a nested loop. The outer loop was "for each keyword in the list." The nested loop was "for each file in the list."

Basically, I printed out the keyword. Then, indented, I printed out each hit for each file. I got the hits, line numbers, using grep and then doing some massaging. Lastly I used "printf" to generate the output.

Good things to research

Handing In Your Work

We'll talk about this next week.

We're Here To Help!

As always -- remember, we're here to help!