CPS 100, Fall 2000, Unix/Emacs Intro
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, 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:
-
Workstations are always on, do NOT turn them off. Wiggle the
mouse if the screen is blank.
- 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.
- 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).
Note: passwords are case sensitive, Banana is different from banana.
-
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.
-
To list your files and directories, type ls (think of this as
shorthand "list"). You should see the directory you just created listed.
- 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
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
-
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:
-
Use the Compile option under the Tools menu.
-
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.
-
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.
Owen L. Astrachan
Last modified: Tue Aug 29 22:10:26 EDT 2000