Introduction to Unix

This introduction will introduce you to some basic Unix commands including:
Copying, moving, deleting files
Traversing and moving between directories
Invoking programs including editors and compilers
Edit, compile, debug, and run a C++ program

This section comes in several parts. You will login, 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

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).

A unix command shell 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.

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.  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 AFSDocs subdirectory by typing cd AFSDocs. Typing pwd should show you in the directory you just changed into. Additionally, you can change into your cps6 directory by either using the cd command again, or going there directly by typing cd ~/AFSDocs/cps6. In this case, you are specifiying the complete path to your cps6 directory.
  3. You might now make, for example, a directory called lastday by typing mkdir lastday. Then change into this by typing cd lastday.
     
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. 

At any time, you can type cd (with no arguments) to return to your home directory. 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 lastday directory (for example) type (do not forget the trailing dot: "."):

cp ~rcduvall/AFSDocs/cps6/lastday/* .

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 &lt;&lt; &quot;Plant a new Truffula. Treat it with care.&quot; &lt;&lt; endl; cout &lt;&lt; &quot;Give it clean water. And feed it fresh air.&quot; &lt;&lt; endl; cout &lt;&lt; endl; cout &lt;&lt; &quot;....The Lorax, by Dr. Seuss&quot; &lt;&lt; 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

Use the file menu to load a file (even one that does not exist yet). Click and hold the menu and choose Open File before releasing the mouse button. (alternatively, you can use cntrl-x cntrl-f, that is hold down the control key and press x followed by f as a substitute for using the menu.)
 

The prompt for Find file: appears in the small minibuffer window at the bottom of the xemacs window. You should also see a path which might be ~/ or ~/groupcps6/aug29/. If you do not see this, consult with a TA because you may not be in the correct directory. You can edit what appears in the minibuffer with arrow/delete keys.

You will create a file named lorax.cpp. To do so complete the path in the minibuffer so that it reads ~/groupcps6/aug29/lorax.cpp and press return.
Enter the C++ program shown previously. Be careful that you type it just as it appears. You will notice sometimes after you type something, the cursor will "automagically" move to another position to indent properly. When you type a "}", the cursor always jumps back to the "{" for a second so you can see if the "}" matches with the correct "{".  These are features of the xemacs editor meant to make it easier for you to complete your programs.
If you make a mistake, use the arrow keys or mouse to move around and edit in the window.  You can use the mouse to position the cursor within xemacs by clicking in the window where you want to type.  You can also use the arrow keys and the scroll bar to move around.

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.
In an xterm window, type ls to see the files in you aug29 directory.
You should see both lorax.cpp and lorax. The file lorax is an executable program. You might also see a file called lorax.cpp~. This is a backup copy of your program that xemacs automatically creates. It is not an exact copy, but rather a copy before you made your most recent changes.
To run any executable program, type its name in your terminal window, so type lorax to execute this program.
You can type ls -l  to see how big the files are and when they were created.
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.