Section 8.10. Sample Project: junk


[Page 321 (continued)]

8.10. Sample Project: junk

To 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.

Figure 8-30. Description of the junk shell script.

Utility: junk -lp { fileName }*

junk is a replacement for the rm utility. Rather than removing files, it moves them into the subdirectory ".junk" in your home directory. If ".junk" doesn't exist, it is automatically created. The -l option lists the current contents of the ".junk" directory, and the -p option purges ".junk".



[Page 322]

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 
[Page 323]
# if (!(-e $junk)) then 'mkdir' $junk endif # # Process options # if ($listFlag) then 'ls' -lgF $junk # list junk directory. exit 0 endif # if ($purgeFlag) then 'rm' $junk/* # remove contents of junk directory. exit 0 endif # if ($fileFlag) then 'mv' $fileList $junk # move files to junk directory. exit 0 endif # exit 0 # # Display error message and quit # error: cat << ENDOFTEXT Dear $USER, the usage of junk is as follows: junk -p means "purge all files" junk -l means "list junked files" junk <list of files> to junk them ENDOFTEXT exit 1





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net