shell programming assignment

I have a very tough shell program to do. Here is the assignment:

Write a non-interactive script that takes in any number of directory names as arguments and calculates and outputs the total number of blocks of disk space occupied by the ordinary files in all the directories. For example, the user would type:

                       assignment3        dir1       dir2       dir3       dir4

                       403

and the output is simply the number of blocks occupied by ordinary files in those directories. If, let�s say dir2 was not a directory and dir4 was inaccessible due to lack of permission say, then the program output (including errors) might be:

                       assignment3        dir1       dir2       dir3       dir4

              assignment3: WARNING: dir2 is not a directory.

                       assignment3: WARNING: dir4 could not be accessed.

                       403

The following script will find and output the number of blocks occupied by the filename supplied as an argument;

if test �e �${1}�

then

ls -sd ${1} | sed 's/^ *//' | cut -d' ' -f1

  else

              echo 0

  fi

If the file given as an argument exists then ls will get the size in blocks and the filename and pass them to sed; sed will strip the leading spaces and pass it to cut; cut will extract and output the number of blocks. If the file given as an argument does not exist then 0 is output (a file that does not exist occupies no space).

This script will be placed in your home directory and named getblocks. You can use it in your script to get the number of blocks occupied by a file. Use it in your script in the following way (I am using a file called �file17� as an example � in your program the filenames will be held in variables):

     \(\( numblocks=$\(getblocks    file17\) \)\)

This line will calculate the number of blocks occupied by a file called file17 and assign it to variable numblocks.

Your script should give sensible error messages and/or warnings where appropriate. For example, you should detect and report on the following possible errors or warnings:

  1. no arguments given: ERROR � need to exit the program;
  2. the argument being processed is not a directory: WARNING � no need to exit� just continue to the next argument;
  3. the argument given names a directory that cannot be accessed (use ls command to determine this): WARNING - no need to exit� just continue to the next argument.

To tackle this assignment you will need to know:

  1. how to capture the output of a command inside a script using �command substitution�; (see lab 7)
  2. how to write a for loop that will process a list of files from a directory named in the argument list; (see lab 7)
  3. how to write a while loop that will continue to process the arguments until they are all dealt with; (see lab 7)
  4. how to create (and initialize) integer variables, and lowercase and uppercase string variables. (see lab 7)
  5. how to do simple arithmetic using the (( )) command. (see lab 7)
  6. how to check if a file is an ordinary file;
  7. how to check if a file is a directory file.
  8. how to write error messages, usage messages, and warning messages;

I just need some help in getting started on it.
Thank you.