CS 23 Software Design and Implementation

Lab2

Shell Programming Lab

This lab comprises five bash shell programming problems. They start easy and get progressively harder.

Note: This lab and all future labs will be graded.

Due date: 5 PM, Tuesday April 8.

You will find that some of the snippets of shell script in Lecture 4 on shell programming can be used as a foundation for some of the problems.

These exercises should develop your ability your shell programming skills. Grading will focus on the correctness of your solutions, but will also consider good coding style - including consistent formatting, selection of identifier names, and use of meaningful comments.

Submitting assignment: The following sequence of Linux commands should be used to submit your work by the deadline:

Change to your labs directory cd ~/cs23/labs This directory contains your lab2 directory where your solutions are found.

Please make sure that the labs2 directory contains a simple text file, named README, describing anything “unusual” about how your solutions should be located, executed, and considered.

Issue the commands:

tar czvf $USER-lab2.tar.gz lab2

mail the “tarball” to cs23@cs.dartmouth.edu

There is a section on creating tarballs in Lecture 3. You might check that everything looks fine using the list command.

Coding style: Please put comments in your scripts to help increase its understanding. Include a header in each script similar to the the one in spy.sh.


# Script name: spy.sh
#
# Description: Monitors when users log in and out of a machine and sends e-mail on
# each time a user logs in and out to the user. It also sends a summary and the end of
# the day to all users informing them of their usage patterns including how many times
# they logged in and out and the duration. The script also computes which user logged
# in the most often, and for the longest and shortest periods of time.
#
# Input: List of users to monitor in terms of their full names such as ‘‘Andrew Campbell’’
#
# Output: E-mails the user each time they log out. And sends a e-mail summary of activity
# at the end of the day to all users monitored.

It is important when your write scripts or C programs to program defensively. That is, you need to check the program’s input and provide appropriate warning or error messages back to the user in terms of the program usage. For example, a script that requires input arguments should check if it has the right number of input arguments and if not it should inform the user of usage errors.

Complete the following questions.

____________________________________________________________________________

1) birthday_match.sh script: Write a scripted called birthday_match.sh that take two birthdays (or in the general case any two dates in history) of the form MM/DD/YYYY (e.g., 05/15/1959) and returns whether there is a match if the two people where born on the same day of the week (e.g., Friday). Do you know which day you were born on? Should be fun to see if any of you are born on the same day of the week. The input and output to/from the script should follow this format:

birthday˙match


-- no match

./birthday_match.sh 12/01/1984 12/02/1984
The first person was born on: Sat
The second person was born on: Sun
Therefore, you are not born on the same day.

-- match

./birthday_match.sh 11/25/1984 12/02/1984
The first person was born on: Sun
The second person was born on: Sun
Jackpot: You were both born on the same day!

Please code defensively for the cases of no arguments or insufficient arguments.

2) leap_year.sh script: Write a script called leap_year.sh that takes a year as input and determines if its a leap year. If the user does not enter a correct leap year then the script should compute the next leap year after the date entered as an argument. The script command line and its response should look like the following:


-- incorrect entry

./leap_year 201
201 is not a leap year.
The closet leap year after that year is 204

-- correct entry

./leap_year 2008

2008 is a leap year.

Here is the definition of a leap year. You will find the pseudo code for the simple algorithm to compute a leap year there.

You code should be defensively written to handle incorrect entry, such as no arguments entered or two many. In each case the user should be give an appropriate response for example:


./leap_year
leap_year usage: No arguments entered, please enter a year.

3) count_linesinfiles.sh script: Write a script called count_linesinfiles.sh that counts the number of ordinary files (not directories) in the current working directory and its sub-directories. For each ordinary file found your script could count the number of lines in file and print the total number of lines for files in the directory tree rooted at the current working directory.

Hint: find without any arguments is a good place to start.

There are no input arguments to the count.sh script. The expected output is shown below.


./count.sh
Counting lines of all files...
23 ./count.sh
1  ./a.java
24 Total
*************************
Number of files found: 6
*************************

4) wget_search.sh script: Write a script called wget_search.sh that given a text file (containing a list of URLs) and a sequence of words, searches for occurences of the words in the webpages. This script has some relationship to the function of a search engines crawler. We will look at the design and implementation of a simple command-line based search engine later on in the course.

You should use the wget command to retrieve the html pages from the web URLs in the file. The wget command is a non-interactive network downloader. Each retrieved web page should be stored and renamed using a progressive integer number (e.g., 1.html, 2.html, 3.html and so on).

The command has the the following format:


./wget_search.sh url.txt word1 word2 ... wordn

where:

- url.txt is the name of the file containing the list of URLs - one per line (e.g, http://www.dartmouth.edu/);

- word1 word2 ... wordN are the words to be checked (e.g., computer).

The number of words is not known a priori. The program should be case sensitive.

For example, given the following list contained in the file url.txt:

http://www.cs.dartmouth.edu
http://www.dartmouth.edu

The execution of the command returns the following output:


./wget_search.sh url.txt Dartmouth Campbell

Dartmouth
http://www.dartmouth.edu 11
http://www.cs.dartmouth.edu 0

Campbell
http://www.dartmouth.edu 0
http://www.cs.dartmouth.edu 0

5) spy.sh script: Write a script called spy.sh that can check if any users given as input to the script, e.g.:


./spy.sh "Andrew Campbell"  "Wei Pan" "Mirco Musolesi"

are logged on to a machine (in this case wildcat). When the user logs out the script sends the user an e-mail with the subject title “Gotcha username!” The e-mail should include the time the user logged in and out, and the duration of time on the machine (e.g., 20 mins).

In Lecture we used a script to find the username from the /etc/passwd file. One simple way to see if the user is running a process is to use who. There are other ways too. We will discuss this is X-hour.

Important: Mirco, Wei, and Andrew will log on to wildcat a number of times each on Sunday - they’ll sneak on, your mission if you accept it is to take them down. Sunday will be a test/debug session to allow you to check out your code. We will post what we did on Sat. to see if you caught us. Monday will be the final run when Mirco, Wei, and Andrew will again log on to wildcat a number of times each during the day.

Extra credit: At or just after midnight send one e-mail to all of them that lists the following:


Dear Sneaky cs23 Guys:

Caught you sneaking on to wildcat today. Here’s a summary of my surveillance:

Andrew you logged on N times for a total period of X. Here is the breakdown:

1) Logged on Sat Jan 12 18:40:34 EST 2008; logged off Sat Jan 12 18:40:34 EST 2008
2) ...

Wei you logged on Y times for a total period of X. Here is the breakdown:

1) Logged on Sat Jan 12 18:40:34 EST 2008; logged off Sat Jan 12 18:40:34 EST 2008
2) ..
3) ..

Mirco you logged on Z times for a total period of X. Here is the breakdown:
1) Logged on Sat Jan 12 18:40:34 EST 2008; logged off Sat Jan 12 18:40:34 EST 2008
2) ..
3) ..

Looks like Andrew || Mirco || Wei spent most time on wildcat today - 100 mins in total for
all his sessions;  Andrew || Mirco || Wei was on for the shortest session for a period
of 2 mins, and therefore the most sneaky; and, Andrew || Mirco || Wei logged on the
longest session of 15 mins.

Three variables are maintained and reported:

Thought you could sneak by my code hey  - nailed you.

Best,

Your name

____________________________________________________________________________

OK. You are done.

Tip: Make sure you always logout when you are done and see the prompt to login again before you leave the terminal.