Getting Started with UNIX and CourseMarker

This document is modified from corresponding lab manual of cs1010. If you have gone through cs1010 already, you may find that most materials here are familiar to you ...

0. Overview

The only way to attain proficiency in programming is to practise, and practise must you. This includes doing the programming exercises, trying out and experimenting with the programs presented in the lectures as well as the discussion sessions. It is not good enough just to read others' Java programs. You must type it in, compile it, and run it yourself! Your programming aptitude can only be improved with programming experience. As you will be using a UNIX platform for Java programming, we have provided here a step-by-step walkthrough which will guide you through the following:

  1. Using the PCs in the programming lab
  2. Connecting to SoC's sunfire UNIX server
  3. Introduction to basic UNIX commands
  4. Developing Java programs in UNIX
  5. Submitting Your Programs Using CourseMarker

 

1. Using the PCs in the programming lab

You should be greeted with a Windows screen if a PC is up (if the PC is off, switch it on first). As instructed on the screen, press Ctrl-Alt-Delete.

A login screen will appear. You are to type in your NUSNET user-id and password, and select the NUSSTU domain. For students registered this year, your NUSNET user-id begins with a00 followed by 5 digits (example: a0012345, note that it is a small letter a). This is different from your matriculation number which has a check character at the end (example: A0012345X).

Note: All NUS students are issued with an NUSNET account by the Computer Centre. The domain name of this account is "@nus.edu.sg". All SoC students, as well as students from other faculties taking SoC modules, are also issued with an SoC UNIX account (see below). The domain name of the SoC account is "@comp.nus.edu.sg". This SoC UNIX account will be purged at the end of the semester for non-SoC students. Do not mix up your NUSNET account and SoC UNIX account; each has its own user-id, password and mailbox.

 

2. Connecting to sunfire

In class, you will need to login to sunfire (our UNIX server). If you are accessing sunfire for the first time, you will need to first create (or re-enable) your SoC UNIX account. Using a web browser, go to https://mysoc.nus.edu.sg/~newacct and follow the instructions to create your UNIX Account (use a maximum of 8 characters in your password or you may encounter problems). After obtaining a UNIX account, you can then login to sunfire using your UNIX user-id and password. Create your UNIX account as soon as you can, preferably before your first lecture.

2.1 Logging into sunfire using SSH Secure Shell (for Windows systems)

In class, you will use the SSH Secure Shell software to login to sunfire. The PCs in the programming labs comes with SSH Secure Shell installed. To install SSH Secure Shell in your own Windows PC/notebook, you can download SSHSecureShellClient-3.2.9.exe from the CS2010 IVLE workbin.

To launch SSH Secure Shell, look for its icon on the desktop and double click on it. (Alternatively, left mouse click on the Start button at the bottom-left corner, select All Programs, look for SSH Secure Shell folder, and left click on Secure Shell Client.) A screen similar to the one shown below will appear. Click on Quick Connect button and a small window Connect to Remote Host will pop up.

Under Host Name, enter sunfire.comp.nus.edu.sg

Under User Name, enter your SoC UNIX user-id. Note that UNIX is case-sensitive (therefore as an example, you must type "happytan" and not "HappyTan").

Ensure that the Port Number is 22 and then click on the Connect button. 

If you encounter the following window, just click "Yes".

When this is done, another small window will appear to prompt you for your password. Note that the user-id and password are case-sensitive (i.e. 'a' is different from 'A'). The password will appear on the screen as asterisks (******) so you need to type slowly and carefully. Once you have logged into sunfire, you are automatically placed in your home directory.

2.2 Logging into sunfire from your local UNIX system

You can also login to sunfire from the UNIX platform of your local machine. Assuming that your user-id in sunfire is happytan, enter the command (in bold) at the local UNIX prompt.

localuser@localmachine:~[xxx]$ ssh happytan@sunfire.comp.nus.edu.sg

