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.
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
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:
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:
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.
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:
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:
5) spy.sh script: Write a script called spy.sh that can check if any users given as input to the script, e.g.:
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:
____________________________________________________________________________
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.