uWaterloo Logo

Unix for CS 241 — The Bare Essentials

CS 241 — Foundations of Sequential Programs


Introduction

All CS 241 assignments use the Unix computers of the CSCF Student Computing Environment. These are accessed directly from the CSCF terminal rooms on the 3rd floor of the Math & Computer Building (MC).

In Unix, there are many ways to do everything. This document describes one way to do each of the things you need to do the first couple of assignments. Alternatives are mentioned in italics.

Logging in and accessing the command line

Use your WatIAM/Quest user id and password to log in to the lab computers. (If you can't log in, and you're sure that you are using the correct credentials, you should speak with a consultant in the MFCF Help Centre in MC 3017 about your account.) Once you have logged in, you will be presented with a graphical user interface. You will need to access the command line interface to do CS 241 course work.

If you are using a Mac, you can run either "X11" or "Terminal". On Linux machines, look under "Applications" for a program called "Console" or "Terminal" or something similar. On Solaris machines, there will be a number of command line windows or "xterms" already open.

To execute a Unix command in a terminal (command-line window), first select the terminal you want to run the command in. On Solaris, position the mouse cursor over the appropriate window. On Linux, click on the appropriate window. Then simply type the command name and press Enter.

Connecting to a Unix server with SSH

The Unix-based systems in the labs will allow you to access the files on your student account and run various standard Unix commands, but they do not necessarily have all the software you will need to complete your assignments. To do course work, you will need to connect to another system with the SSH (Secure SHell) protocol.

