A Tutorial on Unix Environments

For a complete guide to the Unix programming environments, check out the UNIX Frequently Asked Questions, or read The UNIX Programming Environments by Brian W. Kernighan and Rob Pike (Prentice-Hall, 1984, ISBN 0-13-937681-X) or any other introductory UNIX books.


Logging into ZOO PowerPCs

You can log in to any of the PowerPC (PPC) machines directly from the console in the Zoo, or do the remote login (via telnet) from any machines across the campus. The PPCs in the Zoo are named as follows:

aphid        cricket      hippo        macaw        rattlesnake  tiger
bumblebee    frog         hornet       monkey       rhino        tucan
cardinal     gator        jaguar       newt         scorpion     turtle
chameleon    giraffe      koala        peacock      swan         viper
cicada       grizzly      ladybug      perch        termite      zebra
cobra        hare         lion         python       tick
The server of these PPCs is named powered.

If you do the remote login, use the command telnet or rlogin, for example,

    telnet monkey
will get you into the machine named monkey. The terminal will be displaying a message like

    AIX Version 4
    (C) Copyrights by IBM and by others 1982, 1994.
    login: 
UNIX systems are case sensitive; upper and lower case characters are treated as different. UNIX systems generally use lowercase characters.

All commands should be typed in lowercase characters. All users on a UNIX system are assigned their own usernames. When they logon to the system, they are placed in a HOME directory, which is a portion of the disk space reserved just for them.

Now type in your allocated username.

UNIX asks for your password. Now type in your allocated password. Note that the password is not displayed on the screen as you enter it.

    login: shao
    shao's Password:
UNIX checks the username and password you typed, and if correct, will run the shell program for you. The shell program is your interface to the system. It presents the following messages,

    *******************************************************************
    *                                                                 *
    *      Welcome to AIX Version 4.1.3                               *
    *                                                                 *
    *                                                                 *
    *                                                                 *
    *                                                                 *
    *******************************************************************
    # 
    8-02-95 Still in the experimental stages, but we are moving ahead...
     
     man -k ( and whatis db )  exported to clients -- please report any
     missing man pages or whatis  entries to smith-roger@cs.yale.edu or
     requests@cs.yale.edu
     
     Do NOT reboot the zoo nodes by using the power switch
   	try a dsac member or mail to requests from another machine
   	if the machine is locked up for more than a couple minutes
     
     Suned file systems --> powered early AM 8/22/95
     ( powered back up late 8:22 AM  -- early PM?? with new links )
     
    Last login: Fri Aug 25 10:02:08 1995 on /dev/pts/4
     
    TERM = (xterm) 
If the line

    TERM = (xterm)
is displayed, and you are logging in from the console whose type is xterm, just type return. Otherwise, you need to enter the correct terminal type. Parameters can be

    vt100
    vt102
    vt200
    ansi
    sun
    ......
The parameter must match that of the telnet configuration file. Enter the appropriate terminal type for your system (should be vt100 most of the time).

UNIX is ready to interpret and execute your commands.

pwd (print working directory)

When a user logs in to a UNIX system, they are located in their own directory space. Users are generally located off the /server/homes/classes/cs210/class directory. The pwd (print working directory) command displays the pathname of the current directory you are in. This is helpful when you want to know exactly where you are.


Unix Basics

This section is to introduce you to basic UNIX commands. The commands and utilities you will use are the same as those on other UNIX systems. Read this section carefully, and you should be able to do the following

	* manipulate files
		- create, delete, append, concatenate, display, move
		- count the number of lines, words, characters
		- extract lines
		- sort
		- change permissions on files
		- print files
		- search for text strings within files

	* manipulate directories
		- create, delete
		- display contents

	* change your password

	* use basic commands
		- display a list of users
		- display messages and news
		- time commands
		- obtain help on command usage
		- run programs in the background
		- display the shell environment variables


Online Helps

man (manual)

This command locates and prints the reference page for a specified item. Now type the command,

	man ls
will list the online manual for the command ls. The : represents a prompt. Pressing enter will continue the display. To quit from the manual, type q. If you want to print some oneline manual pages on a printer, check out the piping commands.


Handling Files

ls (list files in the current directory)

This command is similar to DIR in DOS; it displays a list of all the files in the directory. New users don't have many files in their home directory.

