Bourne shell programming - Due Wednesday 8 August 2006
80 points. 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.

directory_name - if a name is specified as the 1st argument, find should search from the directory indicated. Clean needs to test for a valid accessable directory before proceeding. Otherwise search from the directory in which Clean was invoked. Clean should store the directory of choice in a local variable and discard it from the command line if so specified. If no starting directory specified, set the local variable to the current working directory.

-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. Use find in the for part of the for statement to generate a list of qualifying files. In the body of the for, do an ls -l on each file and an interactive rm command to select which files to remove.

-n name - selectively remove all files that match the name specified by name

Use find's type filter to catch only regular files then run remove using find's interactive -ok rather than -exec.

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, it must be between 1 and 3. Test to see if 1st arguement is a valid accessable directory.

If it is,

If arguement count is 1
Else if arguement count is 2
Else

For the 1st (single arguement) function

Use a case to test the 1st argument in the command line argument list.

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 -i 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. Do not ask for user confirmation.

For the 2nd (two arguements) function

If the option is -s, then test (use grep) the second argument from the command line for a valid numeric syntax. No sign or a single sign of + or - is acceptable at the beginning of the argument followed by digits as an integer number. If second argument is missing or non-numeric, print error message and terminate. Otherwise, code as described above.

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, terminate with a error message. Otherwise, code as described above.

Else if the option is un-identifyable, indicate problem and terminate.

Note: all processing can be done in the shell environment. There is no reason to create external files.

Extra credit 20 points.

Put main in an always true loop structure. If use starts program with correct arguements, then process and rather than quit, prompt user to do another search or quit. If user chooses to search again : prompt for a directory, if empty, use current directory or use user's choice.

List choices

If choice needs another arguement, prompt from user.

You now have same data as valid on command line. You can use set to reload command line arguements and process the same.

If program starts with no arguements, then perform the prompts and process.