CS 2204 Lab 5
Your name here (please print):
Your student ID number here:
WARM UP:
- Create a subdirectory called lab5 under your home directory.
- Inside this directory, create several files, each with some random text in them. Make sure some files have extension (i.e., names ending with) .c and some files have extension .test.
QUESTIONS TO ANSWER:
In this lab assignment, we are going to explore the programming capabilities of gawk. Instead of
writing gawk scripts to process a file's contents, we are going to write gawk scripts to process the
output of the ls -l command. For instance, we will invoke gawk as (make sure you are cd-ed into
the lab5 directory):
ls -l *.c | gawk -f <your gawk script here>
- (3 points) Write a gawk script to print the sizes of files, one on each line. First, analyze a
typical output of the ls -l command to see which field contains the size information.
Then, determine what the field separator is and whether the default field separator of gawk
will work, or if you have to specify it using the FS= command. Write down your gawk script
here:
- (2 points) There is a potential problem with the script you wrote above. If we had invoked
the gawk command as:
ls -l | gawk -f <your gawk script here>
note that we have an extra line in the ls output, beginning in total, that will mess things
up. You must tell gawk to ignore this line. The way you do it is using a test on the variable
NF. Do a man gawk to see what NF means. Then, write a gawk script that uses a pattern to
check if the NF variable is 9 (using the == operator) and only if true, proceed to print the
size information (Why is 9 the magic number?). Write your new script below.
- (2 points) Oh, Geez! There are even more problems. What if the output of ls -l involved a
directory or a symbolic link? You may not have such issues in your lab5 subdirectory, but
we should try to write as fool-proof a command as possible. In this case, we want to ignore
lines corresponding to directories or links. So, in addition to the NF test, check to see if the
line involves a file. Use a logical AND (&&) to achieve this effect. Only after both
conditions are satisfied, proceed to print the size information. Write your new script below.
- (2 points) Download .cshrc.example from lecture 4 and "install" it in your home directory, by following the insructions in the file header.
If you did everything correctly, a shell with large green font should come up if you type XT on the command line. Why?
- (1 point) Now Create a directory bin in your home directory.
Use vim to edit .cshrc and add /home/ugrads/X/yourname/bin (use the pwd command to get the exact directory name) to the path in
your .cshrc file (look for "set path = ( )" and follow the
pattern). Move the "rasmol" program (from lab2) to your /home/ugrads/X/yourname/bin, OPEN A NEW SHELL, and then check that you can execute rasmol in it by just
typing "rasmol" in your lab5
directory (or any other directory OUTSIDE of /home/ugrads/X/yourname/bin). Call your TA when you are done.