Now type in the command

	ls

You will note that the shell prompt reappears, and there is no file listing of the directory contents. This does not mean that there are no files stored there. Just like DOS, UNIX systems support hidden files.

UNIX systems extend the power of commands by using special flags or switches. These switches are one of the most powerful features of UNIX systems. Switches are preceded with a - symbol.

Now try the command (to illustrate the use of switches)

	ls -la
The switch l stands for long listing, and the a switch is for all files, including directories and hidden files. UNIX responds with the following listing of the directory
	ls -la
	total 6
        -rwxr--r--   1 xtest    cs210        240 Aug 28 10:40 .login

Lets explain what all this means.

   permission settings
    |
    |           number of links to file
    |           |       
    |           |       owner of file
    |           |       |       
    |           |       |       group
    |           |       |       |       
    |           |       |       |       size of file in bytes
    |           |       |       |       |       
    |           |       |       |       |   date/time of last modification
    |           |       |       |       |       |
    |           |       |       |       |       |               filename
    |           |       |       |       |       |               |
-rwxr--r--      1       xtest   cs210   240     Aug 28  10:40    .login

The first part reveals the permission settings of the files (-rw-r--r--). These have the codes as follows

		d       directory
		r       read
		w       write
		x       execute
		-       no access

Note that there are three sets of permission settings.

		specifies whether a directory entry
		|       permissions for the owner of the file
		|       |       permissions for group members
		|       |       |       permissions for others
		|       |       |       |
		-       rw-     ---     ---

WHAT are the permission settings for the file .cshrc in the current directory?

	owner = ...........

	group = ...........

	others= ...........


cat (concatentate)

Is a utility used to combine files or typing files to the screen. The cat command sends its output to the console screen (in UNIX we call this standard out or stdout for short).

Now try the following command to view the file .login on the screen.

	cat   .login

The format of this command specifies that the cat utility is to use the file .login as its input, and send the output to the console screen.

Using the cat command to copy data from the keyboard to a file

Now try the command

	cat  -  >   temp
This specifies that the input is from stdin (- means the keyboard), and writes the output to the file called temp (> means dump into).

Now try the following lines of text (ignore any typing mistakes, and do not use the cursor arrow keys).

	Small change got rained on by his own 38
	down by the arcade,
	The marquees weren't weeping, they went stark staring mad,
	and the cabbies were the old ones who really had it made

Press CTRL d to terminate text entry, and the shell prompt returns. What is the command used to view the file temp on the screen.

	............................................

The cat utility can also be used to join many files together into a single file. The command,

	cat  temp  .login  >   temp1
copies the two files (temp and .login) to a file called temp1. What is the command you would type to copy the file temp to a new file called temp1 using the cat utility.

	............................................

The > > is used for appending to an existing file,

	cat  temp  >>   temp1


more (display a file page by page)

This command is like cat; it also displays the file, but instead , it displays the file screen by screen. This is extremely useful if you are reading the source code in a terminal.

For example, to read the file named README, try the following:

	more README
Alternatively, you can always read a file by openning it inside emacs.

cp (copy)

This command stands for copy, and is used for copying one file to another.

Now try the command

	cp  .login  temp2
This copies the file .login to another called temp2. If temp2 had already existed, its previous contents would've been erased.

Files can also be copied to another directory. The command

	cp * /usr/tmp
would copy all the files in the current directory to the directory /usr/tmp.

What is the command you would type to copy the file temp to a new file called temp1 using the cp utility.

	............................................


mv (move)

The mv command is used for moving or renaming files. Now try the command

	mv temp temp2

This renames the file temp to temp2.

As an example (do not type this), the command

	mv temp2 /usr/tmp
would move the file temp2 into the directory /usr/tmp (it would no longer appear in your home directory).


rm (remove)

The rm utility is used for erasing files and directories.

Now try the command

	rm  temp2
This removes the file. Once a file is removed, it cannot be restored. To cover situations where mistakes might occur, a switch -i appended to this command will request a Yes or No response before deleting the file.

Now try the command

	rm  -i  temp1

NOTE that switches are written before the filenames. Answer Y to the prompt so that temp1 is removed (Normally, people like to set the -i switch as the default).


head

