CPS 100, Fall 2000, Unix/Emacs Intro

This introduction will introduce you to some basic Unix commands including:

This section comes in several parts. You will login, reconfigure certain files to facilitate using libraries in conjunction with this course, practice some UNIX commands, and write a simple C++ program. This document is only a brief introduction to many tools. You will need to spend some time on your own experimenting further to become comfortable with the tools. You should consult the OIT online help page for more information.

In this document, commands that you type will be in a font that looks like this, sometimes commands may be quoted --- "like this" --- do not type the quotes, just type what is between them. When instructed to type something, press the return key after typing it.

Logging in

In this course we will be using Unix workstations or PCs with X windows and logging into the campus unix workstations. You will need to know your campus login and password. If you don't know it, then you may need to go to the OIT Help desk in 101 North Building, although engineering students may need to check with the engineering school office or the dean's office.)  To log in:
  1. Workstations are always on, do NOT turn them off. Wiggle the mouse if the screen is blank.

  2. If you're using a PC in a cluster you will first need to start X windows by either clicking on the X icon, OR clicking on Start at the bottom left corner, then clicking on Programs, and then clicking on X-Win32.

    You should see a line at the bottom of your screen that says "X Win32". Not much else happens yet.

  3. From a PC you need to open a connection to a unix machine. We will be using "ssh telnet", also called TeraTerm Pro. Click on the ssh icon, OR click on Start at the bottom left corner, then click on Programs, and then click on "Tera Term Pro".

    You will see one of the following machines to login to (or if none appears, enter one of these)

        godzilla.acpub.duke.edu
        kiosk.acpub.duke.edu
    

    (Hint: Use tabs to move between fields). Type your duke login-id, then a tab, then type your password (your password is not displayed).

  4. Note: passwords are case sensitive, Banana is different from banana.

  5. A unix xterm window should be displayed and a message indicated that you have successfully logged on.

Getting Started with UNIX

An operating system is a large program that coordinates all the resources for the execution of programs. UNIX is the operating system on the Sun workstations. UNIX contains lots of commands, a few important ones are introduced here. Common commands are are summarized on an OIT web page.

To enter a command, type after the prompt in one of the xterm windows. You should see a prompt that may look similar to

    [1] rodger@godzilla2%
except rodger should be replaced by your login-id and the number and machine name may be different. If you make a mistake when typing a UNIX command, use the delete key or the arrow keys to correct it.

If this is a new account, you should change your password to something that is easier to remember, but not easy for someone to guess. Do not use your name, or an English word, but rather compose a password formed with numbers, upper and lower case letters, and punctuation marks. To change your password, type the UNIX command  passwd and follow the directions it prints.

