This document was originally written for CS50 by Professor Margo Seltzer and members of the CS50 course staff. -DJE
If you have used a PC, then you might be familiar with DOS, or its more recent relatives, Windows 3.1, '95 and NT. If you have used a Macintosh, then you might be familiar with the Mac Operating System. UNIX systems are different from DOS, Windows and Mac OS in that they are designed to handle many simultaneous users (called multi-user systems). Additionally, UNIX systems can perform many tasks at once (called multi-tasking).
Finally, UNIX was designed primarily as a tool for programmers. Rather than offering a complete solution to a problem (e.g., a single application that lets you edit text, format it, and print) UNIX provides you with a large assortment of tools that can be mixed and matched to accomplish a wide variety of tasks. You might find this approach confusing and frustrating at first, but bear with us; once you know your way around the system, you will find it a very productive environment.
The book "Harley Hahn's Student Guide to UNIX (Second Edition)" (Hahn) is a good and in-depth introduction to UNIX. If you are not familiar with UNIX, I suggest that you buy a copy of this book and skim through it as need be (and even if you are experienced, this book can be a valuable reference).
The book "Learning the UNIX Operating System" (Todino & Strang) provides a relatively short and simplistic introduction to UNIX. This book discusses a variant of UNIX called System V, which is similar to HPUX.
To find out more detailed information about any command on the system, use the on-line manual pages described in a later section.
In order to use the Harvard UNIX systems at all, you will need an account. You should have been given an account when you registered for Summer School, and this account will suffice for all of your coursework. If you did not receive an account, please contact the User Services people in the basement of the Science Center ASAP about getting one (or contact me, if you have difficulty obtaining an account).
When you go to log into the HP workstations from the console, there will be a window displayed where you enter your logname and password. When you log in, the window manager will start up automatically. You can click on the small terminal icon to create new windows. Clicking on the background will give you a menu that will allow you to log out.
man topicwhere man is the command you are executing and topic is the topic about which you are seeking information.
You can also find out information about things even if you have forgotten their exact names. To get a list of all the commands or manual pages related to a certain topic, type:
man -k topicSometimes you will see man pages referred to as name(1). The number following the name of the man page is the section in which you will find the command. Normally, you will not need to worry about the sections, but in cases where the same topic appears in multiple sections, you may need to specify the section. In this case, you can type:
man n topicwhere n is the section of the manual you need.
In general, you can use the man pages to answer questions and help you learn about the system. They should be your first source of information when you have questions.
For now, you can ignore shell features and just use it to type commands. As you become more comfortable with the system and want to do more complicated things, you can learn more about the shell, especially how to write programs in it. To find out about some of the power of the shell, look at the man pages.
There are a number of good programming editors available for UNIX, but by far the most popular editors are vi and emacs (or some of their many descendants). Each of these editors has advantages over the other, and there are endless and pointless debates about which is better. For the purposes of this course, however, we recommend that unless you are already familiar with emacs (and don't want to take the opportunity to learn a new editor) that you learn vi.
vi may seem a bit confusing at first, especially if you are used to graphically-oriented text processors such as Word Perfect or Microsoft Word, but given a little time, most users find that it is easy to learn how to do some very powerful things with vi.
Emacs is arguably more powerful than vi, and is vastly more extensible, but many people find it harder to learn, and therefore never learn enough about it to make use of the advanced features it offers. People susceptible to RSI often find emacs commands hard to type, since many of the command commands require awkward use of the control or meta keys.
The default editor on the Harvard systems is pico. This editor was designed for email and to be extremely easy to learn. Is is sufficient for editing prose, but doesn't contain a lot of the features that are useful to programmers. I recommend that you invest the time to learn vi or emacs-- they contain features that will make your editing chores much faster and easier.
If you want to change the default editor that you use, add the following commands to the end of your ~/.cshrc file:
setenv EDITOR prog setenv VISUAL prog(where prog is the name of the editor you prefer).
Many powerful editors, such as vi and emacs, are not completely documented by their manual pages. Instead, they have their own documentation (including on-line help). Emacs and vi tutorials are available from User Services, and there is more information available online (via web pages, etc). The Harley Hahn book (mentioned earlier) includes tutorials for all of vi, emacs, and pico.
The system that we will be using for this class is called RCS (Revision Control System). RCS will keep track of all old versions of a file while you are creating a new version. If you like the changes you have made, and you wish to make them permanent, you can use the checkin command to do so. Once you have done that, RCS believes that most recent copy of the file is the "real" one. If, instead, you decide that you do not like the changes you made, you can retrieve any previous version of the file from RCS using the checkout command. Also, if you are trying to figure out what has changed since an earlier version (because your program used to work and now it doesn't), you can use the rcsdiff command to show you what has changed.
You can find out all the details of RCS by looking at the man pages for rcs, ci, co, rcs diff, rlog, and others. In the meantime, we have created some simplified versions of these commands that are described here.
The editor that checkin uses by default is vi. If you prefer to use a different editor, then make sure that your environment variable EDITOR is set to the name of the editor that you prefer. For example, if you prefer emacs, add the line:
setenv EDITOR emacsto your ~/.cshrc file (if you haven't done so already).
To run submit:
asst1: File exists tar: asst1/RCS/ couldn't create directory asst1: File exists tar: asst1/RCS/LOG - cannot create asst1: File exists tar: asst1/RCS/array.c,v - cannot create asst1: File exists x asst1/array.c, 16 bytes, 1 tape blocksThese messages can be safely ignored.
For each command we give the command name in command font and a mnemonic (in italics) indicating why the command has such a bizarre name, and a description of the command. As always, you can get more information from the man page.
grep Thurs ~lib50/cs50.times > thursday_sectionsThis command would look for the string "Thurs" in the file "~lib50/cs50.times". It would then put the output of the command (what the command would normally print to the screen) in the file "thursday_sections."
On the other hand, you might have a program which expects the "user" to input a long list of words. You might get tired of typing the words in each time you run the program, so you could use input redirection ("<") to help you:
myprog < /usr/dict/wordsThis command line would execute the program myprog and give it as input the data in the file "/usr/dict/words".
In addition to using the shell to redirect input and output to files, you can use a shell mechanism called a pipe to string commands together. For example, suppose that you wanted to look at all the five-letter words in the dictionary. You could type:
grep '^.....$' /usr/dict/wordsUnfortunately, there are many five-letter words and the list would quickly scroll off your screen. What you might really like is to be able to take this list and use the more program to display it one screenful at a time. While you could do this by creating a temporary file:
grep '^.....$' /usr/dict/words > tempfile more tempfileYou do not necessarily want to create the temporary file. Worse yet, suppose you were constructing a file so large that it could not fit on your disk. The shell lets you string together the grep and more command using pipes.
grep '^.....$' /usr/dict/words | moreThe "|" symbol creates a pipeline between the grep command and the more command. The output from grep (your list of five-letter words) is used as the input to more. You can string together as many commands as you like:
grep '^.....$' /usr/dict/words | sort | more
So you don't have to do each of these steps manually, the make utility can keep track of all your ".h" and ".c" files and generate the commands that must be executed to compile and link them. It uses a set of rules to figure out how to do this. The language in which to write these rules is cryptic and difficult to understand, even for experienced programmers. Fortunately, make already knows all of the rules that you need for this class. We will usually give you a makefile (set of rules for make to use) so all you will need to do is type
makeSometimes we will ask you to change the names of the files listed in the makefile. In any case, at this point in your programming career, you need not concern yourself with the details of make. However, you should have an understanding of what it is doing for you.
The core file can be used by gdb in order to help perform an autopsy on the program. You can use gdb as you normally would, only you cannot execute the program from the core; you can just look at the values of variables to determine where it crashed, why it crashed, and ideally, how to make it stop crashing.
Please check your email frequently (especially when you are working on your homework) in order to see whether we've sent out any changes to the assignment or added any hints.
There are a variety of different mail reading programs available on the science center machines. The most common is pine. pine is easy to learn, and has a number of useful features. User Services (in the basement of the Science Center) has documentation and tutorials for pine.
The most basic mail program is called mailx, and is described in Todino and Strang's "UNIX in a Nutshell". Refer to the man pages and the book for instructions on using mailx.
There are two mailing lists set up for this course:
Note that for this mailing list, the entire email address needs to be specified; just using s-q is not sufficient.