Assignment #4 - Advanced Shell Script Assignment

Due: FRIDAY JULY 29th, 11:59:59PM

Objective

To practice with shell scripting, and learn the basic skills to write shell scripts that perform simple tasks involving the monitoring of directories and files.

Task

A given UNIX directory may contain many files and subdirectories. For the purpose of this assignment, a "file" is a nondirectory file, and a directory is a subdirectory of the given directory. Each file or directory might have different permissions. For example, a file might have read and execute permissions for the owner. A directory might have only read permission. The directory is to be specified as a single command line argument to your script. You do not need to account for any contents of nested subdirectories -- just the direct contents of the single specified directory which will be the single command line argument to your script. In this assignment, you will write a shell script that calculates the following information for the contents of a given directory:
- The total number of directories that are in the given directory (note that these are subdirectories).
- The total number of files in the given directory.
- The number of items (files/directories) in the given directory that are readable.
- The number of items (files/directories) in the given directory that are writable.
- The number of items (files/directories) in the given directory that are executable.
- The total number of items in the given directory.
Note that any hidden files or hidden directories are not included in this count. For example, ".." represents the parent directory of the current directory and is not counted as a directory or as a file. Similarly, ".login" would not be counted. Only regular files and directories in the given directory are calculated.

Requirements

  • The script you write should be named assignment4.sh
  • Your script must check for the correct number of arguments (one argument). If somebody tries to invoke the script without passing in the correct number of arguments, then output this exact usage message:
    Please provide one directory to search
    Example usage: assignment4.sh directory_name 
    and then abort the script (i.e. no further processing)
  • Your script must also check that the command line argument actually exists and is a directory. If it is not, then output an appropriate error message, and end the script processing. Example: The user tries the command:
    assignment4.sh abcd
  • If abcd is not a valid directory, then output:
    abcd: Invalid Directory. Aborting Script.
  • The script should print all of its results to standard output (no redirection necessary). Include comments at the top of your script file, which specify your name, the course (COP 3353), the assignment number, and the date

  • Hints

    Scripting basics (Lecture6)
    Variables and command line arguments (Lecture6, Lecture8)
    if statements (Lecture8)
    for loop (Lecture8)
    Command substitution (Lecture 8 - end)
    The expr command (Lecture 8 - end)
    

    Sample Runs (to see how your script should function)

    vastola@shell.cs.fsu.edu:~>assignment4.sh 
    Please provide one directory to search
    Example Usage: assignment4.sh directory_name
    
    vastola@shell.cs.fsu.edu:~>assignment4.sh temp
    temp: Inavlid Directory. Aborting Script.
    
    vastola@shell.cs.fsu.edu:~>assignment4.sh unix1
      Number of directories     : 4
      Number of files           : 3
      Number of readable items  : 6
      Number of writable items  : 4
      Number of executable items: 5
      -----------------------------
      Total Number of items: 7
    
    End of Sample Runs.
    Note that this last run is based on a directory (unix1) with these contents:
    d--x------  3 vastola CS-Faculty 4096 Mar  2 01:49 backup
    drwxr-xr-x  2 vastola CS-Faculty 4096 Mar  2 01:49 code
    -r--------  1 vastola CS-Faculty   22 Sep 24  2009 inputfile1
    -rw-r--r--  1 vastola CS-Faculty  289 Mar  2 01:55 output.log
    -rwxr-x--x  1 vastola CS-Faculty   74 Mar  2 01:53 reset.sh
    dr-x------  2 vastola CS-Faculty 4096 Mar 23 18:23 temp
    drwx--xr--  3 vastola CS-Faculty 4096 Mar  2 02:10 things
    

    Submitting your Script

  • Make sure you are logged into shell.cs.fsu.edu and the file you want to submit is stored in your current working directory. To submit, type the following command:
     ~vastola/usub/submit4 
    where the name of your shell script is the filename. This should be:
     ~vastola/usub/submit4 assignment4.sh
    This will run a script and a C++ program that copies your file into a submission directory. The program will also give you feedback at the end -- it will display the contents of the file you just submitted to standard output. This will allow you to check to make sure that what you submitted was correct. You may resubmit if needed, just be aware that your last submission will be the one that is graded REMINDER: As in all courses, the academic honor code has been and will continue to be upheld. ALL programs will be run through plagiarism detection software. Make sure your work is YOUR WORK.