Programming for Unix is not any more difficult than programming
for other popular operating systems; in fact, it is simpler once
you get started. The main issue confronting aspiring Unix
programmers is that there is no obvious easy-to-learn set of
programming tools (i.e. nothing like Microsoft's Visual
Studio). Instead, command-line programs are the order of the
day. This tutorial is intended to walk the reader through a set of
very simple programming examples using the Unix shell, the editor
emacs, and the GNU C compiler gcc.
First, we assume that you are doing this tutorial from the comfort of your own dorm room -- that is, you are not benefiting from the X Window System available in the Jennings Computer Center. In order to use the campus's systems, you will need to use either an ssh client (recommended) or a telnet client. These are available for many platforms; highly recommended are putty for Windows machines and NiftyTelnet for Macintosh. When you have an appropriate program installed, use it to log in to one of the Unix lab machines. You should be confronted with a prompt like this:
Last login: Mon Jan 14 20:52:53 2002 from adsl-64-108-88- ------------------------------------------- JCC Unix Lab MOTD (unixadmin@eecs.cwru.edu) ------------------------------------------- . . . styx 31%
The % indicates the Unix system is ready for a command. We will set
up a directory for you to work on your EECS 338 projects. Enter
mkdir eecs338 and hit RETURN (all Unix commands are sent
with RETURN). Then type ls (list files). You should see
your new directory. To switch to it, type cd
eecs338. To go up a directory level, type cd
.. (very similar to DOS).
If you want to learn more about the commands you just used, type
man ls, man cd, and man
mkdir. The man command will display the manual
page for any Unix command or programming function, provided a manual
page exists.
If you're not sure what Unix command to use, the
apropos or man -k command might be
helpful. To see it in action, type apropos print to view
every command with the word "print" in its description. For more
information, see Chapter 1 of Gray's text.
emacs editorThe free editor
emacs is installed on the JCC computers. Other text
editors available include vi, xemacs, and
pico. I personally use xemacs, but it is
more resource-intensive than regular
emacs. pico is the easiest, but lacks many
features (such as automatic indenting) which make programming
easier. This tutorial uses emacs throughout; you are free
to use whichever editor you prefer.
To start emacs, merely type emacs at
a command prompt. After a brief start-up period, you will see a
screen with several instructions. Feel free to go through the
tutorial and explore the editor; however, there are a few
enhancements to your preferences which may be very
useful. Foremost is that on many ssh and telnet clients, the
backspace key sends C-h, which is
emacs's help command. If you are afflicted by this
problem, it can be fixed in several ways. One way (which
conveniently introduces opening, editing, and saving files in
emacs) is to type C-x C-f (Control-X,
then Control-F) to open a file. Enter ~/.emacs at the
Find file: prompt. (If you make a mistake and the
system reacts badly, C-g cancels the current
command). Try again, and use the DEL key instead of backspace to
fix mistakes. Enter the following lines exactly as written
(suggested in O'Reilly's Unix in a Nutshell):
(keyboard-translate ?\C-h ?\C-?) (keyboard-translate ?\C-\\ ?\C-h)
This tells emacs to delete a character when
C-h is received, and bring up the online help when
C-\ is received. If you like, you can change
C-\ to something else. To save your changes to the
.emacs file, type C-x C-s. Then type
C-x C-c to quit emacs. Restart it, and
your backspace key should work correctly.
Another important change (in my opinion) is to change the
default C indentation to make code easier to read. Reopen your
.emacs file and add the following lines:
(custom-set-variables '(c-default-style "k&r"))
This time, instead of restarting emacs, reload
your .emacs file by typing ESC followed by
x (or M-x if you know your client's Meta
key), then load-file. You should get a prompt; answer
.emacs. Now your code will be automatically indented
according to the style in Kernighan and Ritchie's The C
Programming Language when you press the TAB key.
Now that you have set up your emacs environment,
create a new file with C-x C-f. Call it
~/eecs338/hello.c (this filename puts it into the
eecs338 directory you created earlier). Enter the
following program:
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
return 0;
}
You will notice that emacs highlights parentheses
and brackets as they are closed, as well as indenting to the
correct position. Save this file and quit emacs.
If you are not already in your eecs338 directory,
go there and make sure your hello.c program is
there. To compile this program,
type gcc -o hello hello.c. Specifically, gcc is the
compiler command, -o hello tells it to name the
output executable "hello", and hello.c is the
filename to compile. After this is done, use the command
ls -l to get a detailed listing of your
directory. You should see something like this:
-rwxr-xr-x 1 sjh13 5924 Jan 14 22:28 hello -rw-r--r-- 1 sjh13 86 Jan 14 22:28 hello.c -rw-r--r-- 1 sjh13 85 Jan 14 21:51 hello.c~
In this listing, hello.c~ is a backup file which
emacs automatically created, hello.c
is the source file, and hello is the
executable. Notice that there are x's in the
listing for hello; this indicates that it is an
executable file. To run your program, type
./hello, which tells Unix to look in your
current directory for a command named hello.
Suppose you wanted to keep all of your assignments and test
programs in their own directory within ~/eecs338. And
suppose you didn't want unauthorized people looking at your EECS
338 assignments. Let's set up the beginnings of such a system
right now.
You should be in your eecs338 directory, which
contains several files related to the "Hello World" program. Make
a new directory for these files called hello_dir (or
if you like, something else entirely). To move files, use the
command mv [source-file]
[destination]. For example, mv hello hello_dir
puts hello into the proper directory. Wildcards can
be used; mv hello.* hello_dir will move all files beginning
with "hello." into the hello_dir
directory. Switch to the hello_dir directory and type
rm hello.c~ The rm command deletes
files; the emacs backup file is no longer needed in this
example. One other useful file manipulation command is
cp; this copies files and has a syntax similar to
mv.
To keep people away from your ~/eecs338/
directory, you'll need to set the proper permissions. Type
cd .. to go back to your eecs338
directory, and type it again to go back to your home directory.
Now type ls -l. You should see something like this in
the listing:
drwxr-xr-x 5 sjh13 1024 May 23 2001 eecs338
The letters to the left show the current permissions for the
directory: read, write, and
execute. In my example, I (the owner) can read, write, and
execute things in the directory (indicated by the second, third, and
fourth letters), while members of my group and everyone else can read
and execute, but not write (the rest of the letters). To keep others
from reading or running programs in the eecs338
directory, use the command chmod og-rx ; this command
removes (the minus sign) read and execute permissions
(rx) from group and others (og). There are
many other options for chmod; see the man
pages for details.
There are a number of other useful utilities for programming in
Unix. Some of the most important ones are the GNU debugger
gdb, the source code control systems rcs and
sccs, and the project building program
make. There are ample resources for these programs on the
web, but if this tutorial is well-received, I can write some short
introductions from an EECS338-specific point of view.