When you are prompted for "Password", type your password slowly and carefully, then press the <ENTER> key. The password is case-sensitive and for security reasons, you will not get to see what you have typed. Once connected you will see the following. Notice that the UNIX prompt has changed to "happytan@sunfire0:~[xxx]$" indicating a successful login. You are now at the home directory of your UNIX account in sunfire.

2.3 UNIX passwords

There are guidelines to choosing good passwords which are safer and less susceptible to break-ins. When you change your password, the system will check that your new password satisfies its stringent requirements, failing which it will reject the change. Generally, the system tends to accept passwords that consist of at least a mix of uppercase and lowercase characters, plus some digits. (Refer to this page Choosing a Good Password to find out more after this lab.) You can change your password any time you like -- in fact, you are advised to change it regularly.

Always remember your password! If you forget, you would need to go to I Forgot My SoC Password page to change your password online (this requires your NUSNET user-id and password).

2.4 Setting up your sunfire account

For this course, you will also need to configure your sunfire account. This step must be correctly done or else you will NOT be able to submit your programs for take-home labs!

After connecting to sunfire using your UNIX user-id and password, type the following commands (you should do this only ONCE):

    ~cs1020/lab0/setup

Check that you enter the above command ACCURATELY (the tilde character "~" is usually found on the top left of your keyboard on the left of the "1" key).

This command initializes your account and prepares it for this introductory lab session. It copies some setting files into your home directory (refer to Appendix A for a list of these files and their descriptions).

After the setup program runs successfully, you must type:

    source  .bash_profile

This sets up useful commands for your program submission later. Remember to type the SPACE in between "source" and ".bash_profile". Note that if the above command ran successfully, you will see no output before the next prompt appears. With many UNIX commands, "no news is good news". This is very different from Windows or Mac where you are used to seeing a dialog box pop out. Try to get used to UNIX quickly.

2.5 Before you proceed further...

 

3. Basic UNIX commands

Whether you are using UNIX on your local machine or logging into sunfire, now is the time to start your UNIX adventure. The power of UNIX stems from the many commands it offers. The following are a few commonly used commands. This list is by no means exhaustive and you are urged to explore on your own. Note that UNIX commands are case-sensitive.

In the examples below, bold words are commands which you are expected to enter. All commands are to be entered after the UNIX (local or sunfire) prompt of the form

user@machine:~[xxx]$

~ indicates that you are currently in your home directory. The following examples assumes that user happytan is logged into sunfire; however you can do it on your local UNIX platform too.

a. Directory commands

pwd to Print current Working Directory to show you which directory you are currently in
 
happytan@sunfire0:~[xxx]$ pwd
/home/h/happytan
ls to LiSt files in your current directory
 
happytan@sunfire0:~[xxx]$ ls
c
  You may also use "ls -F" for more information (-F is one of the many options/flags available for the ls command. To see a complete list of the options, refer to the man pages, ie. "man ls".)
 
happytan@sunfire0:~[xxx]$ ls -F
c/
  The slash (/) beside the filename tells you that the file is a directory (folder). A normal file does not have a slash (/) beside its name when "ls -F" is used.
  You may also use the "ls -l" command (hyphen el, not hyphen one) to display almost all the file information, include the size of the file and the date of modification. Try it now!
cd to Change Directory from current directory to another
 
happytan@sunfire0:~[xxx]$ cd somefolder
happytan@sunfire0:~/c[xxx]$ ls -F
Example1.java     Example2.java     Example3.java
  Note that the prompt changes to ~/c to indicate that you are now in the c directory below your HOME directory.
Entering "cd" alone brings you back to your HOME directory, ie. the directory in which you started with when you first logged into the system.
 
happytan@sunfire0:~/c[xxx]$ cd
happytan@sunfire0:~[xxx]$
mkdir to MaKe a subDIRectory in current directory
 
happytan@sunfire0:~[xxx]$ mkdir another
happytan@sunfire0:~[xxx]$ ls -F
another/   somefolder/
rmdir to ReMove a subDIRectory in current directory -- note that a directory must be empty before it can be removed.
 
