CSC 325 Operating Systems with an Emphasis on UNIX
Assignment 7

In this assignment we will use the conditional structure and it's natural extension the case statement. In addition, we will explore the while loop structure. As you learned in Introduction to Computer Science and subsequent CSC classes, the conditional statement (if statement) is one of the most important of all structures in computer science, so naturally, it is really important in shell programming.

  1. Open a text document, and then use ssh to logon to your Linux account on cs.berea.edu.

  2. Use an editor or some other method to enter the following short shell program called smail into a file in your bin directory:

  3. Make this file executable, and then test this program to see how it works. Explain in your text document what each of the lines of code does. Hint: you might try echo $$ to see what it does.

  4. The next thing we will learn is how to make a command execute at later time. Create a new file called cal which has the single line word cal in it.

  5. Next use the man pages to read about the at command and then read /usr/share/doc/at-3.1.8/timespec to learn how to specify the run time. Note: the command at is similar to the command cron, but cron is only available to system administrators. For example, try typing at -f cal now + 1 minute to have the command(s) executed from your new cal file in one minute. Try typing atq to see a list of your current scheduled future jobs. Copy and paste your output into your text document.

  6. Next use the date command to find out the current time and then execute the at command to make the cal command run in a couple of minutes without using the now time. Note that the standard output of the at command is redirected to mail, so you should get some new mail from both of these runs. Copy your solution into your text document.

  7. Now you are ready to write your first daemon! Create a new shell program which will change the current directory to .trash from any other directory. (HINT: Be absolutely certain that your code to do this works before including the next command to remove all the files in this directory.) Next include the command to remove all the files in this directory. Finally, determine a way to use the at command to make this process run tonight at 2:00 AM. Explain your solution in your text document, being sure to include all your code, commands that you create, and their usage.

  8. Can you determine a way to use the at command to make this same process run EVERY NIGHT at 2:00 AM? Explain how this can be done or why it cannot be done.

  9. In the last lab you created a shell program called dl which moves any number of files from any of your directories into your .trash directory. In this assignment, we will modify this shell program so that the computer checks to see that you are sure before moving the files. To do this we will need to use the shell conditional statement which is called if. Read through the bash the manual pages on the if command. To test the parameter we will need the test command. Read about the test command in the test man pages.

  10. We can use the echo command to type on the screen, but we will need a command to read the response. This command is called read. Read about read in the help pages. Note that to read in a response, you will need to create a new parameter to hold the user's response. The first time you refer to the parameter, the parameter will have no $ and will be created, but after it is created, it must be refered to preceeded by a $. i.e. You might want to use a parameter called $CHECK or $OK which will be created when you execute the commands read CHECK or read OK.

  11. Modify your dl program to do the following:

  12. Test dl to see that it works as expected. Include a copy of your modified program in your text document.

  13. Modify your dl pro gram further to test to see that the user has included the names of at least one file to be moved. If the user has included no filenames to be moved, inform the user what the trouble is. Note that to do this, you will need to use the if and test commands again, however, the test for strings and the test for arguments differ. Reread the man pages for test with this in mind. Modify your dl program and test it to see that it works correctly. Include a copy of your modified program in your text document.

  14. The case statement is an extension of the conditional statement which is generally used when there are more than two possible outcomes of the conditional test. Thus the case statement avoids many nested conditional statements. The case statement compares a word to a sequence of patterns. The structure of the case statement is (this information appears in the bash man page):
  15. Here the commands inside each pattern are separated by a single semi-colon and each new set of commands is separated by a double semi colon. Your task is to write a new shell program called dayst which determines which day of the week it is and then writes a cute or helpful or appropriate message on the screen depending upon what day it is. For example if it is Saturday, maybe you would want to tell yourself to go outside now and work later! Copy your code for this program into your text document.

  16. If you wanted to have the dayst program run every time you login, what would you do? Hint: See chapter 11 of the Linux text. Explain in your text document.

  17. The Linux command expr is used to evaluate expressions. Read the man pages on expr. Believe it or not this is a pretty good man page. It even includes examples! Try typing expr 1 + 4 at the command line, and then try some of the examples from the man page. Explain in your own words in your text document what this command does.

  18. Write a new shell program called inc which takes a number as input and increments it by 1, outputting the result. Copy your code for this program into your text document.

  19. Read the section of the bash man page which lists loops. The most standard loop which exists in all of the shells is the while loop which has format:
  20. where I suggest that you use the Linux test command in test command with arg1 OP arg2

    and OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than arg2, respectively. Create a new shell script called repeat which takes as the first parameter the number of times you would like a statement echoed, and then takes as the remaining parameters the statement which is to be echoed. If the initial count is less than zero, write an error message. Otherwise your program outputs the comment the correct number of times. For example, repeat 3 Happy Birthday to you. should print "Happy Birthday to you." on the screen three times. Note: If you somehow end up with an infinite loop in your testing, open a new telnet session, use ps -al to list all your processes, and kill your old bash with kill -9 PID. Copy your code for this program into your text document.

  21. Read the test man page again. It tells us how to test file types. Next modify your dl program which you created in the last couple of labs so that it checks each file to see if the file actually exists before attempting to move it. For each of the files, have your program print out "filename moved to .trash" or "filename does not exist. Depending upon which is true. Copy your code for this program into your text document.

  22. Save your text document as yourlastnameA7.txt.