This tutorial discusses:
Students in this course must get an account on our CSA network, which consists of machines that run the UNIX operating system. Students will also need to get an NT account to use the machines in our teaching lab, although we will often log into the CSA network from those NT machines.
Like other common operating systems (the Mac operating system or MS Windows) information on our UNIX systems is stored in files. Besides files, UNIX allows the creation of directories, in which other files and directories can be placed (a directory is the same as a folder in Mac-speak).
All the files and directories on the CSA network form a file hierarchy, something like:
File Hierarchy (or File System) ------------------------------- / <-- root directory (a slash) / \ home ... / \ ugrad course / \ ... ... / | \ ... ... username <-- your home directory
So that those that have accounts for this course (except CS majors) will have an account in the location:
This is called the path of your home directory, the directory you are placed in when you log in.
A path just represents the location of a file or directory in
the file hierarchy. For example, the first path above says that under the
root (/) directory, there is a
home directory, under it,
course directory, under it, a
and under that is where your
Since this path starts at the root (/), it is called an absolute (or full) path.
You should use separate directories to store files associated with a tutorial, lab or particular homework. Make a directory for this UNIX material by typing:
which will add another directory to our picture:
Bottom of File Hierarchy ------------------------ / | \ ... ... username <-- your home directory / unixlab <-- new directory
Remember, paths that start from the root (/) directory, like
/home/course are call absolute (or full) paths.
Now go to the directory by typing:
which will change your present location in the file hierarchy:
Bottom of File Hierarchy ------------------------ / | \ ... ... username <-- your home directory / unixlab
To make sure you are there, you can type:
and it will report your present working directory, which should end inpwd
Let's look at a program. We've written a program that sums a series of
numbers. The program's source code is in
To download this program into your
unixlab directory, do
the following in :
To make sure the program got saved correctly, go to your UNIX window and type the ls command to list the files in your current directory. See that it is there.
One way to display a text file is to use the cat utility followed by the name of the file you want to view. In our case, you'd do:
It will display the contents of the file on the screen. However, if the program is too long to fit in the window, you'll only see the end of the file on the screen.
Since long files won't fit in one window, you can use less to view such files instead of cat. Do...
Less will allow you to page forward through the file using
<Space> bar and page backward using the
b key. In that sense, it is a bit more useful than a
similar utility called more.
When you are done looking at the program, type
q to quit
If you needed a printout of this code, you could use lpr to send a copy to the printer, as in:
DON'T PRINT THAT FILE NOW, AS WE DON'T NEED A BUNCH OF COPIES GOING TO THE PRINTER!!!
The source code
sum.cpp is not ready to be run on the
computer. First, we must compile the program (and link it) into what
is called an executable. The executable then can be run.
Now, let's compile and link the program with our C++ compiler
g++. All you need to type is
"g++" followed by
.cpp files that make up the program, as in:
If something is wrong with the source code, g++ will display error messages...If so, pay attention to the first error messages and the lines that they occur at:
sum.cpp:41: unterminated string or character constant sum.cpp:39: possible real start of unterminated constant
Since there is an error in the program, we'll want to edit the source code and fix the error. To edit or even create a text file, we can use the program emacs. To edit the source code, type the following at the UNIX prompt:
emacs sum.cpp &
&) at the end of commands that bring up their own window, since doing so gives us another prompt back right away where we can type more UNIX commands.
Once Emacs is up and has loaded the source file, we can go directly to
the line where the error is. To do so, type the command
<Esc> x goto-line and hit
Then, enter the line number where the error occurs (here, line 39) when
prompted to do so at the bottom of the Emacs screen.
What is wrong with the line? Fix it.
Now to save the file back to disk, choose Save Buffer from the Emacs Files menu.
When you are done with Emacs, choose Exit Emacs from the Files menu.
The source code should now be correct, so go back to the UNIX window and compile the program again.
This time you should have an executable named
(which is the default name for an executable).
You can use ls to see that
a.out is there.
If there is an asterisk (
*) at the end of
a.out, it just means that file is executable, it
is not part of the filename.
If we wanted to change the name of
a.out to something else,
we could do it in 2 ways:
cp a.out exec1
mv a.out exec2
There is also an easy way to tell g++ to give the executable a
name other than
a.out. You can do so using the -o
(minus oh, not zero) option to g++, as in:
g++ -o total sum.cpp
Note that the desired name of the executable follows the -o
option. In other words, we have asked g++ to store the
executable in a file named
To run the executable, you just type its name,
Since this program scans in values, it will sit there
waiting for you to type input.
Type the following bold values when the program prompts you for them:
Enter how many numbers I will sum: 2 Enter number: 3.4 Enter number: 4.5 The sum is: 7.9 Thank you for using the summer!
The program should then tell you that the sum is 7.9.
The program in
sum.cpp uses the C++ object
to get input. By default, input is typed at the keyboard but it can
also be taken from a file.
There is a file named
that can be used as input. Download it into your
directory right now.
To run the program again, this time taking input from the file,
we will using input redirection. Instead of just typing
total, we use:
This will make the program read values fromtotal < data1
data1instead of expecting us to type them from the keyboard.
Note that the output is:
Enter how many numbers I will sum: Enter number: Enter number: Enter number: Enter number: The sum is: 10 Thank you for using the summer!
All the prompts get printed, but you won't see the input since it comes from the file rather than the keyboard.
Note that the program should say that the sum is 10.
Since you have limited disk space for this course, you may want to remove unnecessary files. If you do not know what command is used to remove files, you can look it up on our system.
The UNIX Manual Pages let you look up information on UNIX commands (also C and C++ library functions), including how to use them. To view Manual Pages, we use the man command.
Let's first look for a command to remove files:
man -k 'remove files' ... rm rm (1) - remove files ...
The -k tells it to look up the key phrase 'remove files', which has to be in quotes because of the space between the 2 words. (In practice, you might have to try different key phrases or words before you find what you are looking for.)
It tells us that we want the rm command in Section 1 of the Manual Pages. Now, we can look at the documentation for rm by using:
Man displays the info just like the program less.
Reading the documentation given by man, you can see that all we
need to type to remove a file is
"rm" followed by the names of
files to be removed.
Remove the executables
exec2 by using:
rm exec1 exec2
Finally, since we are done with the files in the
directory for now, let's go back up to our home directory. Since you
are one directory below your home directory, you can just type:
to go up one level. Dot dot (
..) is a way to refer to the
We could also use:
since tilde (~) is shorthand for your home directory at the UNIX prompt (and in some applications, like Emacs). This works even if our home directory is not right above where we are.cd ~
Finally, we could also just use:
by itself, which also goes to your home directory.cd
You can confirm where you are by using pwd.