Richard JP Le Guen.ca

Skip to Content
All about Software Development on the WWW
RSS feed

Navigation

Soen229 Tutorials

Tutoring Courses

Programming Assignment #1

Your first programming assignment is to write some scripts which behave like MS-DOS commands, and for bonus marks, a command line interpreter which executes those commands. Both the commands and the command line interpreter will be run in a Linux environment.

Ms-Dos Commands as Perl Scripts

We will be expecting you to submit four separate scripts which emulate the basic behavior of the Ms-Dos commands listed in the following table. Take note of which ones will have to be able to accept piped input. You will have to research them a little to know what arguments they take. You do not have to parse switches or options; only arguments.

Command What does it do? Accepts piped input?
CLS clears the screen no
FIND searches a file (or other input) for a text string yes
DIR lists the files in the current directory no
TYPE displays the contents of a file onscreen yes

Please provide comments in your code to explain what the code does to prove that you understand your own code.

For Bonus Marks:
An MS-DOS-style Command Line Interpreter

The most important thing for this assignment is to write the four command scripts in the table above. Should you also write the command line interpreter, here are the key characteristics which we will be looking for:

Prompt

The command line interpreter should present the full path to the current directory, followed by a '>' as the prompt. For example:

/home/r_leguen>

Syntax

COMMAND-NAME [ARGUMENTS…]

The COMMAND-NAME is the name of an executable which will be found in the search path. Note that I did not need to type "perl" into the command line interpreter to execute anything.
ARGUMENTS is a list of text strings the command uses or needs, such as filenames etc.

Additional marks will be given for the ability to pipe output from one executable command as input for another, using a '|' character. When you pipe the output of one command into another, the output need not appear on screen.

Your command line interpreter should parse the user's input into a COMMAND-NAME and ARGUMENTS, and then check for the existence of an executable file with a name matching the COMMAND-NAME in your command line interpreter's search path. If the executable file exists, it executes that file using the same arguments.

How Does a Command Line Interpreter Work?

Bear in mind the following when writing your script:

  • Keyboard input is passed to the command line interpreter, which processes it and executes commands by looking for an executable of the same name in a search path.
    • To see what the search path is on your Linux machine, type 'echo $PATH' in on the command line.
    • To see what the search path is on your Windows machine, type 'path' in on the command line.
  • The executable runs, making calls to the system to perform a particular task.

So let's take an example, using the ps command on a Linux machine.

  • When you type in 'ps' on a command line interpreter, it will first look in the your search paths. There be multiple search paths; note that when you type 'echo $PATH' on a Linux machine, there may be several directories listed, separated by colon (':') characters. So the command line interpreter will look for an executable 'ps' in the following directories:
    • /usr/local/bin
    • /usr/bin
    • /bin
  • The command line interpreter finds an executable file '/bin/ps' which it then runs.
    • What if there is an executable file 'ps' in more than one of those directories? The command line interpreter executes the first one it finds. To know which 'ps' would be executed by the command line interpreter, type in 'which ps'.
  • The ps executable accesses system resources to perform its task – displaying a list of processes.

Your command line interpreter will have its own hard-coded search path, which will contain your executable commands, written in Perl. There will be extra marks if I can add my own executable file to the search path – written in any language – and be able to run it from your command line interpreter.

What to Submit

You should submit an archived (zipped) file containing a folder full of your executable commands, the script which serves as your command line interpreter (if you wrote one) and a 'readme.txt' file explaining your scripts. If you submit a command line interpreter, the 'readme.txt' file should explain how to change the search path so we can test your command line interpreter.

You will be marked first on your executable commands; be sure to hand in at least the four command scripts for a passing grade. Focus on writing the individual scripts. You've seen everything you'll need for them in the tutorials.

The command line script will be more challenging. You should try to write it; you may not finish it but marks may still be given for incomplete command line interpreter scripts if you include an explanation in your 'readme.txt' of what challenges prevented you from writing one.

Comment your code well, or you will be marked poorly.

What were you marked on?

Here are some more relevant points we'll be looking for in your assignment #1 submissions, before we give bonus points:

  1. How well/clearly did you comment your code? (and is it also well indented?)
  2. Did you include a readme.txt file?
  3. The DIR script…
    1. Does the script work, listing files in a directory?
    2. Does it accept the right arguments? (one: a globbing pattern)
  4. The FIND script…
    1. Does the script work, searching the contents of a file for a substring?
    2. Does it accept the right arguments (…in the right order)? (two: a substring and – optionally – a filename)
    3. Does it accept piped input?
    4. Does it search for a substring or does it match a RegEx? (the Ms-Dos tool searches for a substring, so if search for \w it looks literally for \w, not word-characters)
  5. The TYPE script…
    1. Does the script work, displaying the contents of a file?
    2. Does it accept the right arguments? (one: a filename)
    3. Does it accept piped input?
  6. The CLS script…
    1. How clear (no pun) is your solution? Were you to not comment it, would a developer still be able to understand it?
  7. Do your scripts give relevant error messages when something goes wrong, such as "File not found"? Extra marks if they deliberately match those given by the corresponding Ms-Dos tool.
  8. If you tried to use Perl modules or functions we did not cover in tutorial, did you either document why or at least use them properly?
  9. Is there reasonable doubt that you didn't just copy-paste this code from the tutorial web site?
  10. Overall, does your code demonstrate a basic understanding of Perl?
Content © 2008-2012 Richard Jean-Paul Le Guen