happytan@sunfire0:~[xxx]$ rmdir another
happytan@sunfire0:~[xxx]$ ls -F
somefolder/

b. File commands

cp to CoPy files
 
happytan@sunfire0:~[xxx]$ cd somefolder
happytan@sunfire0:~/c[xxx]$ cp Example1.java Duplicate.java
happytan@sunfire0:~/c[xxx]$ ls
Duplicate.java    Example1.java     Example2.java     Example3.java
mv to MoVe files from one directory to another; can also be used to rename files.
 
happytan@sunfire0:~/c[xxx]$ mv Duplicate.java New.java
happytan@sunfire0:~/c[xxx]$ ls
Example1.java     Example2.java     Example3.java     New.java
rm to ReMove files. Be careful with this command -- files deleted cannot be restored (unless they have been backed up during the normal backup cycle).
 
happytan@sunfire0:~/c[xxx]$ rm New.java
rm: remove 'New.java'? y
happytan@sunfire0:~/c[xxx]$ ls
Example1.java     Example2.java     Example3.java 

c. Command to display text files

cat to string together or display (CATenate) the contents of files onto the screen
 
happytan@sunfire0:~/c[xxx]$ cat Example1.java
less variant of "cat" (includes features to read each page leisurely)
 
happytan@sunfire0:~/c[xxx]$ less Example1.java
  In "less", use <space> to move down one page, 'b' to move Back up one page, and 'q' to Quit from "less".


An online help facility is available in UNIX via the "man" command ("man" stands for MANual). To look for more information about any UNIX command, for example, "ls", type "man ls". Type "man man" and refer to Man Pages to find out more about the facility. To exit "man", press "q".

 

4. Developing a Java Program

The process of creating a working Java program involves the following steps:

        +---------+
+---+-->|  EDIT   |
|   ^   +---------+
|   |        |
|   |        |
|   |        v
|   |   +---------+
|   +---| COMPILE |
|       +---------+
|            |
|            |
|            v
|       +---------+
+-------|   RUN   |
        +---------+
  1. Using an editor, type in the source code (filename must have extension .java, eg: Example1.java)
  1. Compile your program to obtain the executable file. If there are compilation errors, identify them and re-edit the source code before you proceed.
  1. Run your executable file by typing the name of the executable file.
    If there are run-time errors, you must identify them and re-edit the source code, and compile again.

4.1 Create your first Java program "HelloWorld.java"

You need an editor to write your programs. In this course, you shall be using Vim -- a powerful editor with many commands, but even with the knowledge of a few simple commands it is quite easy to use and very powerful.

In your UNIX command-line, enter "vim HelloWorld.java":

happytan@sunfire0:~/c[xxx]$ vim HelloWorld.java

then press the "i" key. You will see that the words "-- INSERT --" appear on the bottom left corner of your screen. You are now in INSERT mode.

Type in the following program:

1 /************************************************************************
2 * Compilation: javac HelloWorld.java
3 * Execution: java HelloWorld
4 *
5 * Prints "Hello, World". By tradition, this is everyone's first program.
6 *
7 * % java HelloWorld
8 * Hello, World
9 *
10 * These 17 lines of text are comments. They are not part of the program;
11 * they serve to remind us about its properties. The first two lines tell
12 * us what to type to compile and test the program. The next line describes
13 * the purpose of the program. The next few lines give a sample execution
14 * of the program and the resulting output. We will always include such 
15 * lines in our programs and encourage you to do the same.
16 *
17 *************************************************************************/
18 
19 public class HelloWorld {
20 
21     public static void main(String[] args) {
22         System.out.println("Hello, World");
23     }
24 
25 }

While you are in INSERT mode, you may use the arrow keys (Up, Down, Left, Right) to move around your program, as well as the Backspace key and Delete key to delete text. The PageUp and PageDown keys do not work, so do not use them. Do not use the scroll bar as it does not always work as expected.

Notice that line numbers (1, 2, 3, etc.) are displayed on the left. This helps you to easily identify a number by its line. Line numbers are NOT part of Java code. This is extremely useful when identifying the location of compilation errors.