Next, from in the xterm window, at the prompt type ~ola/bin/setup cps100. This runs a program that will set up some files in your directory to make it easier to use the programming tools we provide---it does this by copying some dot files (like .cshrc, see below from  ~ola/newuser). Additionally, it creates a cps100 directory in your account (if you don't already have one) and sets the permissions properly as called for in assignment zero.

Any files changed by this program are saved with a .old  extension, e.g., .cshrc.old.  If you do not get a prompt back after typing the command, consult a TA.  If you do not want to run this program because you have made your own changes to your accounts setup, you will need to look at the setup program to determine which parts of it you want to setup. Some are needed for accessing and submitting files. PLEASE CONSULT A TA if you are not sure.

In order for changes made by the setup command to take effect, you will need to logout from the unix window and then log back in. To logout of your unix window, type logout . Now, please log out, then log back in.

Creating and Traversing Directories

To do the work for the remainder of this class, you will need to create a directory (i.e.,  folder) to contain the files for your program.  In this course, we will follow a convention of creating a separate directory for each assignment in your cps100 directory.

When you login, your session begins in your main directory. To see the name of your directory, type the UNIX command pwd (print working directory).  The complete name it prints in your terminal window should look similar to /afs/acpub.duke.edu/users/r/o/rodger except your login-id should be the rightmost word.

  1. To list your files and directories, type ls (think of this as shorthand "list"). You should see the directory you just created listed.
  2. You can cd (change directory) into your cps100 subdirectory by typing cd cps100. Typing pwd should show you in the directory you just changed into. You might now make, for example, a directory called yahtzee by typing mkdir yahtzee. Then change into this by typing cd yahtzee.
You are now ready to start working on a C++ program.  The command to change directories, cd, lets you change directories relative to your current directory or absolutely to a specific directory.  For example you can type cd ~ola to change into Professor Astrachan's directory. Type ls to see the files and directories contained there.

At any time, you can type cd (with no arguments) to return to your home directory. Instructions for editing, compiling, and printing are given below. To work on an assignment you'll normally copy one or more files into a directory in which you'll do work for the assignment. The copy command is cp so from within your yahtzee directory (for example) type (do not forget the trailing dot: "."):

cp ~rodger/cps100/yahtzee/* .

Type ls to see that the files were copied. You should see a new file called Makefile. We will use the Makefile to compile programs in this course.  This Makefile will automatically link to libraries that will be used in this course. You can use it without modification for any C++ program you write as long as the complete program is in one file. If you write a program in multiple files, you will need to modify the Makefile slightly to tell it which files to combine together to make your program.

Test Program: Edit, Compile, Debug

You will edit, compile, and run a C++ program in this section. This first program is very simple, but will provide some practice with editing, compiling, and debugging. Using the editing commands described below, you should type in the program shown.  Be sure to use both names in your group and lecture section number. // Authors: Joe // Date: 8/30/00 // Course: CPS 100 // Purpose: Practice C++ #include <iostream> using namespace std; int main() { cout << "Plant a new Truffula. Treat it with care." << endl; cout << "Give it clean water. And feed it fresh air." << endl; cout << endl; cout << "....The Lorax, by Dr. Seuss" << endl; return 0; }

Starting the editor xemacs

To type in your program you need to use an editor. To use the editor xemacs to create a new file for your program type the command
 xemacs  & 

An xemacs window should appear (please be patient!). You can use the arrow keys and the mouse to move around in the xemacs window.

Loading, Editing, Saving Files

In general, you should always have an xemacs window open. Unless you are logging off, there is seldom a reason to quit xemacs. You can, however iconify (make small) the window by clicking the mouse on the dot-button in the upper right of the window. This works for any window. Double-clicking on an icon opens that application.

Until you save your file, it is stored in a temporary buffer (the region in xemacs in which you are typing). When you believe the C++ program is correct, save it by clicking the File button at the top of the xemacs window, and then selecting Save Buffer. A message that the buffer has been written should appear in the minibuffer.  You should save your program often to ensure none of your changes are lost inadvertently.  You will not be granted any leniency if you lose your work because you did not save it properly. You can type cntrl-x cntrl-s to save instead of using the mouse.

Compiling a C++ program

A compiler transforms your C++ code into machine readable code the computer can understand. Programs can be compiled from within xemacs as well.  To compile from within xemacs:
  1. Use the Compile option under the Tools menu.
  2. The command make -k, which appears in the minibuffer, is the default compile command. You should compile by editing this command so that it reads make -k lorax, then hit return. It may take a while for the program to compile, but you should see an xemacs sub-window with an indication that something is happening.
  3. If there is a syntax error in the program, a message will appear in the *compilation* buffer. To get to the error you can click on the error with the middle mouse button. This will take your cursor to the line on which the error appears, so you can use it to cycle through multiple errors, fix them, and then recompile.
If there are multiple error messages, fix the first one only and then recompile. Sometimes one error causes the compiler to list additional errors that are not really errors.
An example of an error message is:

lorax.cpp:5: unterminated string or character constant lorax.cpp:5: possible real start of unterminated constant

This message tells you the name of the file (lorax.cpp), the line number the error occurred near (line 5) and a guess as to what is wrong. You do not need to worry about where line 5 is since clicking on the error message takes you there.

If you do not have any errors with the first compile, you might want to modify the program so that there is an error. For example, remove a semi-colon and then compile the program.
When the program has been successfully compiled continue with the next section.

Execute your program

Compiling your program creates a new file with the same name as your source file (in this case lorax.cpp), but without the .cpp extension.  This new file is executable, meaning it can now be used as a UNIX command. To conserve the limited space in your account, you may want to get into the habit of removing the executable program (which is really big) when you are finished with the assignment (since you can always re-create it by recompiling the source code).  To do this, type rm lorax to remove the executable.
Owen L. Astrachan
Last modified: Tue Aug 29 22:10:26 EDT 2000