This is used to view the first few lines of a file. It accepts a switch specifying the number of lines to view. The command

	head  -2  temp
would list the first 2 lines of the file temp on the console.

What is the command to view the first 10 lines of the file .login.

	............................................


tail

This is used to view the last few lines of a file. It accepts a switch specifying the number of lines to view. The command

	tail  -2  temp
would list the last 2 lines of the file temp on the console.

What is the command to view the last 10 lines of the file .login.

	............................................


Printing Files

lpr

This command is used to obtain a hardcopy printout of a file. ONLY type this command IF a printer is attached to the computer system.

	lpr temp

To use a particular printer named pname, type
	lpr -Ppname temp

enscript

This command is also used to print out a file, but the printout looks much better than lpr.

	enscript  temp

The switches enscript accepts are
        
	-r              printing in the landscape mode
	-2r             two column printing
	-G              add a nice header (good for printing source code)
	-Ppname  printing on the printer named pname

Often, people prefer to use the following options to print a REAL NICE looking listing for the C code:
	enscript  -2r -G hello.c

Certainly, you can still specify a particular printer
        enscript  -2r -G -Ppname hello.c


Handling Directories

UNIX systems support hierarchical directory structures. The commands used for directory maintenance are,
	pwd             print current working directory
	cd              change directory
	mkdir           make a subdirectory
	rmdir           remove a subdirectory

pwd (print working directory)

Prints the current working directory on the console screen. In UNIX, the hard disk area is divided into directories, much like abook is sub-divided into chapters and paragraphs. The directories form a hierarcherical level, which simplifies the organisation of the files on the system. The top most directory in UNIX is called root, and consists of a number of subdirectories grouped according to function.

    bin             system binary executables
    boot            file for UNIX boot code
    dev             file names for system devices, terminals, disks etc
    etc             system administration files and tools, system scripts 
    lib             library routines
    lost+found      unreferenced files are placed here by fsck
    tmp             scratch pad directory for temporary files. users must 
                    have read/write access
    unix            unix kernel
    usr             application and system specific information
As you can see from the previous directory tree, there are lots of sub-directories underneath root.

In the space provided below, enter your current directory (use the pwd command).

	........................................

When you use the ls command to list the file contents of a directory, those entries which are directories are preceeded with a 'd' character. The sample screen below shows this.
ls  -la
total 19 files
drwxr-sr-x      3       xtest           512     Nov 24 12:05    .
drwxr-sr-x      46      root            1024    Nov 23 16:46    ..
-rw-r--r--      1       xtest           2501    Mar  3  1992    .login
drwxr-s---      2       xtest           512     Nov 24 12:05    datafiles
-rw-r-----      1       xtest           0       Nov 24 12:05    dirlist
-rw-r-----      1       xtest           24      Nov  5 08:59    tmp
Note that datafiles is a sub-directory. There are two other subdirectory entries in the listing.
	.       the current directory
	..      the parent directory (upwards towards /)

cd (change directory)

This command is used to change the current directory. For each of the following commands, enter the current working directory after the command is executed.

    cd .                    ...................................
    
    cd ..                   ...................................
    
    cd /                    ...................................
    
    cd $HOME                ...................................
	

mkdir (make directory)

This command makes a sub-directory under the current directory. In the space provided below, enter the command you would use to create a sub-directory called junk.

	.............................................

rmdir (remove directory)

This command removes (deletes) a sub-directory under the current directory. In the space provided below, enter the command you would use to delete a sub-directory called junk.

	.............................................


Piping Commands

UNIX systems offer true pipelining. This feature is extremely powerful. It means that the output of one utility can be fed directly into the input of another. The | symbol indicates pipelining in UNIX systems.

To illustrate how this works, consider the following command.

	ls -l | wc -l
This creates a pipe between the output of the ls utility and the input of the utility wc. The command runs ls, which lists the contents of the current directory, feeding the output into wc which prints the line count on the standard output.

Similarly, we can use the piping commands to send the online unix help files to a printer, the command

	man ls | enscript -2r
will print the manual in two column using the enscript command described above. A pipe inter-connects two programs together.


Changing Passwords

passwd (password)

All users in UNIX are assigned passwords. Currently, there is a password assigned to the user. This is changed using the passwd utility. The user would type the command

	passwd
