When a professor has students submit multiple files to moodle for an assignment, and then downloads all the submissions for grading, Moodle gives the professor a zip file containing multiple poorly named directories containing files. The professor then has to nagivate up and down the file system hierarchy to see all the files for grading. Additionally, the directories often have spaces in their names, which makes it very difficult to navigate via a command line -- which is the preferred method for professors, who are "power users".

For this project, you will write a bash script to unzip this Moodle download, and rename the directories so that a student's submissions are all in one directory and that directory has no spaces in its name.


One feature that distinquished UNIX from its predecessor operating systems was its provision of the shell, which provides much of the UNIX user-interface, plus a built-in shell command language. Over the years, various shells have been created for UNIX, including:

and others. Different shells have different features and conventions, including different command-languages.

sh is the original UNIX shell, and is called the bourne shell after Steve Bourne, its creator. While later shells such as csh, ksh, tcsh, and bash extended the features of the bourne shell, it is still available on every UNIX system. However, the Bourne again shell has now become the default shell, and thus most shell scripts are written in that language.

To create a shell script, you simply store a sequence of shell commands in a file, and then make this file executable (using the system chmod command). Typing the name of the file then executes the commands within it.

The commands that can appear in a shell script include both

  1. system commands (e.g., ls, ps, cd, mkdir, rmdir, ...); and
  2. shell commands, which are unique to a given shell.
The shell commands for a given shell are specified on its manual page (e.g., type man bash to read about the bourne again shell commands, man csh to read about the c-shell, etc.). Such commands include selection commands (e.g., if, case), repetition commands (e.g., for, while, until), and other commands useful for problem-solving.

For safety, shell scripts should be executed in a shell that is distinct from the login shell. For the bourne shell, this is accomplished by making

the first line of the shell script. The effect of this line is to make the system
  1. execute the bash shell (as a separate process), and
  2. pass the script to that shell to be executed.
This has a beneficial side-effect of indicating to a reader the particular shell language the script is written in.

Do this...

In /home/cs/232/ you will find a file called 17FA CS-332-A-Firewall Lab-678308.zip, which is a zip file I downloaded from my CS332 lab in the fall of 2017. When you unzip this file, you'll find this hierarchy of files:

Notice that many of the directories and some of the file names have spaces in them. Most inconvenient! Also, each person's submissions are in two different directories. Yuck.

Your assignment is to write a bash script to accept a zip file as an argument, unzip it into a new directory, and then rename files and folders to remove the spaces and other "cruft", like the numbers and "assignsubmission" junk. E.g., when you run your script the above hierarchy will to be converted to this hierarchy:

Note these requirements:

You can find tutorials for bash scripting on the web. I found these to be very useful:

Shell-script comments begin with the # symbol and end at the end of a line. Your script should include full documentation and should use white space to reflect its structure. You do NOT have to write a comment for every line of code.

Hints / Thoughts

  1. Spend a few days studying the bash command language, using the bash manual page and/or WWW tutorials.
  2. Here is some code to get your started. Don't say I never did anything for you...
    removespaces() {
        echo "$1" | sed -e "s/ //g"
    nospaces=$(removespaces "$inputfile")
  3. Begin slowly. Write a line of code and see if it works. If it doesn't, get it working before moving on. Test little bits of code by putting them in their own script file first and getting them working there.
  4. Run your script file this way: bash -x ./moodlefix options and file name here. Using -x gives very valuable debugging output.
  5. Read up on using regular expressions. This helps a lot in this project.
You may discuss with your classmates broad ideas and basic approaches on how to solve the different part of this problem. However, you may not look at anyone else's script.

If you have not written a shell script before, then this may be a long project, because you will have a great deal of knowledge to acquire. Getting the shell command-language syntax right is tricky, and may take some trial and error... Don't procrastinate!

As with a normal program, your script is to be carefully documented, and written with attention to readability by someone other than yourself. Make your code hospitable!

Turn in: Your program. You do NOT have to submit a typescript file. I'll run each program to make sure it works.

Submit your code to /home/cs/232/current/<yourid>/homework08/.

Due date: Tuesday, May 1, 11:59:59 p.m. (i.e., one minute before midnight)

CS > 232 >

This page maintained by Victor Norman.