Assignment #4 - Advanced Shell Script Assignment
Due: Monday, July 30th, 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.
The goal of your script is to ORGANIZE the files within your current working directory.
You do not need to account for any contents of nested subdirectories -- just the direct contents (files only) of the current working directory (where the script is located).
In this assignment, you will write a shell script that organizes photos, text files, or code files, into an appropriate directory. Your script will only ever
organize one group of these items at a time, given by a command line parameter to the script. IE: if the script is run like:
> organize.sh photo
You would only be organizing the photo files in the current working directory.
-Photo files are denoted by any file ending in .jpg and should be organized into a directory named photo (placed within your CWD)
-Text files are denoted by any file ending in .txt and should be organized into a directory named text (placed within your CWD)
-Code files are denoted by any file ending in .cpp or .h and should be organized into a directory named code (placed within your CWD)
Requirements
The script you write should be named organize.sh
Your script must check for the correct number of arguments (exactly one argument). If somebody tries to invoke the
script without passing in the correct number of arguments, then output this exact usage message:
>organize.sh
Must give a category to organize, either type photo code or text as a parameter to the script.
Example Usage > organize.sh photo
and then abort the script (i.e. no further processing)
Once the user has entered a command line parameter, determine whether it was photo, text, or code. If a directory for those files does not already exist, make one and move
only the matching files into the appropriate directory. If it does already exist, just move the files.
Example run (i've also showed the directory contents before and after the script run to be clear):
vastola@linprog2:AssignmentSols/newA4/test>ls
a.txt b.txt c.txt filea.jpg fileb.jpg filec.jpg organize.sh thing.cpp thing.h
vastola@linprog2:AssignmentSols/newA4/test>organize.sh text
Organizing text files...
moving a.txt ...
moving b.txt ...
moving c.txt ...
Moved 3 text files to text directory. Complete.
vastola@linprog2:AssignmentSols/newA4/test>ls
filea.jpg fileb.jpg filec.jpg organize.sh text thing.cpp thing.h
If there are no files to move in that category, this is what your
output should look like:
vastola@linprog2:AssignmentSols/newA4/test>organize.sh text
Organizing text files...
Moved 0 text files to text directory. Complete.
The script should print out each file that it moves, as well as a count of how many it moved when it is completed. See sample runs.
Include comments at the top of your script file, which specify your name, the course (COP 3353), the assignment
number, and the date
If the directory you're creating already exists, do not create it again (you'd see an error message appear). You should never see any error messages appear such as:
mkdir: cannot create directory text: file exists
Your script MUST employ if statements and for loops. This is a REQUIREMENT.
Hints
Scripting basics (Lecture6)
Variables and command line arguments (Lecture6, Lecture8)
if statements (Lecture8)
for loop (Lecture8)
Command substitution (Lecture 8 - end) (for counting stuffs....)
The expr command (Lecture 8 - end) (for counting stuffs...)
Sample Runs (to see how your script should function. I'll show the contents of my directory too so you can see how it should work.)
Wrong number of Parameters:
vastola@linprog2:AssignmentSols/newA4/test>ls
a.txt b.txt filea.jpg fileb.jpg filec.jpg filetxt organize.sh thing.cpp thing.h thisisadirectory.jpg
vastola@linprog2:AssignmentSols/newA4/test>organize.sh
Must give a category to organize, either type photo code or text as a parameter to the script.
Example Usage > organize.sh photo
vastola@linprog2:AssignmentSols/newA4/test>organize.sh text photo
Must give a category to organize, either type photo code or text as a parameter to the script.
Example Usage > organize.sh photo
Note on this example below: "thisisadirectory.jpg" does not get moved into the photo folder because it is not a file.
vastola@linprog2:AssignmentSols/newA4/test>organize.sh photo
Organizing photo files...
moving filea.jpg ...
moving fileb.jpg ...
moving filec.jpg ...
Moved 3 photo files to photo directory. Complete.
vastola@linprog2:AssignmentSols/newA4/test>ls
a.txt b.txt filetxt organize.sh photo thing.cpp thing.h thisisadirectory.jpg
Other sample (note, if user enters parameter that's not valid, no directory should be made, and no files should be moved):
vastola@linprog2:AssignmentSols/newA4/test>ls
a.txt b.txt filetxt organize.sh photo thing.cpp thing.h thisisadirectory.jpg
vastola@linprog2:AssignmentSols/newA4/test>organize.sh photo
Organizing photo files...
Moved 0 photo files to photo directory. Complete.
vastola@linprog2:AssignmentSols/newA4/test>organize.sh text
Organizing text files...
moving a.txt ...
moving b.txt ...
Moved 2 text files to text directory. Complete.
vastola@linprog2:AssignmentSols/newA4/test>ls
filetxt organize.sh photo text thing.cpp thing.h thisisadirectory.jpg
vastola@linprog2:AssignmentSols/newA4/test>organize.sh SPONGEBOB
Organizing SPONGEBOB files...
Moved 0 SPONGEBOB files to directory. Complete.
vastola@linprog2:AssignmentSols/newA4/test>ls
filetxt organize.sh photo text thing.cpp thing.h thisisadirectory.jpg
vastola@linprog2:AssignmentSols/newA4/test>organize.sh code
Organizing code files...
moving thing.cpp ...
moving thing.h ...
Moved 2 code files to code directory. Complete.
vastola@linprog2:AssignmentSols/newA4/test>ll
total 20
drwx------ 2 vastola CS-Faculty 4096 Apr 4 14:09 code
-rw------- 1 vastola CS-Faculty 0 Apr 4 14:01 filetxt
-rwx------ 1 vastola CS-Faculty 1015 Apr 3 16:48 organize.sh
drwx------ 2 vastola CS-Faculty 4096 Apr 4 14:01 photo
drwx------ 2 vastola CS-Faculty 4096 Apr 4 14:03 text
drwx------ 2 vastola CS-Faculty 4096 Apr 4 14:01 thisisadirectory.jpg
End of Sample Runs.
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 organize.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.