Using find and various other Unix commands in a Bourne shell script, you are to create a program that will remove various files found in you directories. You can call the program Clean.
Clean will take either command line arguments (part 1) or provide user with a menu of actions (part 2) indicating the desired actions. Choices are to delete: core files (generated when a program crashes), files with obj extensions (created when a program is compiled from source code), files of a specified size, or files of a name specified by the user.
Clean will use find to generate a list of files of interest. In some case, find may be able to both find and remove the files. In other cases, find must be used with other commands to allow better control by the user.
If an option/arguments are specified when Clean is invoked, the following option/argument action relationship will be implemented.
-c - remove all core files without any further interaction by user.
-o - remove all files with obj extensions. However, prompt user before deleting each found file. Use the conditional option of find to prompt.
-O - remove all files with obj extensions without any further interaction by user.
-s #### - selectively remove files that are of the number of bytes specified by ####. #### may be signed. If -, then files of interest will be smaller than specified, if +, then larger, and if no sign, then of an exact size. Show long listing for the located file and prompt user before deleting found file. Use the find with the long listing option to generate the list of candidates. Use the for loop to parse though this list, displaying the retrieved data and invoke a separate rm command on the files the user approves for deleting.
-n name - selectively remove all files that match the name specified by name
Use the long listing and for loop as with the size option.
dname - if a name is specified without a preceeding -n as the 1st argument or an additional argument after the options listed above, find should search from the directory indicated by the dname. Otherwise search from the directory in which Clean was invoked.
If not option specified, use a function to display a menu of the available options. Use the here form of redirection to display a pre-formatted menu. The function will also take the menu selection from the user and confirm its validity. Since the output of the function is the menu, use the return feature to indicate to the calling code which selection was chosen.
Basic structure of menu.
1. Remove core files. 2. Remove selected object modules. 3. Remove all object modules. 4. Remove selected files of a size to be specified. 5. Remove selected files of a name to be specified. E. Exit program. Enter choiceAssign an return code for each menu choice. When the menu is invoked in the body of the program and returns, preserve and test the specific return code. Use a case to examine the code returned. Use set to set the command line arguments to the appropriate values. By doing this, your main program block will work for either command line or menu selected options.
If menu choice is 4 or 5, prompt user for the appropriate additional information. Do not worry about error checking, that will be done later.
The main body of the program
Use a loop with an always true condition, "while true", as the program loop driver.
For the assignment, replace the rm command with an echo of the rm command and any arguments. This will protect against accidentally deleting something important and also allow to repeatedly test the program without having to constantly recreate the files being deleted. Hand the program in in this form.
Test the count of command line arguments, if count of 1 or 2, test to see if the 1st argument does not start with a - indicating that it is not an option. If this condition exists, test that the 1st argument is a valid directory name. If it is valid, save in a variable and discard the command line argument (shift).
If the first argument exists and is an option, test for a second argument that represents a valid directory. If it is valid, save in a variable.
If there are more than 2 arguments, give a short explaination of correct syntax and terminate program.
Check the number of command line arguments and if there are none or the 1st argument does not start with a - (argument not option but a specified directory to start search from), then:
Copy the return code to a user variable
If the return code indicates termination, then break out of loop and allow the program to terminate normally
Otherwise test the return code and set the command line arguments.
If the option is -c, then delete all core files. Use find to generate the list of files to delete and to issue the delete command. Use the non-interactive option (-exec).
If the option is -o, then use find to delete all files with a .obj extension. Use finds interactive option (-ok) or use rm -r to invoke an interactive form of the rm command. Remember to only echo the rm command.
If the option is -O, then use find to delete all files with a .obj extension. Use finds non-interactive option (-exec).
If the option is -s, then test (use grep) the second argument on the command line for a valid numeric syntax. A single sign of + or - is acceptable at the beginning of the argument. If second argument is missing or non-numeric argument, continue at the top of the driver loop. Use find with the long listing option in a for statement to generate and parse the list of identified files. Then show the data found for each file, prompt the user to confirm deletion and proceed accordingly.
If the option is -n, then treat the second argument as the filename to search for. If the filename was quoted when specified on the command line, wild cards may be included and should be honored. If second argument is missing, continue at the top of the driver loop.
Else if the option is un-identifyable, indicate problem and return to top of loop.
Part 1. Start by writing the body of the program as a sequential itteration that handles the real command line arguments and terminates after processing one request. Do not code the loop or the menu features initially. Get the option case, argument tests, and find implementation working first.
Part 2. Then when that is working, then code the menu and looping mechanism to allow a user friendly program.