Home‎ > ‎CIS 98‎ > ‎

Lab 4: Tools of the Trade

Introduction

In this lab we'll use some of the "tools of the trade" that we've discussed in the last two weeks. You'll also create your first BASH script. Even though the challenge in this lab may seem simple (it's only one line of code) give yourself plenty of time. It's a complicated line of code. Also, the lab is accomplished by building commands out of pipes. It's best practice to do this one command at a time. 

Part 1: Text Processing

In this exercise you'll create your first executable BASH script. Start with this template: 

#!/bin/bash 

# prog4.1: A program that counts unique words using standard input 

Once you create your prog4.1 script with the contents above make it executable with the command:

$ chmod u+x ./prog4.1

If you did it correctly you can run it with no error:

$ ./prog4.1 

If you see a "permission denied" error after the above command check your chmod command. Once you have prog4.1 ready, create a file with the following text caled lytton.txt:
 
It was a dark and stormy night; the rain fell in torrents -except at occasional intervals, when it was checked by a violent gust of wind which swept up the streets (for it is in London that our scene lies), rattling along the housetops, and fiercely agitating the scanty flame of the lamps that struggled against the darkness.

Your prog4.1 program should read words from standard input and, 
  1. remove all punctuation, 
  2. separate each word onto its own line,  
  3. sort the words,
  4. remove duplicate words, then finally
  5. count the number of unique words.
Your program will need sed, tr, sort, uniq and wc. Connect the commands together using pipes. You can test your program from the command line with any input: 

$ ./prog4.1 
blah blah. blah. blah
<CTRL-D>
1

If you achieve the goal prog4.1 should work just like this: 

$ ./prog4.1 < lytton.txt 
46

Part 2: Here Document

Make a new program, called prog4.2 that performs the same function as prog4.1 but uses a here document. Your new program should include the "dark and stormy night," text from Part 1 and not read from standard input. 

Turn In

Place the files prog4.1 and prog4.2 in ~/lab4. 

Grading

  • 10 points for prog4.1 
  • 10 points for prog4.2

Comments