8.10. Sample Project: junkTo illustrate some of the C shell capabilities, I present a C shell version of the "junk" script project that was suggested at the end of Chapter 6, "The Bourne Again Shell." Figure 8-30 defines the junk utility about to be described.
The C shell script that is listed below (and available onlinesee the Preface for more information) uses a list variable to store filenames. The rest of the functionality should be pretty easy to follow from the embedded comments. #!/bin/tcsh # junk script # author: Graham Glass # 9/25/91 # # Initialize variables # set fileList = () # a list of all specified files. set listFlag = 0 # set to 1 if -l option is specified. set purgeFlag = 0 # 1 if -p option is specified. set fileFlag = 0 # 1 if at least one file is specified. set junk = ~/.junk # the junk directory. # # Parse command line # foreach arg ($*) switch ($arg) case "-p": set purgeFlag = 1 breaksw case "-l": set listFlag = 1 breaksw case -*: echo $arg is an illegal option goto error breaksw default: set fileFlag = 1 set fileList = ($fileList $arg) # append to list breaksw endsw end # # Check for too many options # @ total = $listFlag + $purgeFlag + $fileFlag if ($total != 1) goto error # # If junk directory doesn't exist, create it |