The main purpose of this lab is to introduce you to the computing environment of your laboratory. You will use the ideas in this lab again and again throughout this course, so you should make every effort to understand not only what, but why you are doing what you are doing at each step.
Before we can begin our session, your instructor must inform you how to begin a session with the computer at your particular institution. The way this is done differs from school to school, according to the kind of computer being used, whether they are networked or stand-alone, whether a security system is in place, and so on. Among the things your instructor will tell you will be the answers to the following questions:
The distinction between typing something and entering something is as follows:
%or perhaps this:
<1>~:or something similar, then your computer is ready for your session to begin. The text being displayed is called the system prompt -- a message from your computer's operating system (UNIX) that it is ready for you to enter your first command. (Your prompt is easy for a user to customize; as a result, the same UNIX system may display different prompts for different users.) Any time that you see this prompt, you can enter a command following it. Throughout this manual, we will use % to refer to the prompt.
The commands we'll ask the computer to perform take different amounts of time to complete. Some will take a fraction of a second. Others may take a several minutes. Others will continue to execute indefinitely, until we specifically ask them to stop.
The way in which you interact with a computer depends upon the computing environment of its operating system. For example, an environment in which you repeatedly:
Another common UNIX environment created at the Massachusettes Institute of Technology (MIT) is the X-window system. The X-window system provides an environment in which you use the mouse to interact with on-screen windows, menus and icons. Such environments are called graphical user interface, or GUI (pronounced gooey) environments.
One of the kinds of windows available in the X-window environment (called an x-term) provides a separate command-line environment. The X-window environment is thus a combination environment, combining the convenience of a GUI with the power of a command-line, making it a favorite of computer scientists around the world. Since both plain UNIX and X-windows require knowledge of command-line commands, we will concentrate on them in this exercise.
In order to use a command-line environment, you must learn those commands that the environment "understands." Let's first see what happens if you type something that the environment does notrecognize. Enter
% qwerty(Remember,
If you try other nonsense words, you'll find that the system responds in the same way. It's thus safe to make mistakes and mistype commands -- you won't harm the computer.
During this course, we'll create dozens of files, and so some means of organizing them is needed. Just as the documents in a filing cabinet are often kept in manila folders, we can create a container in which to keep related files, called a directory. Let's do it! Enter
% mkdir projectsThe mkdir command is the UNIX command to make-a-directory. By placing the word projects after the mkdir command, we tell the system that we want to make-a-directory named "projects." Practice by using the mkdir command to make three directories: one named "practice", one named "labs" and one named "myLib".
% lsThe ls command stands for list the contents of a directory. If all is well, you should see (at least) four things listed: the four directories you just created!
labs mylib practice projects(You may see more than these four directories, depending on how things are set up at your institution.) Many people like to visualize the relationships of these directories like this:
where $HOME represents our home directory -- the directory where UNIX places us at the beginning of a session.
The ls command can also be used to find out what is in a particular directory. Try entering:
% ls labsIf you follow the ls command with the name of a directory, it displays that contents of that directory. Since each of your directories are new (i.e., empty), there is nothing for ls to display.
To get a more detailed listing of the current directory, enter
% ls -lThe -l is called a switch that causes the ls command to generate a long (i.e., detailed) listing.
% man lsthe manual entry for ls will be displayed, providing exhaustive information about that command, including all of its switches, the meaning of the long listing information, and other information. (You can use the spacebar to 'page forward' in the manual. Most systems let you use b to 'page back'. You may need to enter q to quit reading the manual.) Don't be discouraged if much of the manual does not yet make sense, reading the UNIX manual is an acquired skill that requires much practice, and you will become more accomplished at it over time.
% rmdir practiceThe rmdir command is the remove directory command, so this changes our picture as follows:
Whatever directory you specify following the command will be removed from the current directory, provided that the directory being removed is empty. To remove a non-empty directory named DirectoryName, the command
% rm -r DirectoryNamecan be used (but this should only be used when DirectoryName contains nothing important).
Using the ls command, verify that practice no longer exists.
The directory in which you are working at any given time is called your working directory. (Your home directory never changes, but your working directory can.) To find out the name of the working directory, you can use the pwd command (which stands for path to the working directory).
% pwd /home/adamsTry it for yourself and note what is displayed.
UNIX systems utilize what is called a hierarchical directory system, meaning that directories can contain other directories, and that there is one root directory named / that contains all of the other directories in the system. Type
% ls /to list the contents of your system's root directory. Within / are a number of subdirectories, including usr, sys, include, and so on, each of which contains other directories and/or files. On my UNIX system, the users' home directories are stored within one of these subdirectories whose name is home. Within home, the name of my home directory is adams. Here is a picture showing a bit of the hierarchy on my machine:
Now, if you compare this to my output from pwd, you can see that pwd begins by displaying / (the root directory) followed by home, followed by a slash (UNIX unfortunately uses slash both for the name of the root directory and to separate directory names) followed by adams. That is, pwd lists the sequence of directories between the root directory and my working directory (which is called the path), with slashes between the directory names. Since my working directory is currently my home directory, the last name that pwd displayed happens to be my home directory (in my case adams). If we use shading to indicate the working directory, then we can visualize the situation this way:
Apply this to the output from your pwd command. What is the name of your home directory?
A last word on the home directory: UNIX treats the tilde character (~) as an abbreviation for your home directory, which can save you a lot of typing if the path to your home directory has lots of characters. We will make use of this abbreviation at the end of the lab.
Since we are working on a lab exercise, let's change the working directory to directory labs. Enter
% cd labsThe cd command is the change directory command that changes the working directory to the directory whose name follows the command.
Next, use the pwd command and note its output: how is it different than it was before?
The cd command changes our location within the directory hierarchy, effectively changing the working directory. The effect is to change the preceding picture as follows:
Since pwd outputs the path (from the root) to the working directory, the output of pwd changes each time you cd to a different directory.
To see everything in a directory, enter
% ls -aThe -a switch tells ls to list all of the contents of a directory. You will notice two odd items in the output of ls -a: a period (.) and a double-period (..).
In the UNIX file system, . is another name for the working directory (whatever directory you are in); and .. is another name for the parent of the working directory.
For example: We created labs within our home directory. The cd labs command took us from that directory "down" into labs. How do we get from labs back "up" to the directory where we began? Enter the command:
% cd ..This command moves us "up" one directory because .. is always a synonym for the parent of the working directory, regardless of its actual name. We've thus changed the working directory back to what it was previously:
(Note that since that directory is our home directory, we could also have used cd to accomplish the same thing, because cd without an argument always returns you to your home directory.) Use pwd to verify that you are currently "in" your home directory.
To wrap up this section, do the following:
Once we have created a file containing our source program, the second step can be accomplished through the use of a C++ compiler: a program that translates C++ programs into machine language (the binary 1s and 0s that a computer "understands." Graphically the process can be pictured as follows:
As we shall see in a later lab exercise, this is a bit of a simplification, but it is sufficiently accurate to give you an idea of what is occuring.
The C++ compiler used throughout this manual is named g++ and it (like emacs) is a freeware product of the Free Software Foundation.
% xemacs bases.cpp &(If you are using a terminal or telnet instead of the X-window environment, omit the ampersand (&) from the end.)
Since the file bases.cpp does not exist in the working directory, xemacs will create a new, blank editing window (called a buffer) named bases.cpp into which you can type a program. (If a file named bases.cpp existed in the working directory, then xemacs would create a blank buffer, open that file and read the file into the buffer). Note that xemacs displays the name of the buffer status bar near the bottom of the window, and that the name of a buffer is the name of the file it is displaying.
Within the bases.cpp buffer, we are going to enter (and personalize) the following C++ source program:
/* bases.cpp demonstrates basic I/O in C++.
*
* Written by: Jane Doe, Feb 29, 1999.
* Written for: CS I, at City University.
*
* Specification:
* Input(keyboard): aNumber, an integer;
* Output(screen): the base 10, 8 and 16 representations of aNumber.
***********************************************************************/
#include <iostream>
using namespace std;
int main()
{
// declare an integer container to hold the input number
int aNumber;
// 0. print a message explaining the purpose of the program.
cout << "\nThis program inputs a base-10 integer"
<< "\n\tand displays its value in bases 8 and 16\n";
// 1. ask the user to enter an integer.
cout << "\nPlease enter an integer: ";
// 2. input an integer, storing it in variable aNumber.
cin >> aNumber;
// 3. output the base-8 and base-16 representations of aNumber.
cout << "\n\nThe base-8 representation of " << aNumber << " is "
<< oct << aNumber
<< ",\n\tand the base-16 representation is "
<< hex << aNumber
<< "\n\n";
}
If you are working from a terminal or via telnet,
you will have to type in this program;
but if you are using the X-window environment,
you can instead copy-and-paste the text above
from your browser's window into the xemacs window.
Let's learn how to do this next.
In the X-window system, a block of text can be selected by dragging (holding down the left mouse button while moving the mouse) from the beginning to the end of the desired text.
Most X environments support a copy-paste short-cut:
Using one of these methods, copy the program shown above into the xemacs bases.cpp buffer.
If you are using xemacs in the X-windows environment, you can use the mouse to position the cursor at an arbitrary point by pointing at that point and clicking the left mouse button.
The editor command C-d (typing the Control and d keys simultaneously) can be used to delete the character beneath the cursor, and C-k can be used to delete from the cursor to the end of the line.
If you are running xemacs outside of the X-windows environment, you can move the cursor by the editor commands:
If you make a mistake, you can always undo the effects of any xemacs command, either by using the undo command: C-x u (type Control and x simultaneously, then type the u key by itself), or by using the Undo button on the xemacs menu bar.
The undo command can be used to undo the effects of an xemacs command that has completed. However some xemacs commands take a significant length of time, and you may wish to get out of the command while it is still being performed. For this, you can use the get-out command: C-g, which we find to be useful in a wide variety of situations.
Save file .../labs/0/bases.cpp? (y, n, !, ., q or C-h)To save the contents of the buffer in a file named bases.cpp, answer 'yes' by typing y:
Save file .../labs/0/bases.cpp? (y, n, !, ., q or C-h) yXemacs will then confirm the save operation by displaying the Wrote file ... message shown above. Since we have saved bases.cpp in the subdirectory ~/labs/0, we can visualize our situation as follows:
Compile command: make -k
For simple programs like this one, the easiest way to translate our program is to replace the make -k with an explicit call to the GNU C++ compiler g++. Using the delete (or backspace) key, erase the make -k and replace it as follows:
Compile command: g++ bases.cpp -Wall -o basesThis changes the compile command from a call to make (a program we will learn about in a few weeks) to an explicit call to the GNU C++ compiler g++, asking it to translate the program in the file bases.cpp. The -Wall is an optional switch that tells g++ to warn us on all potential sources of error. The switch -o bases tells g++ that it should write its output (i.e., the binary executable program) to a file named bases.
Once you have edited the compile command, press the Return key and xemacs will split its window in two, displaying the bases.cpp buffer (containing your source program) in one half and displaying a new *compilation* buffer (containing the results of your compile command) in the other half. If bases.cpp is free of errors, then you should see a message like:
Compilation finished at Mon Sep 5 9:12:44in the *compilation* buffer. If you instead see an error listing:
bases.cpp: In function `int main()': . . . Compilation exited abnormally with code 1 at Mon Sep 5 9:12:44then you have made typing errors in entering the program. The editor next-error command: C-x ` (Control and x, followed by the left-quote (not the single-quote) key) will move the cursor to the line in your source program where it discovered the error (but the error may be on a previous line). Compare the program above with what you have written and then change what you have written to fix the error. Then use the compile command again to see if you fixed the mistake. Repeat this edit-compile cycle as many times as necessary, until your program compiles correctly. Once your program compiles correctly, g++ writes the resulting executable file to the working directory, giving us this picture:
Use ls to verify that your directory contains the executable file named bases (it may also contain additional files, such as automatic back-up files created by xemacs).
If you are using the X-window environment, the xemacs window is just one of many independently running windows. Locate a different window in which the system prompt is displayed (i.e., the xterm window from which you invoked xemacs). Simply move the mouse into this window (or perhaps click in it) and the cursor should appear, indicating that it is ready for your command.
If you are using a terminal or telnet instead of the X-window system, use the xemacs suspend command: C-z. The operating system will then suspend xemacs and display the system prompt, indicating it is ready to accept a command.
Once you are at the operating system prompt, use pwd to check that your working directory is your 0 subdirectory. (If not, change directory to make it so.) Then use ls to display the contents of 0, to make sure that both the source file bases.cpp and the binary executable bases are there. At that point, you can enter
% basesand your program should execute successfully. If it does not, ask your instructor for help.
If you are using a terminal or telnet and you suspended xemacs using C-z, then enter:
% fgwhich changes xemacs from being suspended to being a foreground process.
In an AT&T UNIX command-line environment, you can usually print a hard copy of this file by entering
% lp bases.cppor
% lp -d PrinterName bases.cppwhile in the Berkeley UNIX command-line environment, you can usually print a hard copy by entering
% lpr bases.cppor
% lpr -PPrinterName bases.cppYour instructor will inform you of the details of printing at your institution.
Then modify bases.cpp as necessary to perform your experiment, re-translate bases.cpp into machine language, and run it. If the resulting behavior indicates that your hypothesis is false, repeat the preceding Observe-Hypothesis-Experiment steps until you form a hypothesis that is not false.
When your experimental hypothesis is not proven false, print a hard copy of your modified bases.cpp. On that hard copy, write down your hypothesis, your experiment, and circle that part of the program that performs your experiment.
As noted above, an experiment in which your hypothesis is not rejected does not prove that it is correct!
% cp bases.cpp bases.2The cp command makes a copy of the file with the first name (i.e., bases.cpp), and names the copy the second name (i.e., bases.2). Here is the pictorial representation:
Use the ls command to verify that cp command worked.
It's more often the case that we need to make a copy of a file in a directory different from the one in which the file currently resides. How are we to do this? The key is to learn a bit about how files and directories are named. Both of the names bases.cpp and bases.save are the relative names of these files. To use these names, we must be in the same directory as they are.
However, every file or directory also has an absolute name: a name that starts with the root directory (i.e., /), and lists each directory between there and the file name, just as we saw in the output from pwd. For example, the absolute name of bases.cpp on my machine is as follows: /home/adams/C++/labs/0/bases.cpp (or more briefly ~/C++/labs/0/bases.cpp.
To use a file's (or directory's) absolute name, you need not be in the same directory as that file! For example, try entering:
% mkdir ~/projects/0What did we just do? We used the mkdir command to make a new directory, but where did we make it? Recall that earlier in this exercise, we made two new directories within our home directory: labs and projects. Since ~ is another name for our home directory, we just made a new directory inside projects, called 0, using an absolute path name! Here is the picture:
Almost all of the UNIX file manipulation commands accept either relative or absolute file names. For example, you don't need to be "in" a directory to see its contents: Enter
% ls ~/projectsand you should see the directory you just made.
Similarly, to put a copy of bases.cpp in that new directory, you can enter:
% cp bases.cpp ~/projects/0and a copy of bases.cpp will be stored in that directory.
The cp command thus behaves differently, depending on whether you specify a file or a directory as the second name: if you specify a file as the second name, then the cp command will store the copy in a file with that name. If you specify a directory as the second name, then cp will place a copy in that directory with the name as the original.
Using ls and an absolute path name, verify that there is a copy of bases.cpp in ~/projects/0.
% mv bases.2 bases.saveThe mv command changes the name of (or moves) the name of the file on the left to the name on the right. Our picture changes as follows:
It can be used with both relative and absolute file names, and can be used to rename directories.
% scriptThe script command runs a program that records everything that appears on the screen of a terminal (or everything that appears within an xterm window). Thus, if you subsequently execute bases, then its output will be recorded by script. Once bases has terminated, enter
% exitto terminate the script program, which informs you that its recording is in a file named typescript. A hard copy of the file typescript can then be printed using lp (or lpr), as described previously.
% rm bases.saveThe rm command removes (or deletes) whatever file you specify. Note that it does not ask you for confirmation! (If you want such confirmation, call rm with the -i switch.) Earlier we saw that when rm is invoked with the -r switch, it recursively removes a directory and everything within it.
Use the rm command to remove the files typescript, bases.cpp~ (a back-up file created by xemacs) and the executable file bases. Then use the ls command to verify that all of the files except bases.cpp are gone. If there are other files remaining in your 0 directory, use the rm command to remove them, as well.
Then end your session with the computer, following your instructor's instructions.
Note: If your head feels ready to explode, don't panic! This first lab covers a great deal of material, that you will use over and over again, and as you do so, you will begin to naturally memorize those commands that you use most frequently. You can speed up the process by reviewing each of the steps you took in this exercise.
Until such a time as you remember the various commands, here are two quick reference pages to help you "look up" commands: one listing some of the commonly used UNIX commands and one listing some of the commonly used xemacs commands. Print hard copies of these, and store them in a convenient place!
To learn to use these quick references, I would recommend that you review today's exercise, and find each of the commands we used in the exercise on the appropriate quick reference sheet. Then study the description of that command on the quick reference sheet, so that you associate the two in the future.
Forward to the Homework Projects