Lab 5 (online) 5 points
Note: Lab5 begins the implementation of a real program, which is continued in lab6. As a real program, there is more work and much more description. Start early. You must do lab5 to do lab6. There is extra credit available in lab6.
I have written about half of lab5 for you, as discussed in the Hints section. I suggest you start with that part and add the missing code.
In Lab 5 and Lab 6 we will be implementing a program to help the user use the "user information program" on the local system, called finger. finger was designed for central servers that have many users logged in. In the current climate of many small personal computers it has fallen into disuse. However, on a system like hills it can be useful. The syntax of finger is finger [-m] name. finger performs a pattern match using name on all system user logins and real name or titles (see below). (The m is used if string is the user's login. This suppresses the pattern matching and speeds up finger)
Here is sample output from finger
bash$ finger -m gboyd
Login name: gboyd (messages off) In real life: Unix/Linux Instructor
Directory: /users/gboyd Shell: /usr/bin/bash
On since Oct 30 16:57:17 on pts/22 from dsl027-156-114.sfo.comcast.net
New mail received Wed Jun 13 09:54:04 2001;
unread since Sat Oct 30 16:57:17 2010
To learn as much as I can about Unix and Linux
and hopefully teach some of it.
Alternate plan: Anything but Microsoft
finger's output includes information provided by the system such as the user's home directory, when they last read their system mail and when they last logged in. It also outputs some personal information provided by the user (which I have indicated above in bold). This includes
who the user wants to be known as (the user's real name or title)
where the user is located (the user's office)
two phone numbers provided by the user (which are often not provided today). We will not give them to chfn. Hence they do not appear in the output above.
This part of the personal information is stored in the system passwd datafile. The user can update this information by running the program chfn (for change finger information)
In addition to these pieces of information, finger will output text information that the user provides. (In the output above, this part is italicized.) This personal descriptive information is stored in two files the user must create in their home directory. In order for finger to access these files, they must be readable by everyone, and the user's home directory must be executable by everyone:
the user's project (from ~/.project) - a one-line description of what they are working on now
the user's plan (from ~/.plan) - a multi-line description of the user (a text "profile")
Everyone used to know how to set up their finger information. Today, no one does. Your program will help them do it by 1) ensuring that their home directory is searchable (executable) and that their .plan and .project files exist and are readable, 2) asking them questions to retrieve their title and location, then, 3) running chfn for them to set their finger information.
The final version (in lab6) will be named pinfo. This first, partial, implementation (in lab5) will be named xpinfo.
xpinfo's job will be as follows:
output a welcome message
Check that the user's home directory is world-executable (has execute set for user, group, and other). If not, ask the user if he or she wants you to change it. If they allow you to change it, do so. Otherwise, output a message that you cannot continue, and exit. (I have written this code and a function to help you. See the Hints)
check that each file .plan and .project exist. If one does not exist, tell the user to create it and rerun xpinfo. You should check both files before you exit, however. Don't make the user run your program twice to figure out which files he or she must create. (Again, most of this is written for you. See the Hints)
check that each file .plan and .project is world-readable (readable by user, group, and other). If it is not readable, ask the user if you can change the permissions. If they allow you to change it, do so. Otherwise, output a message that you cannot continue, and exit. (You must write this part, but it is very similar to checking the home directory permissions.)
Ask them for a name or title, and a location. Then create four lines to give to chfn as standard input. (When running chfn you must throw away both standard output and standard error so that it is silent.) The four line file should look like this:
name or title (retrieved from the user)
location (retrieved from the use
Again, you must write this last part, but I show you how in the Hints below.
As we have learned, your error messages and prompts must be output to standard error. (If you use the functions provided in the starting version of xpinfo, and the read -p command for your questions, this is done for you.) Here is sample output from a version of xpinfo. In this example, the user's home directory was searchable, his two files existed, but the .plan file had the incorrect permissions:
Welcome to xpinfo - Set your finger personal information
Your ~/.plan file must allow r access for all users.
Do you want me to change its permissions?(y/n):y
Enter your title:Linux Instructor
Enter your location:L413
Your finger information has been updated successfully!
This is a lot of work for a lab, so I have written part of it for you. The file xpinfo in the lab5-online directory in our public work area contains code to
check out the home directory permissions of the user running it, and to ask them to fix them.
check whether each of the ~/.plan and ~/.project files exist. It sets a variable to indicate if one or both files is missing. xpinfo must abort with an error message if this variable is set.
It is also fully documented and contains and uses three functions to help you. (You are welcome to write the entire program by yourself, but xpinfo is about 70 lines of code. I suggest you start with the part I wrote for you and use the functions I wrote to help you when writing your part.)
Creating the file for chfn may confuse some of you. It is easy. Assuming $title is the title retrieved from the user and $location is the location they provided, here are two examples of code:
echo "$title" > $TMPFILE
echo "$location" >> $TMPFILE
echo -e "none\nnone" >> $TMPFILE
chfn < $TMPFILE
rm -f $TMPFILE
echo -e "$title\n$location\nnone\nnone" |
Note that I didn't throw away the output of chfn, which you must do.
Submit a copy of your shell script online for grading by pasting it into the assignment submission box. Then run the transfer program on hills to place a machine-readable copy of your shell script (which must be named xpinfo ) in your share directory as in all assignments like this:
This document was produced with free software: OpenOffice.org on linux.