A Brief Introduction to Unix

You will be using a Unix operating system. The version of Unix you will be using is developed by Sun.


 

The Unix Command Shell

You will be using the bash command shell. It is a command line interface, similar to DOS and VMS’s. In fact, many of the commands are the same. For example, cd means to change directory in DOS and bash. A brief list of frequently used commands follows (bash is case sensitive):

CC <name> Compile the indicated file using the C++ compiler. See more about this command in the section on compiling below.
cd <name> Change to the directory indicated. cd .. moves up to the parent directory, as in DOS.
cp <name1> <name2> Copy the indicated file to the indicated directory or file.
emacs <name> Edit the indicated file with the world's most powerful text editor.
pico <name> Edit the indicated file with an easy to use text editor.
logout Log out.
ls List files in the current directory.
man <name> Display a detailed description of the indicated command. Type q to quit displaying.
less <name> Display the contents of the indicated text file. Type q to quit displaying.
mv <name1> <name2> Move indicated file to the indicated directory or file, i.e., rename the file.
rm <name> Delete the file indicated.

The man command is particularly useful for finding out more information on a command.
 

The Emacs Editor

I recommend you use the emacs text editor for creating and editing your programs. Using

Here a few of the most essential commands in the emacs editor. C- means hold the control key down while typing the following key. M- means type the escape key first, then the following key.
C-h  t Run the tutorial about emacs.
C-x C-s Save the current file with the current name.
C-x C-c Exit the editor. You will be prompted to save, if necessary.
C-x C-f Find and edit another file.
C-x  i Insert file at current cursor location
C-x  b Switch to another buffer.
C-x C-b List buffers
C-v Page down.
M-v Page up.
C-f Forward one character
C-b Backward one character
C-n Forward to next line
C-p Back to previous line
C-a Beginning of line
C-e End of line
DEL Delete preceding character
C-k Kill line to cut buffer
M-@ Mark point as beginning of region
C-w Kill region to cut buffer
M-w Copy region to cut buffer
C-y Yank cut buffer back into document, i.e., paste
C-s Search forward
C-r Search backward
 

Emacs is an enormous editor. Real emacs afficiandos spend their whole work week inside emacs. They use it to read and send mail, to compile their programs, and generally treat it as their user interface shell.

 

Compiling C++ Code

The command for compiling a number of files of C++ code with the C++ compiler is

Assuming your code is syntactically correct, the compiler will produce an executable file named:

To execute this file, just type

on the command line. If you wish a different file name, just rename it with the mv command or use a long form of the CC command:

CC -o <executable file name> <source file name>

Debugging

You will find that there are times when it would help to find a problem in your code by watching the code execute. There is a debugger program that makes it possible to examine your code as it executes. In order to see the function and variable names that you use in your code, you first need to ask the compiler to insert those names in the executable code with the following variation on the compile command.

CC -g <source file name>

Then start the debugger with the following command.

dbx a.out

You will see the debugger prompt: (...dbx). You may then use the commands in the following table (and a lot more besides, which you can learn about on-line with the man command.)

stop in <name> Stop, whenever call to function <name> occurs.
stop at <line_no> Stop, whenever get to line number <line_no>.
where shows current file, function, line#
run Run the program, until the first stop point.
cont Resume running the program, until the next stop point.
next Execute the next instruction.
step Step inside the code for a function call.
list
list +20
Show source code at current position.
Show next 20 lines of source.
up Look at the context in which the current function was called.
down Go back down to the current context.
print <name> Print the value of the variable with the name <name>.
If you just see an address, try print *<name>.
display <name> Show value of the variable at EVERY break
exit Quit the debugger.