Problem session 1, 11 Sept 2002
Demonstrated basic unix usage, compiling programs, moving around files.
Created the basic
"Hello, world" C program and compiled and ran it.
Demonstrated the use of basic unix commands:
- an editor -- you might want to start with "pico"
- The "cat" command is the easiest way to display a short file on the
screen in unix. Type "cat hello.c" to see the new file.
- Type "ls" to see a list of your files (in the current directory).
- Short version of compiling this C program: cc hello.c
- That creates a file called "a.out" which is the compiler output.
Run it by typing "./a.out".
"." is the current directory. This runs the program "a.out" in the current
directory.
- If you don't specify the location of the command you are trying to run,
e.g. when we typed "ls", it looks in a standard list of directories.
For example, it finds ls in the file named "/bin/ls".
The current directory may or may not be in this directory list, depending upon
configuration. That is, you may or may not be able to type simply "a.out".
- Type "mv x y" (for "move") to change the name of the file "x" to "y".
Then try typing "ls", "cat y", and "cat x". Type "rm y" (for "remove") to
delete the
file. Type "ls". Also note the "cp" command ("copy"), which is like the "mv"
command except it creates a new file, so that then there are two.
- As with editors, there are several available e-mail reading programs. The
mail reader called "pine" is a good but simple one, and it has the same pico
editor built in. Type "pine" to start it. Beware its habit of displaying
important messages at the bottom.
- When you are done with the computer, don't forget to "log out". If you
don't log out, then whoever next uses the computer can impersonate you and
modify your files, etc. On the other hand, if you come upon a computer on
which someone has forgot to log out, you must log them out and then log in as
yourself.
More about compiling:
- Use "gcc" for better error messages. (In linux, "cc" is the same as
gcc; in solaris (e.g. eddie.cdf), "cc" is the Sun C compiler and "gcc" is
what we recommend you use.)
- Use options -Wall -ansi -pedantic for more error messages.
- If you call any functions in the math library, you need to put "-lm" at
the end of your command line. Demonstrated with sqrt.c
which uses the sqrt() math library function (trivially).
- Most other libraries are rolled into the "C library", which is linked in
by default.
Directories in unix:
- "mkdir" to create a directory.
- "cd" to change the "current" directory.
- "pwd" to tell you the full path name of the current directory.
- Just as "." always means the current directory, ".." always means the
parent. Thus "cd .." means "go up one".
- A file path name which begins with a slash is an "absolute path name",
starting from the root. A file path name which does not begin with a slash is
a "relative path name", starting from the current directory.
- "rmdir" removes an empty directory. I don't think I mentioned that "rm
-r" removes a non-empty directory by first removing all files inside it; it's
recursive, in that if any of those files are themselves directories, it will
delete recursively. So you can remove a huge number of files (accidentally?)
with "rm -r".
[list of problem session notes]
[main course page]