There are a number of hosts available in the student computing environment (see http://www.cs.uwaterloo.ca/cscf/student/hosts), but it is recommended that you use one of the linux.student.cs.uwaterloo.ca computers. Other systems may have different configurations and outdated software, and we cannot guarantee that the CS 241 course tools will work properly on those systems. If you are having issues with any of the programs and tools mentioned in lectures or assignment specifications, make sure you are running them from a linux.student.cs.uwaterloo.ca host before asking for help.

To connect to a host, execute the following command in a command line window:

  ssh -Y linux.student.cs.uwaterloo.ca
(The "-Y" part is optional. Including it enables "X11 forwarding", which allows you to start graphical windowed programs from the terminal.)

You will be asked for a username and password; enter your WatIAM/Quest credentials. Note that unlike many password prompts, the prompt from the ssh command will not display "*" characters as you type each character of your password, so it might seem like it's not receiving your keyboard input. However, it is receiving input, so if you just type your password correctly and hit Enter then you should be able to log in. Then you should see a command prompt that looks something like this:

  linux028:~>
Where "linux028" is the name of the machine you are connected to. You can use this command prompt to do work on the remote machine just as you would with a local command line window. The rest of this document will explain some of the basic commands and tools you will need to know to complete CS 241 assignments.

Note: SSH can also be used to access the Unix hosts from any computer with an internet connection. Thus, if you have an SSH client on your home computer, you can use SSH to do your course work from home. Any Unix-based computer (including Macs) will probably already have the ssh command available, so you just need to open the command line and execute ssh -Y linux.student.cs.uwaterloo.ca as above. Windows installations normally do not come with an SSH client, so you will have to install one yourself. PuTTY is a commonly used free SSH client, and can be downloaded from http://www.chiark.greenend.org.uk/~sgtatham/putty/. Another option is to install Cygwin, a Unix-like environment for Windows that has the ssh command available.

Unix command line basics

Note: In this document what you type is shown in bold typewriter font and what the computer generates is shown in italic typewriter font, but you won't see any difference on the screen. Let's try executing a simple Unix command. One such command is date which simply outputs the time and date:
   linux028:~> date
   Mon Sep 19 10:56:51 EDT 2011
   linux028:~>
You can redirect the standard output of a Unix command to a file using > (a 'greater than' sign) followed by a name for the file; for example,
   linux028:~> date > myfile
   linux028:~>
places the output of the date command in the file named myfile. The cat command shows the contents of one or more files. The output of cat, like any other command, may be redirected. The three commands below show the contents of myfile, copy the contents of myfile to yourfile, and show the contents of both myfile and yourfile concatenated together (hence the name cat).


   linux028:~> cat myfile

   Mon Sep 19 10:56:51 EDT 2011
   linux028:~> cat myfile > yourfile
   linux028:~> cat myfile yourfile
   Mon Sep 19 10:56:51 EDT 2011
   Mon Sep 19 10:56:51 EDT 2011

   linux028:~>
cat, like many commands, can take its input from the standard input. The first cat command below takes two lines of input from the keyboard and puts them in the file theirfile. Using the keyboard it is necessary to type the two lines and in addition to indicate the end of input by typing ^D (D with the control key held down). The second cat command shows the contents of the newly created file.
   linux028:~> cat > theirfile
   the quick brown fox jumps over the lazy dog
   now is the time for all good men to come to the aid of the party
   ^D
   linux028:~> cat theirfile
   the quick brown fox jumps over the lazy dog
   now is the time for all good men to come to the aid of the party

   linux028:~>
Standard input may also be redirected from a file using < (a 'less than' sign):
   linux028:~> cat < myfile
   Mon Sep 19 10:56:51 EDT 2011

   linux028:~>

Note: Be sure to use ^D to indicate the end of input, not ^Z as with Windows. If you use ^Z, you'll suspend the cat command and it will stay suspended until you use the fgcommand to start it up again. These facilities are examples of Unix job control.

Manipulating files and directories

ls lists the names of the files that you have created, in addition to some files automatically created by the system. rm removes (deletes) a file immediately and forever but there are periodic backups in case you delete your life's work. cp copies a file and mv moves (renames) a file.

   linux028:~> ls
   bin       cs241           myfile        theirfile     yourfile
   linux028:~> rm theirfile
   linux028:~> cp myfile zip
   linux028:~> mv zip zap
   linux028:~> ls
   bin       cs241           myfile        yourfile      zap

   linux028:~>

You will probably want to organize your files into different directories, instead of leaving everything in the default home directory. Each time you enroll in a CS course, a directory for that course is automatically created. You can use the cd command to change to your personal cs241 directory:

   linux028:~> cd cs241
   linux028:~/cs241>
This directory is a convenient place to store all the files you create for the course. You can organize things further by using the mkdir command to make new directories. For example, suppose you want to create a directory to store all the files for the first assignment:
   linux028:~/cs241> mkdir A1
   linux028:~/cs241> cd A1
   linux028:~/cs241/A1>
When you're finished working on Assignment 1, you can change back to your home directory. In Unix, .. refers to the directory "above" your current working directory. So simply type cd .. twice:
   linux028:~/cs241/A1> cd ..
   linux028:~/cs241> cd ..
   linux028:~>
Alternatively, ~ refers to the home directory. So from anywhere in the system, you can type cd ~ to return to your home directory.
   linux028:~/cs241/A1/P1/solutions/attempt279> cd ~
   linux028:~>

Copying files between computers

If you are working from your own computer (do remember to always use the CSCF Unix environments to create files and test your solutions!) you can easily copy files back and forth using the scp command. This should be done from your computer's Unix environment (on Windows, you will need to download an SCP client or install Cygwin). The format of the command is scp PathToFile PathToTargetDir. You can perform host-machine to server copying, server to host-machine copying, and server to server copying with the scp command. To specify a server name, use the format userid@host. To specify a file path within the host, use a colon to separate the userid@host from the file path.
   freds-computer:~> ls
   bin       cs241           myfile        yourfile      zap
   freds-computer:~> scp myfile fred@linux.student.cs.uwaterloo.ca:~/cs241/
   fred@student.cs.uwaterloo.ca's password:

   myfile                                    100%  185     0.2KB/s   00:00
   freds-computer:~> scp fred@linux.student.cs.uwaterloo.ca:~/cs241/myfile ./
   fred@student.cs.uwaterloo.ca's password:

   myfile                                    100%  185     0.2KB/s   00:00
In the first example, you are first copying myfile to Fred's CS account, fred@student.cs.uwaterloo.ca, and placing it in the directory ~/cs241/. In the second example, you are copying myfile from the directory ~/cs241/ on Fred's CS account, to the directory ./. (In Unix, . refers to your current working directory.)

Editors

You can create and edit files using evim. The command below starts the evim editor in a new window to edit the file myfile. You can type text into the evim window and there are icons and menus to do all the things you've become accustomed to like cut and paste, printing, and so on.
   linux028:~> evim myfile &

evim is a dumbed-down version of gvim. gvim a graphical version of the character-based editor vim. vim is an extended version of the standard Unix editor vi. vi has very powerful command and macro facilities, and is the prefered editor of many serious programmers. But those accustomed to Windows-based editors may find it weird. evim removes the weirdness at the expense of being incompatible. gvim is compatible and so requires you hit the i key before inserting text in the file. (If you start gvim and hit i it behaves pretty much the same way as evim until you hit esc. Then if you hit i again it behaves like evim again.)

evim and gvim are graphical programs that run in a separate window from the terminal. Thus, as explained in the SSH section, you will need to include the -Y flag when you run the ssh command to be able to use these windowed programs. If you are having trouble running them, you will have to use an editor that runs in the terminal such as vim, emacs or pico. pico is the simplest of the three to use, but also the least powerful.

Email, Newsgroups and the Web

You will need to read email, newsgroups and web pages for this course's work. You will also need to use the web in order to submit your assignments. mozilla does all of these things; to start mozilla:
   linux028:~> mozilla &

Like evim and gvim, mozilla runs in a window, so you will need to enable X11 forwarding by giving the -Y option to ssh. If you don't want to run windowed programs, you can use the terminal programs w3m to browse the web and alpine to read email and news. If you only need to use a web browser to submit assignments, the marmoset_submit script is much easier to use than w3m or mozilla.

To read mail and news, click the envelope icon near the bottom left of the Mozilla window. The first time you do this you'll have to answer some questions:

(If "mailservices.uwaterloo.ca" does not work, your email account may be on a different server. You will have to figure out the correct server information on your own. It is most likely somewhere on your department/faculty's website.)

Then you'll get a mail/news window. In this window, click your email address in the left panel. In the right panel, under Accounts click Create a new account. This time:

In the left panel of your news window click news.math and in the right panel click Manage newsgroup subscriptions. Find uw.cs.cs241 and any other newsgroups you want to read and click Subscribe then OK. Now you can read your mail by clicking Inbox on the left panel and you can read the newsgroup by clicking uw.cs.cs241 in the same panel.

If you already use another program to read email and news, feel free to continue. Make sure that you do receive and read mail addressed to your UW account! You can browse the web from home, too, but you might have trouble submitting your assignments from home if they are on a UW computer!

Course-Specific Tools

Special Unix commands are required for some of your assignments. Usage of the tools will be described with the assignments that require them. Before using these commands you must enable them by entering:
   linux028:~> source /u/cs241/setup

You must re-enter this command whenever you log out and log in again. You can automate this process by using evim to edit the file named .cshrc You need to insert this line immediately before the second-last endif in the file. If you screw up this file, bad things will happen.

Marmoset Automatic Grading System

All assignments will be submitted electronically by using a Web browser to access the Marmoset system. A link to Marmoset may be found on the course web page.

Alternatively, you can use the marmoset_submit command line tool to submit assignments. It is much easier and faster than doing it through a web browser. You need to run the source /u/cs241/setup command before marmoset_submit will be available. A more complete tutorial is here: http://www.student.cs.uwaterloo.ca/~cs241/marmoset_submit/

To use Marmoset, log in using your WatIAM/Quest user id and password. Marmoset displays a table of "Projects" -- each question in each assignment is treated as a separate project. Use the link in the column titled project to view the assignment specification; use the link in the column titled submissions to view the status and grade for any previous submissions you have made for this project; use the link in the column labeled web submission to submit a file for testing. When you click submit a page will open to allow you to type or select the name of your file containing your submission.

Your submission will immediately be run on a set of public tests; these results should be available quite quickly -- you must refresh the view screen to see the scores when they are available.

If your submission succeeds on the public tests you will be invited to submit for release testing; that is, for actual grading. You will be informed as to which of the release tests your program passes, but no details. If you fail some tests you should debug your submission and resubmit. There is no penalty for resubmission, but to encourage you to start early and to discourage you from stabbing in the dark you are restricted to three release tests per project per day.

Further Reading

CSCF Environment: http://www.cs.uwaterloo.ca/cscf/student/
CSCF Unix Hostnames: http://www.cs.uwaterloo.ca/cscf/student/hosts
A much larger tutorial: http://www.math.uwaterloo.ca/mfcf/service-groups/consultants/firstweek/
FAQ: http://webtest.mfcf.uwaterloo.ca/wiki/index.php/MFCF_FAQ

If you notice any errors in this document, report them to cs241@student.cs.uwaterloo.ca.