When you are done, press "<ESC>:x<ENTER>" (more clearly seen as pressing the following four keys one after another: <ESC> : x <ENTER>) to save your program and exit from the Vim editor.

If you would like to learn more about Vim commands, refer to Vim Keys.

4.2 Compile and run your program

Compile and run the program as follows:

happytan@sunfire0:~/c[xxx]$ javac HelloWorld.java

(this will compile "HelloWorld.java" and produce the bytecode file "HelloWorld.class" if compilation is successful).

There will be error messages if your program has errors. Go to Step 4.1 to make the necessary corrections and re-compile it. If there is no compilation error message, proceed with program execution as follows:

happytan@sunfire0:~/c[xxx]$ java HelloWorld

Then you will see the output "Hello, World" be printed on the screen.

 

5. Submitting Your Programs Using CourseMarker

We use the CourseMarker system for two very specific purposes:

The following is a walkthrough for your CS1020 lab #0. Before you start, make sure you have the following:

Enter "cs1020CM" at the UNIX prompt. You should see the following screen.

CourseMarker password problems

If you did not receive your CourseMarker password in your NUSNET email account or you suspect your password is wrong, then you can create a new password https://mysoc.nus.edu.sg/~cmarker/get-password.cgi.

If you see the login prompt of CourseMarker above, then you have been successful thus far. The CourseMarker system will prompt you for the following

  1. username: Enter your CourseMarker id.
  2. full name: Enter your full name. This only appears the first time you use CourseMarker. You can type in a nickname if you wish.
  3. Password: Enter your CourseMarker password.

Note that both username and password are case-sensitive. Having verified your username and password, CourseMarker will display a short welcome message.

You have now got access into CourseMarker. Before looking at the different CourseMarker commands, let's have a high level view of the system. The CourseMarker system is organized as a hierarchy of the following levels:

5.1 Retrieving questions from CourseMarker

As shown in the preceding screen shot, a successful login to Coursemarker starts you off at the system level. At each level, the valid commands are listed in the [] brackets. For example, at the system level, the valid commands are "lc" and "vm". We only cover those commands that are useful for your lab submission; you can ignore those not mentioned in this guide.

Enter "lc" (short for List Courses), which shows all the courses you have under CourseMarker system. In our case, there will be only one course "cs1020 : no title". To navigate to the course level, enter "cs1020". You will be presented with another set of commands [ vm csum lu up ]. The "up" command moves you back one level (back to the system level in this case). The "lu" command lists the available units (labs).

Enter "lu" and you should see one unit listed as "lab00 : no title". Enter the unit name "lab00" to get to the unit level. At unit level, there are four commands [ lx vn usum up ]. "up" moves you back to the course level and "lx" lists the available exercises for this lab.

Enter "lx" and you should see "ex1 : Volume of a Box". Enter "ex1" to reach the exercise level.

The exercise level is the most important level, so spend some time understanding the various commands. Initially, you will only see three commands: [ set vq up ]. "set" downloads the relevant files (question, skeleton program files, etc) from the system to your directory. "vq" (view question) displays the lab problem, and "up" transports you back to the unit level.

Enter "set" to download the necessary files. If the command is successful, you will see quite a few more commands at the prompt.

The "set" command overwrites any existing file in the exercise directory. Use this command once per exercise. If you use the "set" command after having written some code, your work may be overwritten!

The additional commands do not concern us at the moment. Enter "quit" to exit CourseMarker and return to the UNIX prompt.

5.2 Working on the lab problem

The various files downloaded from CourseMarker are stored under the following directory:

~/CMhome/cs1020/studentArea/yourCourseMarkerId/cs1020labXXexX

where yourCourseMarkerId refers to your personal CourseMarker ID. The last directory "cs1020labXXexX" is simply the course name, unit name and exercise name joined together. Navigate to this directory using the "cd" command. If you are not sure how to do it, here's one example:

