CSE 2031 - Lab Test 2 question

What to do

Requirements

Your script will receive 0 or 1 arguments. If there is an argument, then you can assume that it represents an accessible directory (you do not have to validate that it is indeed an accessible directory). If there is no argument, then the directory to work with will be the current working directory. Any extraneous arguments to the script are ignored.

The first task of your script is to count the number of empty files in the provided directory, i.e. the number of files of size 0. Ignore any subdirectories. Your script must then output a message showing the number of empty files as in the sample run below. This is followed by a list of the names of the empty files in alphabetical order (one filename per line).

The second task of your script is to output the average number of characters for all non-empty files in the provided directory. Ignore any subdirectories. The average must be given with a precision of two decimal digits with a message as in the sample run below. If there are no non-empty files in the directory the average must be shown as 0.00

Following is a sample run that first shows the contents of the provided directory and then the output of your script:


% ls -l dir1
total 12
-rw------- 1 bil faculty    0 Nov 29 16:40 empty
-rw------- 1 bil faculty    0 Nov 29 16:40 nothing
-rw------- 1 bil faculty  894 Nov 29 16:54 somefile
-rw------- 1 bil faculty 2761 Nov 29 16:40 someotherfile
drwx------ 2 bil faculty 4096 Nov 29 16:54 subdir/
-rw------- 1 bil faculty    0 Nov 29 16:40 vacant
% lt2f.sh dir1
Found 3 empty files
dir1/empty
dir1/nothing
dir1/vacant
Average number of characters for non-empty files: 1827.50