The utility asks for the new password (twice, to make sure you didn't misspell it). The user types in the new password, but notice that it will not displayed on the screen when typed (for security reasons).

The new password would then be in effect. If the user were to log in again, the system would ask for this password to be typed before allowing access.


Background Processing

Another feature of UNIX is running processes in the background. When a command is appended with the & symbol, it is run in the background and the shell prompt immediately returns.

This is handy for such things as printing documents, as it allows you to continue doing something else instead of waiting for the document to be finished printing.

Each process in UNIX is identified by a PID (process identification number). When a background process is started off, UNIX will print its PID number in square brackets.

Now try to type the command

	ls  -l  >   dirlist&
NOTE that the shell prompt immediately returns, and a number in brackets is printed. The & character signals to the shell that the command is to run in the background.

	2868
	
This is the PID number of the background task. You can have more than one background task.

Sometimes, you may want to shut down a background task. The kill command is used to shutdown a task, and it accepts the PID number of the task to shutdown.


Miscellaneous

wc (word count)

This utility displays a count of the number of characters, words and lines in a file. Now try the command

	wc  temp
This prints out a list of the number of lines, words, and characters respectively.

The switches for this utility are

        
	-l      print line count
	-c      print character count
	-w      print word count

who

This command displays a list of the users of the system. Using the -u switch displays a list of those users currently logged into the UNIX server.

Now try the command,

	who -u

grep

The grep command searches a file for a pattern. The following command

	grep 'change' temp
searches the file temp in the current directory for the text string change. Using the * wildcard specifies all files, and using the switch -n will display on which line the text string is found.

time

This command times how long it takes to execute a given command. Now try the command,

	time  ls  -la
The command responds with the elapsed time during the command, the time spent in the system, and the time spent in execution of the command.

Exercise: How long does it take to execute the who command?

	.............................................

ps (process status)

This command reports on the active processes being run.

Now try the command,

	ps
You may even see the sleep process listed if it is still running in the background.


Compiling and Running your Programs

To start writing your program, use emacs to edit your C programs into one or several files. For example, suppose you have editted a file named hello.c, you can then compile this program inside emacs or in the command line by typing:
	gcc hello.c
This will produce a file named a.out, which can be executed by just typing:
	a.out

If a program contains multiple files, you should use the make facility. Alternatively, you can do it in the command line by typing

	gcc main.c foo.c bar.h bar.c
assuming your program contains four files main.c, foo.c, bar.h, and bar.c.

If you want the resulting executable to be named by names other than a.out, use the option "-o"

	gcc -o main main.c foo.c bar.h bar.c
the first string following "-o" is the file name for the resulting executable. Alternatively, you can always rename the a.out file to the file name you want (using the mv command).

Sometimes, even though your program contains multiple files (e.g., main.c, foo.c, bar.h, and bar.c), you still want to compile one at a time; with this, you can debug syntactic errors file by file. This is done via the "-c" option, typing

	gcc -c main.c 
        gcc -c foo.c 
        gcc -c bar.c
The resulting binary from the above compilations will be three object files called maino, foo.o and bar.o. You can then link them together by typing
        gcc -o main main.o foo.o bar.o


Surfing the Net

All the course-related information will be kept at the following World Wide Web site:
          http://zoo.cs.yale.edu/classes/cs210

You can access this home page by using netscape; just type

          netscape http://zoo.cs.yale.edu/classes/cs210
If you are working on character-based terminals, you can use lynx; type
          lynx http://zoo.cs.yale.edu/classes/cs210

Also, yale.cs.cs210 is a local newsgroup specifically for CS 210. Announcements and such will mostly show up on the course web page, but you may find the newsgroup handy for interacting with your fellow students, commiserating over the unreasonableness of the latest homework, and so forth.

You can also use netscape to browse yale.cs.cs210, or you can use rn to read unread news. To view unread articles in yale.cs.cs210 using rn, just type "rn yale.cs.cs210".

"man rn" will display the (very long) man page for rn. (The first time you use rn, it will display a brief introduction to its use.)

If you add the line "rn -c yale.cs.cs210" to your .login file, you'll be advised of unread articles in yale.cs.cs210 every time you log in.


Copyright (c) 1995, Zhong Shao, Dept. of Computer Science, Yale University

Updated for Fall '98 by Jim Aspnes