In-class assignment for Tuesday, August 21

You are encouraged (but not required) to work with a partner, in which case the two of you will turn in one paper with both your names on it (and both of you get the same grade). If you are not familiar with Unix or Linux, then start with this introduction.

Some additional comments and examples about the standard input, output, and error streams can be found here.

Exercises

Please use a text editor to place your answers to the following problems in a text file and print it out. This assignment is due at the beginning of class on Tuesday, Aug. 28.

There are several text processing utilities that are useful for handling numerical data. If you are not already familiar with them, look at the manual pages for head, tail, more, sort, cat, and wc. You can read the manual page for head by typing

man head
at the command prompt, and similarly for the others. (Note: some systems substitute a command called less for the command more, but the operation of the two programs is similar.)

1. Here is a sample data file; save it to your home directory as sample.dat. What command would you use to

2. The sort command sorts text, but an option, -n, causes text to be sorted by numerical value. Give the command that you would use to sort the contents of sample.dat and store them in a file called sample.sort.

Pipes are a mechanism by which the standard output of one command can be directed to the standard input of another without having to create a temporary intermediate file. For example, if myprog is some program that writes to the standard output, then the command

myprog | more
displays the output one screenful at a time.

3. What command renames sample.dat to wbn1.dat? Run this command. (Here's a hint if you need one.)

4. Here's a second and a third data file, which you should save as wbn2.dat and wbn3.dat respectively. Using the for construct discussed in class, write a Korn (or bash) shell script that produces, on the standard output, the largest 10 values in each data file. (That is, the output consists of the largest 10 values in wbn1.dat, followed by the largest 10 values in wbn2.dat, etc., for a total of exactly 30 lines of numbers. Don't print a header with the individual file names.)

5. Store your script in a file, say big10.sh, and illustrate a command that outputs the 10 largest values in each of the three data files in descending numerical order. (That is, the output of Exercise 4 should be sorted in descending order.)

6. Give an example of a command that concatenates all of the contents of wbn1.dat, wbn2.dat, and wbn3.dat, then finds the five smallest values in the result.

7. The shell allows you to define variables, which require no declaration and whose contents are always character strings. Try out the following commands. Important: Do not put spaces around the = signs!

version=08/21/07
echo $version

announcement="My great and wonderful program, version $version"
echo $announcement

announcement='My great and wonderful program, version $version'
echo $announcement
(a) What is the difference in effect between single and double quotation marks?

Also try each of the following commands:

animal=dog
echo $animal

animal="the dog"
echo $animal

animal=
echo $animal

animal=the dog
echo $animal

(b) When are quotation marks required to assign a string to a shell variable?

(c) What happens when the right-hand side of a shell variable assignment is empty?

8. The goal of this exercise is to write a simple program in the shell (otherwise known as a shell script). Here are a couple little examples of shell usage first.

The date command prints the time and date. Try it by typing simply

date
Now consider the following construct:
starttime=$(date)
echo $starttime
The construct $(command) puts the standard output of command into the shell variable named on the left-hand side of the assignment.

The for construct gives a loop. Type the following lines into a file (call it loop.sh):

for file in *.dat
do
   echo $file
done
Then execute your code as follows:
sh loop.sh
Compare the results with the command
ls *.dat

The programming exercise: Write a shell script containing a loop that, for each of the files wbn1.dat, wbn2.dat, and wbn3.dat, prints the name of the file, a space, and the first value in the file (one name-value pair per output line).

9. Write a shell script containing a loop that, for each of the files wbn1.dat, wbn2.dat, and wbn3.dat, prints the name of the file and the largest value in the file (one name-value pair per output line).

10. Write a shell script containing a loop that, given the data files wbn1.dat, wbn2.dat, and wbn3.dat, determines which of them contains the largest value x and displays both x and the name of the file that contains x. (If there's a tie, then select any one of the files containing x.)

11. The sed command, which stands for streaming editor, is a batch-oriented text editor. It is handy for simple editing jobs, particularly when the same edits must be done to a large group of files. Here are some examples.

Write a shell script, called tl, which uses sed to type selected lines of input. Your script should work as follows: Your script should behave in a reasonable manner under any of the following circumstances: Send error messages to the standard error, not to the standard output.

Copyright (c) 2005-2007 by Eric J. Kostelich. All rights reserved.