Korn shell script counts different file

profileJack Jonshen
9.8.txt

Write a Korn shell script filecount which counts the number of ordinary files (defined as everything except the following), number of executable files, number of links, and number of directories in one or more directories which are provided as commandline arguments. Requirements ? The above counts include dot files, except that . and .. are not included in the directory count (investigate the -A option to ls). ? Files in sub-directories are not included in the counts. ? The distinction between file types is the same as that of ls -F. ? If the script is invoked with no directory name provided, it must work on the current directory. Otherwise, it must produce a single line of output for each directory it processes, as in the following sample (on fictitious locations): 1 $ ./filecount 2 . : 10 ordinary 9 executable 3 links 5 directories 3 $ 4 $ ./filecount courses tmp 5 courses: 2 ordinary 8 executable 7 links 42 directories 6 tmp : 8 ordinary 17 executable 5 links 51 directories ? The script must support the following command-line options: -f: include the count of ordinary files in the output -x: include the count of executable files in the output -l: include the count of links in the output -d: include the count of directories in the output If any of these options are specified when the script is called, then only the requested totals must be printed for each directory. ? If an invalid option <option> is given, the script must print ./filecount: Illegal option: <option> and a usage message to stderr and halt with a exit status 1 as shown below. 1 $ ./filecount ?t 2 ./filecount: Illegal option ?t 3 Usage : filecount [?dflx] [directory . . . ] 4 $ echo $? 5 1 ? If an invalid directory <directory> is given, the script must print ./filecount: Invalid directory: <directory> and a usage message to stderr and halt with a exit status 2. 1 $ ./filecount somedir 2 ./filecount: Invalid directory: somedir 3 Usage : filecount [?dflx] [directory . . . ] 4 $ echo $? 5 2 ? The script must execute using the Korn shell interpreter (ksh). You may not use C, C++, Perl, Python, Ruby, or any similar language. ? The script must run at the command line as: ./filecount [-dflx] [directory ...]. ? The script must have -rwxr-x-- permission. ? The script must terminate with a proper exit statement. ? Do not use any specific aspects of your environment within the script. In other words, use native Linux command names as opposed to your environment¡¯s aliases for those commands and do not rely on any specific aspect of your environment (e.g., values of particular shell variables). ? Each line of output must separate the directory from the counts with a single colon (:) followed by exactly two spaces. Delimit each count from its label with a single space and delimit each count label pair from each other with exactly two spaces (exactly as shown above). Always print the ordinary count first, followed by the executable count, then the link count, and finally the directory count, if requested, regardless of the order in which the options are given on the command line. ? Each line of output must not contain any leading and trailing whitespace or any extraneous text. ? All options must precede all directories on a command line. ? Use - to indicate the end of options. ? Options can be given as singletons (e.g., -x) or in any combinations (e.g., -fx, -xf, -fxld). ? The file counts are mutually-exclusive. One file must never be counted twice. Anything that is not a directory, symbolic link, or executable, is an ordinary file. ? Executable files are to be counted as executable only, not executable and ordinary. ? The script must not create any new files or remove any existing files. ? The script must not create any new directories or remove any existing directories. You are encouraged to make creative use of the given tools (grep, sed, awk, and others) and string operators (i.e., do not reinvent the wheel). Remember, grep, sed, and awk can be used on shell variables (e.g., $(echo $PATH | sed ¡¯s/:/2/g¡¯)). Also, explore getopts (though not necessary), ls -A, print -n, and print - -n. If designed properly, the script required for this homework should occupy no more than 100 lines of code.