CS345 Operating Systems
Spring 2004
Take-Home "Final Exam"


Before doing this exam you should have read chapters 3 and 4 in Unix for Programmers and Users.  You may  use the textbook, as well as any other resource (except other people),  to help you answer these questions. You should answer each question specifically as well as write the commands you used to obtain the information necessary to answer each question. Please don't just scribble on this sheet of paper but rather create an answer sheet(s) with a word processor. Also, please give me a listing of each  script, as well as its output. Leave all your scripts in your directory on anita.simmons.edu so that I can easily find and execute them.

1)  Access the /etc/passwd file and determine your default shell. What is it?
 

2). Use the chsh command to determine what shells are available, what are they?
 

3). What command will list all files that begin with the letter 'a' or 'b'? How about all files that begin with any letter between a and e or m and q?
 

4).  Type the commands   $ls  xyz123  &&  echo  test
          and                            $ls  xyz123     ||    echo test

      What output does each command produce?  When does the word "test" appear on the screen
       and why?

5). Type one command  that will output a single integer number equal to the number of processes
     you are running. What is the command?   (hint you will have to use pipes, ps and maybe grep
     and wc)

6). Type one command that will output a line of text:

                             The number of processes on the system is  nn

           Where nn is an integer number like 50 etc.

           (hint - you will have to use the echo command and command substitution)


7).  Write a script that produces the following output
               date and time
               My working directory is  xxxxxxxxx
               The number of files in my directory is  yyyyyyyy

       where xxxxxxx is your actual directory ie /home/tis
       and     yyyyyyy is an integer

8). What are the values of the following environment variables: $HOME, $PATH?

9). Create a  variable called "course" and equate it to "CS345". Write a script that displays the
       name of the shell script, the process ID and the value of the course variable. What does the
      script output?  Do you have to do anything special to get the value of the variable course
      displayed properly?

10). Write a script which will accept 3 command line arguments and print them out.

11) Write a script that takes a single command line argument, the login name of a user, and checks to see if that user is logged in. If the user is logged in the script should output "User xxx is logged in", if the user is not logged in the script should output "User xxx is not logged in" - where xxx is the login name of the user. If the user forgets to type the single argument an appropriate error message should be issued from your script.

12) Write a script, with no command line arguments or user input,  whose output will be a function of the time of day the script is executed. If run between midnight and noon the script will output "Good morning, xxx", if run between noon and 6pm the script will output "Good Afternoon, xxx", and if run between pm and midnight the script will output "Good evening, xxx". Where xxx is the login name of the user.

13) Write a script called junk which takes one or more file names as arguments. The script should move each of the files specified to a subdirectory called .junk in your home directory. If the subdirectory does not exist the script should create it. The script should output to the screen the name of each file moved as well as the total number of files moved.

14) Write a script that manages a telephone directory(file). Each entry in the directory will contain first name, last name and telephone number. The script should be called "tel" and have the possible command line commands and arguments -

            tel -a Harry Jones 521-2703   where -a means add the following first name, last name and telephone number to the directory
            tel -f  Jones                                where -f means find the entry for the last name and display it
            tel -d Jones                                where -d means delete the entry for the last name
            tel -s                                            where -s means displays the directory  sorted by last name

Your script should handle all error conditions gracefully i.e. invalid number of arguments, missing file, invalid option etc.

Here are some hints
    - use positional arguments i.e. $1, $2 etc. to refer to the arguments on the command line
    - use $1 to drive a case statement for the different options
    -  in order to write to the directory file use input and output redirection where the input comes from a string of variables specified on the command line and the output is the directory file called telephone
    - refer to the man pages for the grep option which will allow you to delete lines containing a string, or rather copy all lines that don't have a specific string