happytan@sunfire0:~/c[xxx]$ cd ~/CMhome/cs1020/studentArea/yourCourseMarkerId/cs1020lab00ex1

Enter "ls" to view the downloaded files. Note the following files:

5.3 Submitting your work

To submit, start up CourseMarker by issuing the command "cs1020CM" again. Note that the CourseMarker system automatically resumes from where you left off. If you followed closely to the steps given above, you should find yourself in the "ex1" exercise level as shown below.

If you are not at this level, just use the appropriate commands to navigate to the correct level. You will see more commands available now, in particular, the "submit" command. This command is enabled automatically as soon as CourseMarker detects the executable for the particular exercise. If this command is missing from the choices, then the most likely reason is that you failed to compile the program.

Type "submit" to submit your work. If the submission is successful (may take some time depending on the system load, so please be patient), CourseMarker will give you an assessment of the submission. The result is too long to fit into one screen, so use the Window scroll bar to view the full report. The full report looks something like this (only partially shown):

(L1)===========================
Description:General Grade
==============================
   (L2)===========================
   Description:Compilation Tool
   ==============================
      (L3)===========================
      Description:Search for errors in the compilation
      ==============================
      (L3)===========================
      Description:Search for warnings in the compilation
      ==============================
   (L2)===========================
   Description:Typographic Tool
   ==============================
      (L3)===========================
      Description:Avg. chars per line
      ==============================
      (L3)===========================
      Description:Avg. spaces per line
      ==============================
      (L3)===========================
      Description:% of blank lines
      ==============================
   (L2)===========================
   Description:ALL Dynamic Tests
   ==============================
      (L3)===========================
      Description:Test 1 -
      ==============================
         (L4)===========================
         Description:Test #1a
         ==============================
      (L3)===========================
      Description:Test 2 -
      ==============================
         (L4)===========================
         Description:Test #2a
         ==============================
            // ... Similarly for Test 3 and 4 ... ... //
   (L2)===========================
   Description:Feature Tool
   ==============================

Among all the testing features of CourseMarker, please only focus on All Dynamic Tests (highlighted above) and ignore the rest. This section will tell you whether your program produces correct output or not. If some tests fails, that means your program contains either syntax or logic error and you need to go back and double check it.


Appendix A: Pre-loaded files

These are the files copied into your home directory by the setup program.
In your home directory:

  1. .bash_profile: Personal initialization file for login shells (sunfire only) (You may also download this file here.)
  2. .bashrc: Personal per-interactive-shell startup file (for bash) (You may also download this file here.)
  3. .vimrc: Vim configuration file (You may also download this file here.)

Notes: Filenames that begin with a dot (.) are 'hidden files' and are not shown with the "ls" command. Use "ls -a" to see them.


Appendix B: Transferring files

There are two ways to transfer files between your UNIX account on your local machine and your sunfire UNIX account.

Using "scp"

From the UNIX platform on your local machine, issue the command "scp <source> <destination>" in a similar way as the copy "cp" command, except that you need to also specify the user-id in sunfire.

For example to copy the file "Example1.java" from the local "java" directory to the home directory in sunfire, enter the command from the local UNIX prompt.

localuser@localmachine:~[xxx]$ scp ~/java/Example1.java happytan@sunfire.comp.nus.edu.sg:~
Note that the source is "~/java/Example1.java" and the destination is "happytan@sunfire.comp.nus.edu.sg:~".

Using secure file transfer in SSH Secure Shell

If you have downloaded and installed SSH Secure Shell on your local machine, perform the usual login to sunfire, then open the File Transfer program by clicking "Window -> New File Transfer". You will see the contents of your local machine on the left window panel, and the sunfire home directory on the right window panel.

You can navigate around both panels to copy/delete files. To transfer a file from sunfire to your local machine, right-click on the file you wish to transfer, and then choose "Download". To transfer a file from your local machine to sunfire, right-click on the file you wish to transfer, and select "Upload". Be sure to change to the correct folder/directory at the destination before performing the transfer.



Last update: 6 Jan 2011