Table of Contents



Introduction 1 - What is UNIX 2 - Files and commands 3 - Text Handling 4 - The shell 5 - Unix Processes 5 - X windows 5 - Other Unix topics, Mail, utilities, .....
 
Contents
Next

Introduction

A Hands on class, and group therapy session, intended to remove the fear you hold deep within regarding this evil uncle Unix of yours. We hope to discover that beneath his dull green terminal, the exciting colorful guy that's been hidden there, waiting to fufill your wildest computing dreams.
 
Contents
Previous
Next

Your Lecturer

My background is mechanical engineering, which I did for about 15 years. During that time I learned to use computers, and eventually began using Unix machines around 1990 to run CAD/CAM software on. I wrote software during that period that enabled designers to solve various visual and computational problems unique to our product line. Eventually I knew too much about Unix and was sentenced to keep our network of 500 machines running. Realizing my fate, I fled to Smith College, where I do pretty much the same thing, but with a lot less machines and PHB's (Pointy Haired Boss's).
 
Contents
Previous
Next

Why this Class?

I have found that for a scientist or engineer, Unix is hard to beat for a computing platform. It's sort of a Swiss-Army-Knife computer. It's a abrupt, get-to-the-point kind of operating system, that was born in a research lab and never got the polish applied that the Windows world did. That's the bad thing about unix, it's hard to learn. It's also the good thing about Unix, which I hope you will begin to see by the end of this class.
 
Contents
Previous
Next

Which Books should I get?

Easy answer. Unix in a Nutshell by O'Reilly. This is the only Unix book you need. Forget all the others. All you need is a basic understanding of how the thing works and a few conventions understood and your on your way. That is the purpose of this class, to give you the confidence to sit at a terminal (or Xterm) and open your book and create solutions for yourself.


 
Contents
Previous
Next

1 - What is UNIX

 
Contents
Previous
Next

History of Unix

The Unix operating system found its beginnings in MULTICS, which stands for Multiplexed Operating and Computing System. The MULTICS project began in the mid 1960s as a joint effort by General Electric, Massachusetts Institute for Technology and Bell Laboratories. In 1969 Bell Laboratories pulled out of the project.

One of Bell Laboratories people involved in the project was Ken Thompson. He liked the potential MULTICS had, but felt it was too complex and that the same thing could be done in simpler way. In 1969 he wrote the first version of Unix, called UNICS. UNICS stood for Uniplexed Operating and Computing System. Although the operating system has changed, the name stuck and was eventually shortened to Unix.

Ken Thompson teamed up with Dennis Ritchie, who wrote the first C compiler. In 1973 they rewrote the Unix kernel in C. The following year a version of Unix known as the Fifth Edition was first licensed to universities. The Seventh Edition, released in 1978, served as a dividing point for two divergent lines of Unix development. These two branches are known as SVR4 (System V) and BSD.

Ken Thompson spent a year's sabbatical with the University of California at Berkeley. While there he and two graduate students, Bill Joy and Chuck Haley, wrote the first Berkeley version of Unix, which was distributed to students. This resulted in the source code being worked on and developed by many different people. The Berkeley version of Unix is known as BSD, Berkeley Software Distribution. From BSD came the vi editor, C shell, virtual memory, Sendmail, and support for TCP/IP.

For several years SVR4 was the more conservative, commercial, and well supported. Today SVR4 and BSD look very much alike. Probably the biggest cosmetic difference between them is the way the ps command functions.

The Linux operating system was developed as a Unix look alike and has a user command interface that resembles SVR4.

 
Contents
Previous
Next

The Connection Between Unix and C

At the time the first Unix was written, most operating systems developers believed that an operating system must be written in an assembly language so that it could function effectively and gain access to the hardware. Not only was Unix innovative as an operating system, it was ground-breaking in that it was written in a language (C) that was not an assembly language.

The C language itself operates at a level that is just high enough to be portable to variety of computer hardware. A great deal of publicly available Unix software is distributed as C programs that must be complied before use.

Many Unix programs follow C's syntax. Unix system calls are regarded as C functions.

What this means for Unix system administrators is that an understanding of C can make Unix easier to understand.

 
Contents
Previous
Next

Why Use Unix?

One of the biggest reasons for using Unix is networking capability. With other operating systems, additional software must be purchased for networking. With Unix, networking capability is simply part of the operating system. Unix is ideal for such things as world wide e-mail and connecting to the Internet.

Unix was founded on what could be called a "small is good" philosophy. The idea is that each program is designed to do one job well. Because Unix was developed different people with different needs it has grown to an operating system that is both flexible and easy to adapt for specific needs.

Unix was written in a machine independent language. So Unix and unix-like operating systems can run on a variety of hardware. These systems are available from many different sources, some of them at no cost. Because of this diversity and the ability to utilize the same "user-interface" on many different systems, Unix is said to be an open system.


 
Contents
Previous
Next

Unix Anatomy

As is illustrated here, Unix is a multi layered system. In a strictly physical sense the kernel, shell, and utilities rest on the hardware. Logically the system is arranged as the diagram shows.

Although the words "operating system" are frequently used to refer to the kernel, shells, and the utilities or commands. Technically utilities are not part of the operating system. Utilities that come with the operating system are basic tools that have evolved into standard Unix commands. They make the operating system more immediately useful to the user, but only the kernel and the shell are truly the operating system.

Each layer in the system can be thought of as a country with its own indigenous language. Hardware includes the physical parts of the system: monitor, mouse, printer, cables, chips, etc. The hardware layer and the user layer are monolingual. They can only talk to one other layer of the system. The hardware can converse with the kernel. The kernel is bilingual. It can talk to the hardware or to the shell. The shell is multi lingual and can talk to any part of the system with the exception of the hardware. The shell can be thought of as the master interpreter. It retrieves the file that holds instructions for every command that's typed in and it allows users to utilize the services of the kernel.

Unix utilities can converse with the shell and indirectly with the kernel. Utilities use functions called system calls to request services from the kernel. They cannot interact with the kernel directly.


 
Contents
Previous
Next

Using Man Pages

"The Unix approach to on-line documentation works fine if you are interested in documenting a few hundred programs and commands that you, for the most part, can keep in your head anyway. It starts to break down as the number of entries in the system approaches a thousand; add more entries, written by hundreds of authors spread over the continent, and the swelling, itching brain shakes with spasms and strange convulsions."
(The UNIX-HATERS Handbook)
Unix manual pages are created by the man command which finds, prints, and displays entries from the online reference manuals. Man pages are vendor specific by necessity, as individual commands and options to them may vary from operating system to operating system.

Man pages are organized into sections. The content of each section can vary slightly between vendors, however they are basically ordered as follows:

  1. User commands
  2. System calls
  3. C library routines
  4. Device drivers and network interfaces
  5. File formats
  6. Games and demos
  7. Misc. (ASCII, macro packages, tables, etc)
  8. Commands for system administration
  9. Locally installed manual pages. (i.e. X11)
The best way to determine the exact organization of a particular set of man pages is to peruse them.

The manual pages can be accessed via the man command. For example, entering, man ls, means that all sections of the manual pages are searched for information on the ls command. Searches can also be limited to a particular section, keyword, filename or combinations of these. Typing man man at the command line will print a list of all the options for man as well as instructions on how to do searches by section, keyword, filename, etc.

The X Window System uses xman, a graphical browsing tool for viewing man pages. With xman the user is presented with a listing of all available man pages and a "Sections" menu.

Because man pages are tersely written they can be most helpful for looking up things such as the options to or syntax of a command. They can also be useful for finding out what command performs a particular function, by using the function as the keyword for a keyword search to locate the command. Due to the style in which they're written, man pages are generally not a a good place to go for learning the basics of how a command works.

Tip: check out the whatis command, also the keyword (-k) option of the man command.


 
Contents
Previous
Next

Superuser

The superuser is a privileged user who has unrestricted access to the whole system; all commands and all files regardless of their permissions. By convention the username for the superuser account is root.

The root account is necessary as many system administration files and programs need to be kept separate from the executables available to non-privileged users. Unix allow users to set permissions on the files they own. A system administrator may need to override those permissions.

Access to the root account is restricted by a password. Because the superuser has the potential to affect the security of the entire system, it is recommended that this password be given only to people who absolutely need it, such as the system administrator. It is also a good idea to change the password on this account often. On BSD derivative systems, users who have access to the root account are frequently listed as members of group 0, also known as wheel.

There are several ways to log into the root account. If a system comes up in single user mode whoever is logged in automatically has root privileges. When a system is already up in multi user mode, a user can log in directly as root. This is not recommended as it is easy to take superuser privileges for granted and perform mundane tasks in this mode. When a user is already logged in, issuing the su command, without options, will cause the system to prompt for the root password. Once it is given the user becomes root.

The root account has its own shell and frequently displays a prompt that is different from the normal user prompt. If this is not the case, changing the default shell for the root account will change the prompt. Commands and programs that a system administrator will need as root are kept in /etc to decrease the chances of a user trying them by accident. For example, /etc contains /etc/passwd, which holds a list of all users who have permission to use the system.

Because the root account has extensive privileges it has an equal potential for destruction. This is amplified by the fact that safeguards built into some commands do not apply to this account. For example, the superuser may change another user's password without knowing the old password. The superuser can also mount and unmount file systems, remove any file or directory, and shut down the entire system. The root account should be used with caution and only when necessary to perform a given task. A misplaced keystroke in this mode can have disastrous results.



 
Contents
Previous
Next

2 - Files and commands

 
Contents
Previous
Next

Everything is a file

One of the unique things about Unix as an operating system is that regards everything as a file. Files can be divided into three categories; ordinary or plain files, directories, and special or device files.

Directories in Unix are properly known as directory files. They are a special type of file that holds a list of the other files they contain.

Ordinary or plain files in Unix are not all text files. They may also contain ASCII text, binary data, and program input or output. Executable binaries (programs) are also files, as are commands. When a user enters a command, the associated file is retrieved and executed. This is an important feature and contributes to the flexibility of Unix.

Links are a special file type that allows two files names/paths to actually point to the same file. They are a powerful feature.

Special files are also known as device files. In Unix all physical devices are accessed via device files; they are what programs use to communicate with hardware. Files hold information on location, type, and access mode for a specific device. There are two types of device files; character and block, as well as two modes of access.

Block device files are used to access block device I/O. Block devices do buffered I/O, meaning that the the data is collected in a buffer until a full block can be transfered.

Character device files are associated with character or raw device access. They are used for unbuffered data transfers to and from a device. Rather than transferring data in blocks the data is transfered character by character. One transfer can consist of multiple characters.

Some devices, such as disk partitions, may be accessed in block or character mode. Because each device file corresponds to a single access mode, physical devices that have more than one access mode will have more than one device file.

Device files are found in the /dev directory. Each device is assigned a major and minor device number. The major device number identifies the type of device, i.e. all SCSI devices would have the same number as would all the keyboards. The minor device number identifies a specific device, i.e. the keyboard attached to this workstation.



 
Contents
Previous
Next

Working with Files and Directories

The following topics will be discussed in this section:

The UNIX filesystem structure

All the stored information on a UNIX computer is kept in a filesystem. Any time you interact with the UNIX shell, the shell considers you to be located somewhere within a filesystem. Although it may seem strange to be "located" somewhere in a computer's filesystem, the concept is not so different from real life. After all, you can't just be, you have to be somewhere. The place in the filesystem tree where you are located is called the current working directory.

CONCEPT: The UNIX filesystem is heirarchical (resembling a tree structure). The tree is anchored at a place called the root, designated by a slash "/". Every item in the UNIX filesystem tree is either a file, or a directory. A directory is like a file folder. A directory can contain files, and other directories. A directory contained within another is called the child of the other. A directory in the filesystem tree may have many children, but it can only have one parent. A file can hold information, but cannot contain other files, or directories.

CONCEPT: To describe a specific location in the filesystem heirarchy, you must specify a "path." The path to a location can be defined as an absolute path from the root anchor point, or as a relative path, starting from the current location. When specifying a path, you simply trace a route through the filesystem tree, listing the sequence of directories you pass through as you go from one point to another. Each directory listed in the sequence is separated by a slash.

UNIX provides the shorthand notation of "." to refer to the current location, and ".." to refer to the parent directory. These two symbols actually are two hidden directories, the first being a link to the entries in the file structure for the current directory, and the second a link to the entries in the file structure in the directory just above it in the directory tree. These names make it easier to move through the directory tree and to describe paths relative to the current directory.

EXERCISE: Specify the absolute path to the directory named jon at the bottom of the tree diagram.

EXPLANATION: Since the absolute path must always begin at the root (/) directory, the path would be:

/users/admin/jon
EXERCISE: Specify the relative path from the directory named student to the directory named jon in the tree diagram.

EXPLANATION: Starting from the student directory, we would first have to move up the filesystem tree (using the .. notation) to the directory called users before we could descend to the directory called jon. The path would be:

../admin/jon

Changing Directories

In UNIX, your location in the filesystem heirarchy is known as your "current working directory." When you log in, you are automatically placed in your "home directory." To see where you are, type the command
pwd
which stands for "print working directory."

To change your location in the filesystem heirarchy, use the cd (change directory) command, followed by an argument defining where you want to go. The argument can be either an absolute path to the destination, or a relative path.

EXAMPLE: Type the command

cd /tmp
to go to the /tmp directory. You can type
pwd
to confirm that you're actually there.

If you type the cd command without an argument, the shell will place you in your home directory.

EXERCISE: Type the command

pwd
and note the result. Then type
cd ..
to the shell. Type
pwd
again to see where you end up.

EXPLANATION: The cd .. command should have moved you up one level in the directory tree, since .. is UNIX shorthand for the parent directory. The result of the second pwd command should be the same as the first, with the last directory in the path omitted.

File and directory permissions

CONCEPT: UNIX supports access control. Every file and directory has associated with it ownership, and access permissions. Furthermore, one is able to specify those to whom the permissions apply.

Permissions are defined as read, write, and execute. The read, write, and execute permissions are referred to as r, w, and x, respectively.

Those to whom the permissions apply are the user who owns the file, those who are in the same group as the owner, and all others. The user, group, and other permissions are referred to as u, g, and o, respectively.

A short note on groups: UNIX allows users to be placed in groups, so that the control of access is made simpler for administrators.

The meaning of file and directory permissions

Read permission
For a file, read permission allows you to view the contents of a file. For a directory, having read permission allows you to list the directory's contents.
Write permission
For a file, write permission allows you to modify the contents of the file. For a directory, write permission allows you to alter the contents of the directory, i.e., to add or delete files.
Execute permission
For a file, execute permission allows you to run the file, if it is an executable program, or script. Note that file execute permission is irrelevant for nonexecutable files. For a directory, execute permission allows you to cd to the directory, and make it your current working directory.

Viewing permissions

To see the permissions on a file, use the ls command, with the -l option.

EXAMPLE: Execute the command

ls -l /etc/passwd
to view the information on the system password database. The output should look similar to this:

-rw-r--r-- 1 root sys 41002 Apr 17 12:05 /etc/passwd

The first 10 characters describe the access permissions. The first dash indicates the type of file (d for directory, s for special file, - for a regular file). The next three characters ("rw-") describe the permissions of the owner of the file: read and write, but no execute. The next three characters ("r--") describe the permissions for those in the same group as the owner: read, no write, no execute. The next three characters describe the permissions for all others: read, no write, no execute.

Setting permissions

UNIX allows you to set the permissions on files that you own. The command to change the file permission mode is chmod. The chmod command requires you to specify the new permissions you want, and specify the file or directory you want the changes applied to.

To set file permissions, you may use the rwx (read, write, and execute) notation to specify the type of permissions, and the ugo (user, group, others) notation to specify who those permissions apply to.

To define the kind of change you want to make to the permissions, use the plus sign (+) to add a permission, the minus sign (-) to remove a permission, and the equal sign (=) to set a permission directly.

EXAMPLE: Type the command

chmod g=rw- ~/.cshrc
to change the file permissions on the file .cshrc, in your home directory. The tilde character, ~, is UNIX shorthand for your home directory. Specifically, you are specifying group read access and write access, with no execute access.

EXERCISE: Change the permissions on the .cshrc file in your home directory so that group and others have read permission only.

EXPLANATION: Typing the command

chmod go=r-- ~/.cshrc
would accomplish the task.

Listing the contents of a directory

The ls command allows you to see the contents of a directory, and to view basic information (like size, ownership, and access permissions) about files and directories. The ls command has numerous options, so see the manual page on ls (type man ls) for a complete listing. The ls command also accepts one or more arguments. The arguments can be directories, or files.

EXAMPLE: Type the command

ls -lR /etc/i*
to the UNIX shell.

In the example, the l and R options of the ls command are invoked together. Some commands permit you to group options in that way, and some commands require the options to be named separately, e.g., ls -l -r. The l option calls for a long output, and the R option causes ls to operate recursively, moving down directory trees.

The last part of the example, /etc/i*, directs the ls command to list files and directories in the /etc directory, that begin with the letter "i". The wildcard character, "*", matches any character(s).

EXERCISE: Type the command

ls -m /etc/i*g
to the shell. How did the shell respond, and why?

EXPLANATION: The shell responded by printing all the entries in the /etc directory that start with the letter "i", and end with the letter "g". The -m option causes the output to be streamed into a single line. See the manual page for ls to get a complete description of the ls command's options.

EXERCISE: Find the permissions on your home directory.

EXPLANATION: There are many ways to accomplish this. You could type

cd
to get to your home directory, and then type
ls -la
The -a option instructs the ls command to list all files, including those that start with the period character. The directory permissions are listed next to the . symbol. Remember that . is UNIX shorthand for the current working directory.

Viewing the contents of a file

CONCEPT: Text files are intended for direct viewing, and other files are intended for computer interpretation.

The UNIX file command allows you to determine whether an unknown file is in text format, suitable for direct viewing.

EXERCISE: Type the command

file /usr/bin/sh
to see what kind of file the shell is.

EXPLANATION: The shell is a shared executable, indicating that the file contains binary instructions to be executed by the computer.

The cat command
The cat command concatenates files and sends them to the screen. You can specify one or more files as arguments. The cat makes no attempt to format the text in any way, and long output may scroll off the screen before you can read it.

EXAMPLE: Send the contents of your .cshrc file to the screen by typing

cat ~/.cshrc
to the shell.

The more command
The more command displays a text file, one screenful at a time. You can scroll forward a line at a time by pressing the return key, or a screenful at a time by pressing the spacebar. You can also scroll up or down one line at a time by pressing the "k" and "j" keys respectively. You can quit at any time by pressing the "q" key.

EXAMPLE: Type

more /.cshrc
to the shell. Scroll down by pressing return, and by pressing the spacebar. Scroll up and down by pressing "j" and "k". Stop the more command from displaying the rest of the file by typing "q".

The head and tail commands
The head command allows you to see the top part of a file. You may specify the number of lines you want, or default to ten lines.

EXAMPLE: Type

head -15 /.cshrc
to see the first fifteen lines of the /.cshrc file.

The tail command works like head, except that it shows the last lines of of file.

EXAMPLE: Type

tail /.cshrc
to see the last ten lines of the file /.cshrc. Because we did not specify the number of lines as an option, the tail command defaulted to ten lines.

Copying files and directories

The UNIX command to copy a file or directory is cp. The basic cp command syntax is cp source destination.

EXAMPLE: The command

cp ~/.cshrc ~/pcopy
makes a copy of your .cshrc file, and stores it in a file called pcopy in your home directory.

EXERCISE: Describe the permissions necessary to successfully execute the command in the previous example.

EXPLANATION: To copy the .cshrc file, one must have read permission on the file. To create the new file called pcopy, one must have write permission in the directory where the file will be created.

Moving and renaming files

The UNIX mv command moves files and directories. You can move a file to a different location in the filesystem, or change the name by moving the file within the current location.

EXAMPLE: The command

mv ~/pcopy ~/qcopy
takes the pcopy file you created in the cp exercise, and renames it qcopy.

Removing files

The rm command is used for removing files and directories. The syntax of the rm command is rm filename. You may include many filenames on the command line.
CAUTION: Once the rm command has been executed on a file it is gone and cannot be retreived.
EXAMPLE: Remove the qcopy file that you placed in your home directory in the section on moving files by typing
rm ~/qcopy

Creating a directory

The UNIX mkdir command is used to make directories. The basic syntax is mkdir directoryname. If you do not specify the place where you want the directory created (by giving a path as part of the directory name), the shell assumes that you want the new directory placed within the current working directory.

EXAMPLE: Create a directory called temp within your home directory by typing

mkdir ~/temp
EXERCISE: Create a directory called bar, within the directory called temp, within your home directory.

EXPLANATION: Once the temp directory is created, you could just type

mkdir ~/temp/bar
Alternately, you could type
cd ~/temp; mkdir bar
In the second solution, two UNIX commands are given, separated by a semicolon. The first part of the command makes temp the current working directory. The second part of the command creates the bar directory in the current working directory.

Removing a directory

The UNIX rmdir command removes a directory from the filesystem tree. The rmdir command does not work unless the directory to be removed is completely empty.

The rm command, used with the -r option can also be used to remove directories. The rm -r command will first remove the contents of the directory, and then remove the directory itself.

CAUTION: When the rm -r command is executed on a directory the directory and all it's contents are removed and cannot be retreived.
EXERCISE: Describe how to remove the temp directory you created, using both rmdir, and rm with the -r option.

EXPLANATION: You could enter the commands

rmdir ~/temp/bar; rmdir ~/temp
to accomplish the task with the rmdir command. Note that you have to rmdir thebar subdirectory before you can rmdir the temp directory. Alternately, you could remove the temp directory with the command
rm -r ~/temp

 
Contents
Previous
Next

Introducing Redirection and Pipes

This is feature that comes from the shell. Our next session will focus on shells completely, but we will introduce it now to facilitate our working with our file manipulation commands.

Redirection is a shell feature that allows us to direct command output to files. By default report their results to a special file called Standard Output (stdout) which during an interactive session (your at a terminal) is mapped to the screen. Therefore if you type cat myfile the contents of myfile will be printed on the screen. If you type cat myfile > newfile nothing will appear on the screen, instead stdout will be redirected to newfile, either creating or replacing the contents of newfile. You can replace the > with >> which appends the contents to file (if it exists). The other special files we are concerned with are Standard Error (stderr) and Standard Input (stdin). Stderr is the file error messages are directed to from commands. This is also mapped to the screen by default. Stdin is assigned to the terminal by default as well. Therefore using our cat example: cat >& newfile will take input from stdin (the terminal)
and combine the stdout and stderr (if any) into the same file, newfile.

Pipes (|) make use of commands adherence to stdin and stdout. If you connect two or more commands like cmd1 | cmd2 , the stdout of cmd1 is redirected to the stdin of cmd2. You can make your own commands using this operator. Remember this is a shell feature, it is not the same kind of pipe that C programmers use for interprocess communication.

Example: ls | wc -w reports the number of files in a directory.


 
Contents
Previous
Next

Using Pipes and Filters

CONCEPT: UNIX allows you to connect processes, by letting the standard output of one process feed into the standard input of another process. That mechanism is called a pipe.

Connecting simple processes with a pipe allows you to perform complex tasks without writing complex programs.

EXAMPLE: Using the more command, and a pipe, send the contents of your .rhosts and .cshrc files to the screen by typing

cat .rhosts .cshrc | more
to the shell.

EXERCISE: How could you use head and tail with a pipe to display lines 25 through 75 of a file?

ANSWER: The command

cat file | head -75 | tail -50
would work. The cat command feeds the file into the pipe. The head command gets the first 75 lines of the file, and passes them down the pipe to tail. The tail command then filters out all but the last 50 lines of the input it received from head. It is important to note that in the above example, tail never sees the original file, but only sees the part of the file that was passed to it by the head command.

It is easy for beginners to confuse the usage of the input/output redirection symbols < and >, with the usage of the pipe. Remember that input/output redirection connects processes with files, while the pipe connects processes with other processes.

Grep

The grep (global regular expression and print) utility is one of the most useful filters in UNIX. The grep utility searches line-by-line for a specified pattern, and outputs any line that matches the pattern. The basic syntax for the grep command is grep [-options] pattern [file]. If the file argument is omitted, grep will read from standard input. It is always best to enclose the pattern within single quotes, to prevent the shell from misinterpreting the command.

The grep utility recognizes a variety of patterns. Here are some of the characters you can use to build regular expressions:

  • The carat (^) matches the beginning of a line.
  • The dollar sign ($) matches the end of a line.
  • The period (.) matches any single character.
  • The asterisk (*) matches zero or more occurrences of the previous character.
  • The expression [a-b] matches any characters that are lexically between a and b.
EXAMPLE: Type the command
grep "usr" /.cshrc
to search the /.cshrc file for any lines containing the string "usr".

EXAMPLE: Type the command

grep "^alias" /.cshrc
to see the lines in /.cshrc that begin with the character string "alias".

EXERCISE:List all the files in the /tmp directory owned by the user root.

EXPLANATION: The command

ls -l /tmp | grep 'root'
would show all file listings with the word "root" somewhere in the line. That doesn't necessarily mean that all the files would be owned by root, but using the grep filter can cut the down the number of files you will have to look at.

EXERCISE:List all the processes owned by the user root.

EXPLANATION: The command

ps -eaf | grep 'root'
would show all processes with the word 'root' somewhere in the line. Once again this doesn't necessarily mean that the process would be owned by root, but it does narrow down the possibilities.


 
Contents
Previous
Next

Commands - the Unix way

Unix is infamous for it's commands, zillions of commands, each one with zillions of options. For better or worse we need to understand some basic conventions regarding the commands we type.

Convention

  • Brevity - Nobody wants to type a lot, Unix commands are terse, 2 characters are good, three or four is plenty.
  • Lower case - Unix cares about the case of everything you type, unlike DOS. If you type ls you'll get a directory listing, if you typeLs or LS you'll get "command not found".
  • Flags - Commands have options that allow them to perform different tasks related to their function. These are called flags or sometimes switches. By convention they are prefixed with a - (ex. ls -l give a "long" directory listing).
  • Terse Output - Unix commands don't give you a lot of feedback. If you type rm myfile and hit enter, it just quietly deletes the file. This is intentional. Commands follow a convention of minimal formatting of output and quiet returns. This allows commands to be strung together using redirection and pipes to powerful effect. More on this in our discussion of shells.
  • man pages - All commands have a man page. This is a detailed document defining what the command does, explains all the flags, and refers you to related commands. I cannot overemphasize the importance of man pages. Use them!

 
Contents
Previous
Next


 
Contents
Previous
Next

Good Basic commands

The following is a list of basic commands you will get to know. We will explore most of these in our labs, and build some applications using them. Note, all of these commands work at a terminal and a text based. X windows is not required.

File Manipulation commands

This list is an essential group of commands that you should be familiar with. Of course ls and cd are an essential requirement to just get around the filesystem, but do you know what csplit does? There are quite a few other commands that could be listed here, but this list is enough to take you a long way.
cat more
cdmv
chmodpwd
cprm
csplitrmdir
filesplit
headtail
lntouch
lswc
mkdirscript

File Searching and Comparison

grepegrepfindcompdiff
dircmpsdiffcommstrings

Command text editing

cutfmtfoldjoinpaste
sortsedawktruniq

Interactive text editing

emacsvi



 
Contents
Previous
Next

3 - Text Handling

 
Contents
Previous
Next

Editing Text with EMACS

One of the most basic operations you will need to perform on a UNIX system is text editing. Whether you are preparing a document, writing a program, or sending email to a colleague, you will need a utility to allow you to enter and edit text.

There are many editors available for UNIX systems, but this discussion will focus on the emacs text editing program because of its power, flexibility, extensibility, customizability, and prevalence. No matter where you get a UNIX account, you are likely to have emacs at your disposal. Here is an index to the topics in this section:

General features of the emacs editor

Emacs is a visual editor. That means that you have a representation of your entire document on your screen, and you can move around freely, editing any part of the document you wish. Older editors, referred to as line editors, required all changes to the file to be made on a line-by-line basis. Each command to a line editor specified a line number, and the changes to be applied to that line. Line editors are truly horrible things, and you should feel lucky if you have never seen one.

Emacs prefaces commands with either a control character or meta character in order to distinguish editor commands from text to be inserted in the buffer. In this tutorial the notation "Control-X" means to hold down the Ctrl key, and type the letter "x". You don't need to capitalize the "x" or any other control character, by holding down the shift key. Meta characters vary depending on the system, usually it they are the Escape, Alternate, or Extended character keys. With the Esc key the notation "Meta-X" means to press the escape key down, release it, and then type "x". With the Alt or Extended char keys the notation "Meta-X" means to press down either of Alt or Extended char and simultaneously press type the letter "x".

Working with buffers

When you edit a file in emacs, you're not really editing the file itself, as it sits out on a disk somewhere. Instead, emacs makes a copy of the file, and stores the copy in a part of RAM memory called a buffer. All the changes you make to the file are applied to the buffer. When you save the file, emacs writes the contents of the buffer to the disk.

Because the buffer exists in RAM memory, it disappears if the power is turned off, or if the system crashes. Thus, you should use the save command often, flushing your current buffer to disk. Once the file is on disk, a power outage or system crash shouldn't harm it.

Basic operations in emacs

Here are some of the fundamental things you'll need to do when you edit a document in emacs.

Starting emacs

To start emacs, just type the command
emacs
to the UNIX shell. If you want emacs to start with a file already loaded into a buffer, type
emacs filename
where "filename" is the name of the file you want to edit.

Quitting emacs

To exit emacs and return to the UNIX shell, type Control-X-Control-C. If you have made changes to the buffer since the last time you saved it to disk, emacs will ask you if you want to save. Type y for yes, or n for no.

Getting help

Emacs has an on-line help system that can be invoked by typing Control-H. If you type the question mark (?), emacs will present a list of help topics you can choose.

The emacs display

The display in emacs is divided into three basic areas. The top area is called the text window. The text window takes up most of the screen, and is where the document being edited appears. At the bottom of the text window, there is a single mode line. The mode line gives information about the document, and about the emacs session. The bottom line of the emacs display is called the minibuffer. The minibuffer holds space for commands that you give to emacs, and displays status information.

Aborting a command

You can abort an emacs control or escape sequence by typing the command Control-G.

Working with files

To read a disk file into an emacs buffer, type the command Control-X-Control-F. Emacs will ask you for the name of the file. As you type the name of the file, it will be displayed in the minibuffer. When you have entered the file name, press the return key, and emacs will load the file into a buffer, and display it in the text window.

The command to save the contents of the buffer to a disk file is Control-X-Control-S. The save command overwrites the old version of the file. You may also write the contents of the buffer to a different file with the command Control-X-Control-W. Emacs will prompt you for the name of the file you want to create.

To create a new file, use Control-X-Control-F, just as if the file already existed. When emacs asks you for the file name, type in the name you want your new file to have, and emacs will create the file, and display an empty buffer for you to type in.

Emacs will perform file name completion for you. Type part of the name of the file you want, and press the spacebar or tab key to get emacs to complete a file name. If the partial name you've given matches more than one file, emacs will display a list of all potential matches. You can continue typing in more of the file's name, and pressing either file completion key, until you zero in on the file you want.

Cursor motion

On well-configured systems, you will find that the keyboard arrow keys will function correctly in emacs, moving you forward or backward one character at a time, and up or down one line at a time. If the arrow keys do not work, here's how to accomplish the same functions:
  • Control-F : moves the cursor forward to the next character.
  • Control-B : moves the cursor back to the previous character.
  • Control-N : moves the cursor to the next line.
  • Control-P : moves the cursor to the previous line.
In addition to basic cursor motion, emacs provides some other handy cursor motion functions:
  • Control-A : moves the cursor to the start of the current line.
  • Control-E : moves the cursor to the end of the current line.
  • Meta-F : moves the cursor forward to the next word.
  • Meta-B : moves the cursor back to the previous word.
  • Meta-< : moves the cursor to the start of the buffer.
  • Meta-> : moves the cursor to the end of the buffer.

Inserting and deleting text

To insert text into a buffer, place the cursor where you want to start inserting text, and start typing away.

If you want to insert the contents of another file into the current buffer, place the cursor at the desired insertion point, and type Control-X-I. Emacs will ask you for the name of the file you wish to insert.

You may also insert text by cutting it from one place, and pasting it at the insertion point. See the next section for information on cutting and pasting.

ild all the Khoro Deleting text is easy. As you'd expect, the delete key (or backspace key) deletes backward one character. Here are some other ways to delete text:

  • Control-D : deletes forward one letter.
  • Control-K : deletes from the point to the end of the line.
  • Meta-D : deletes forward one word.
  • Meta-delete : deletes backward one word.

Cutting and pasting text regions

Emacs allows you to select a region of text, and perform cut and paste operations on the region. It uses a temporary storage area called the "kill buffer" to allow you to store and retrieve blocks of text. There is only one kill buffer in emacs, which means that you can cut text from one document, and paste it into another.

To define a region of text, place the cursor at one end of the region and press Control-spacebar. That sets the mark. Then, move the cursor to the other end of the region. The text between the mark and the cursor defines the region.

To cut a region of text, and place it in the kill buffer, use the command Control-W (think of Wipe).

The paste command is Control-Y. It Yanks the block of text from the kill buffer, and places it where the cursor rests. The Control-Y command only retrieves the most recently-cut block of text.

You can paste in earlier cuts by pressing ESCAPE-Y. The ESCAPE-Y command, used repeatedly, will take you back through several previous text blocks that were cut. The ESCAPE-Y command does not work unless you type Control-Y first.

You may copy a region of text into the kill buffer without cutting it. Define the text block by setting the mark at one end, and moving the cursor to the other end. Then type ESCAPE-W.

Undoing changes

It is possible to undo the changes you have made to a file by entering the command Control-_. (That's Control-underscore. On some keyboards, you'll have to hold down both the control and shift keys to enter the underscore character.)

Many word processing programs can only undo the most recent command, but emacs remembers a long history of commands, allowing you to undo many changes by repeatedly entering the Control-_ code.


 
Contents
Previous
Next

Editing with vi

About vi

vi (pronounced vee-EYE, short for "visual") provides basic text editing capabilities. Three aspects of vi make it appealing. First, vi is supplied with all UNIX systems. You can use vi at other universities or any businesses with UNIX systems. Second, vi uses a small amount of memory, which allows efficient operation when the network is busy. Third, because vi uses standard alphanumeric keys for commands, you can use it on virtually any terminal or workstation in existence without having to worry about unusual keyboard mappings. As a point of interest, vi is actually a special mode of another UNIX text editor called ex. Normally you do not need to use ex except in vi mode.

Starting vi

To start vi, enter:
    vi filename RETURN
where filename is the name of the file you want to edit. If the file does not exist, vi will create it for you. You can also start vi without giving any filename. In this case, vi will ask for one when you quit or save your work.

Command Mode and Input Mode

vi has two modes, command mode and input mode. In command mode, characters you type perform actions like moving the cursor, cutting or copying text, or searching for some particular text. In input mode, you type to insert or overwrite text. When you start vi, it is in command mode.

To switch from command to input mode, press the "i" key (you do not need to press RETURN). vi lets you insert text beginning at the current cursor location. To switch back to command mode, press ESC. You can also use ESC to cancel an unfinished command in command mode.

Unfortunately, vi does not normally indicate which mode you are in. The next exercise turns on a mode indicator. If you are uncertain about the current mode, you can press ESC a few times. When vi beeps, you have returned to command mode.

TABLE 1.Cursor Movement Commands
------------------------------------------------------
Command Cursor Moves to
------------------------------------------------------
b beginning of previous word
w beginning of next word
e end of current/next word
0 (zero) or ^ beginning of line
$ end of line
( beginning of current/previous sentence
) beginning of next sentence
{ beginning of current/previous paragraph
} end of current paragraph
H top line on screen
M middle line on screen
L bottom line on screen

------------------------------------------------------

TABLE 2.Common Command Mode Functions
-------------------------------------------------------------
Command Function
-------------------------------------------------------------
x Delete only the current character
D Delete to the end of the line
yy Yank line (coipes contents of line to a buffer, for later pasting)
dd Delete the current line
dw Delete from the current character to the beginning
of the next word
p Paste contents of yank or delete buffer
G go to end of file
#G go to line number denotes by #, ex. 24G, moves you cursor to line 24
ZZ quit and save
/ find the string, ex. /eric will find the first orrurance of eric
n repeat the last find
. repeat the last command
u undo the results of the last change
-------------------------------------------------------------

TABLE 3.Common ex Mode Functions
-------------------------------------------------------------
Command Function
-------------------------------------------------------------
:w write the changes (save)
:q quit
:q! really quit, discard changes
:wq write and quit
:n1,n2d delete range of lines from n1 to n2
-------------------------------------------------------------

Changing file context

Typing these commands puts you into insert or edit mode. You see the text change. To go back to command mode remember to hit esc when done editing the text.

TABLE 3.Common ex Mode Functions
-------------------------------------------------------------
Command Function
-------------------------------------------------------------
i insert before the cursor
a append after the cursor
r replace a single character
R replace characters starting under the cursor
cw replace contents of the word under the cursor
-------------------------------------------------------------


 
Contents
Previous
Next

Batch Editors - sed, awk

Sed is a batch file editor. Sed stands for Stream oriented editor. It reads a file and a script of editing commands and produces it's output on stdout. Sed is very cryptic and hard to use. However it's worth knowing the basics of it's use, as it is an invaluable tool for editing files without user interaction, such as in a shell script. Your Nutshell book details the programs, so I will only give them topical treatment here.

Awk is a pattern matching program. I think awk stands for the initials of it's creators, not the awkward program as I used to call it. Awk is also a cryptic batch program like sed, but it excells at pattern matching and processing based on the patterns. Like sed it reads a file and a script of commands and produces it's output on stdout. Awk is an incredably powerful program and it supports a full programming environment with loops, arithmetic and string operations. Awk is often used to populate databases.

Both these programs use has been eclipsed by the Perl language. It's no coincidence that all of the capabilities of these two programs are fully embedded into Perl. I still use both sed and awk in shell programs.


 
Contents
Previous
Next

Text processing commands

These are the essential text editing commands.

Command text editing

cutfmtfoldjoinpaste
sortsedawktruniq

Interactive text editing

emacsvi



 
Contents
Previous
Next

4 - The shell

 
Contents
Previous
Next

Shell overview

The shell is an intermediary. It sits between your program and the kernel, and provides a environment for your program. In this section we will learn how to work with the interactive shell (at your terminal prompt), and program in the shell.

There are several shell available on most unix systems. Here is a quick rundown on them.

  • sh - the bourne shell, the first unix shell, and available on all unix systems
  • ksh - the korn shell, an enhanced version of the bourne shell
  • csh - C shell, patterned after the C language, now a standard shell on most unix systems
  • tcsh - an enhanced C shell
  • bash - the "Bourne again" shell, a GNU creation, freely available for all unix systems.
We will focus our efforts on the csh/tcsh since it is what we use at Smith College, and seems to be the most popular one in general use today.

 
Contents
Previous
Next

Interactive Use of the Shell

This section discusses tips and tricks to make your use of the shell more efficient.

File name completion

Both csh and tcsh will perform file name completion for you. You can type in a partial file name, and press the ESCAPE key for csh, or the TAB key for tcsh. The shell will then complete the name of the file for you. If no file exists that begins with the characters you typed, the shell will beep at you, and will not complete the name. If more than one file begins with the characters you typed, the shell will complete the name up to the point where the names differ. Then you can type additional letters to specify the file name you want, reusing the ESCAPE key if desired.

To enable file completion the following variable is set: set filec

Command name aliasing

Both csh and tcsh provide command name aliasing, to allow you to rename commands. Aliasing can save a lot of keystrokes if you must frequently issue the same lengthy command. The alias command requires two pieces of information: The command you wish to alias, and the alias you wish to use to refer to it.

EXAMPLE: To alias the "history" command to "hi" you could use the following command in the csh or tcsh shell

alias hi history
EXERCISE: Create an alias in the TC shell called "clean" that would remove any files from your home directory that have the extension .gif or .jpg.

EXPLANATION: The command

alias clean 'rm ~/*.gif; rm ~/*.jpg'
would work.

Command aliasing can be tricky. Surround the alias string with single quotes (') to prevent the shell from interpreting special characters. If you use command history substitution in an alias, use the backslash character (\) to escape characters that you don't want the shell to interpret.

EXAMPLE: This example, written for the C shell, creates an alias for the cd command, so that it stores the current location in a shell variable called old before it changes to the new location. It also creates a new command alias called back that allows us to go back to the previous location:

alias cd 'set old=$cwd; chdir \!*; pwd'

alias back 'set dirvar=$old; cd $dirvar; unset dirvar'
There are several things to note in the above example. The alias for cd has three parts: The first reads the current working directory from the shell variable cwd, and saves it in a shell variable called old. The second part uses history substitution and chdir to change the current location. The use of chdir prevents an "aliasing loop," where the cd command calls itself. The third part executes the pwd command to print the new location on the screen.

The alias for back also has three parts: The first part reads the previous location from the shell variable old, and stores it in a shell variable called dirvar. That is necessary because the new cd alias will change the value of old when we call it in the second part of the back alias. The third part cleans up our mess by unsetting the variable dirvar, removing it from the environment.

You can remove an alias using the unalias command. To remove the "clean" alias you created in a previous exercise, enter the command:

unalias clean

Command history substitution

The C shell and TC shell will keep an ordered list of the commands you have issued, and allow you to retrieve commands from the list. That facility, called command history substitution, makes it possible to reuse all or part of your previously issued commands. Each command on the list is given a command number, according to the order it was issued. You can view the command history list by issuing the command:
history
The exact mechanism of retrieving commands from the command history list depends on the shell you're using, and how you have customized your shell.

csh, and tcsh
The C shell allows you to recall previous commands in whole or in part. In the C shell, the history shell variable is used to specify the number of lines the shell will remember. The statement

set history=60
will cause the C shell to remember sixty commands.

To recall previous commands from the history list, the C shell uses the exclamation point (!) character, sometimes referred to in computer jargon as "bang." The bang character can be used in combination with history line numbers, and text patterns. Here are some examples of how to use history substitution in the C shell:

Recall the last command:
!!
Recall the third most recent command:
!-3
Recall command number ten from the history list:
!10
Recall the last command that began with the letters "ls":
!ls
You can also recall specific pieces of previous commands, and use them to create new commands. The colon character is used to select specific words from a command. Each word in the command is referred to by position. The command name itself is item number zero. The dollar sign represents the last word in this case. Here are some examples:
Recall the third word from the last command:
!:2
Perform an "ls" on the second word from command number 8:
ls !8:1
Use more to view the last item from command number ten:

more !10:$

 
Contents
Previous
Next

Customizing the UNIX Shell

Table comparing shell features
FeatureFunctionshcshtcsh
Job controlAllows processes to be
run in the background
NoYesYes
History substitutionAllows previous commands to be
saved, edited, and reused
NoYesYes
File name completionAllows automatic completion of
partially typed file name
NoYesYes
Command line editingAllows the use of an editor to
modify the command line text
NoNoYes
Command aliasingAllows the user to rename
commands
NoYesYes

Choosing your shell

It is possible to invoke any available shell from within another shell. To start a new shell, you can simply type the name of the shell you want to run, tcsh, csh, or sh.

It is also possible to set the default startup shell for all your future sessions. The default shell for your account is stored in the system database /etc/passwd, along with the other information about your account. To change your default shell, use the chsh command The chsh command requires one argument, the name of the shell you want as your default. To change your default shell to the C shell, you could enter the command

ypchsh /usr/bin/csh

Default file access permissions

Whenever you create a file or directory in a UNIX filesystem, the newly created file or directory is stamped with a default set of permissions. That default set of permissions is stored in a variable called the umask. You can change the value of umask to suit your preferences. To see the current value of the umask variable, enter the shell command:
umask
The umask is stored as an octal (base 8) number, that defines which permissions to deny. As you recall, three kinds of file permissions (read, write, and execute) are given for each of three classes of users (owner, group, and others). Each of the nine permissions is specified as a zero (allow access), or a one (deny access).

To set your umask to deny write permission to group and others, use the command

umask 022
To deny all access to group and others, use the command
umask 077

Customizing with user login scripts

Below is an example login script, .login from an IRIX system.

# user login file for IRIX
#

if (! $?ENVONLY) then
# Set the interrupt character to Ctrl-c and do clean backspacing.
if (-t 0) then
stty intr '^C' echoe
endif

# Set the TERM environment variable
eval `tset -s -Q`
endif

# Set the default X server.
if ($?DISPLAY == 0) then
if ($?REMOTEHOST) then
setenv DISPLAY ${REMOTEHOST}:0
else
setenv DISPLAY :0
endif
endif

Customizing with Csh Resource file

The resource file is the .cshrc file. Below is a example one from an IRIX system.

# .cshrc file for IRIX (SGI)
#

# list directories in columns
alias ls 'ls -C'
alias mail Mail
alias passwd yppasswd # always use yppasswd
alias chfn ypchfn # always use ypchfn

# Remember last 100 commands
set history = 100

# For interactive shells, set the prompt to show the host name and event number.
if ( (! $?ENVONLY) && $?prompt ) then
if ( -o /bin/su ) then
set prompt="`hostname -s` \!# "
else
set prompt="`hostname -s` \!% "
endif
endif

 
Contents
Previous
Next


The Shell Environment

What are Environment Variables?

CONCEPT: The exact behavior of commands issued in the shell depends upon the execution environment provided by the shell.

The UNIX shell maintains a set of environment variables that are used to provide information, like the current working directory, the type of terminal being used, and the programs you run. The environment variables are passed to all programs that are not built in to the shell, and may be consulted, or modified, by the program. By convention, environment variables are given in upper case letters.

To view all the environment variables, use the command

env
You can also view a particular environment variable using the echo command:
echo $TERM
The above command echos the value of the TERM environment variable to the standard output.

The creation of the execution environment

When you log in, a sequence of events establishes the execution environment. The exact sequence of events depends on the particular flavor of UNIX, and also depends upon the default shell for your account. You are first prompted to enter your username. After you have entered your user name, it will prompt you to enter your password. Your password is encrypted and compared with the encrypted password associated with your username. (Note that login will request your password even if there is no entry in /etc/passwd for the username you've given. That prevents someone from finding valid usernames by trial and error.) Login turns off terminal echo so that your password is not displayed on the screen.

Having verified your password, login then uses information in /etc/passwd to invoke your default shell. If no default shell is specified in the /etc/passwd entry, login starts the Bourne shell (/bin/sh).

Shell startup: System login scripts
When the shell program starts, it reads configuration files called login scripts to configure the execution environment. The files /etc/.login and /etc/cshrc is used for csh on an IRIX system. After the system login scripts are read, the shell looks for user-specified login scripts.
Shell startup: User login scripts
After the system login scripts are read, the shell reads user login scripts. User login scripts are kept in one's home directory, and are the means by which one can customize the shell environment. Sh and ksh look for a file called .profile. Ksh also reads a file defined in the environment variable ENV. Csh and tcsh read a file called .cshrc, and (if it is the login shell), the file .login.

Important environment variables

Here are descriptions of some of the most important environment variables, and examples of how some variables can affect the execution of commands.
TERM
The TERM environment variable defines the type of terminal that you are using. Most UNIX systems have a database of terminal types, and the capabilities of each terminal type.
PATH
The PATH variable contains the names of directories to be searched for programs that correspond to command names. When you issue a command to the shell, the shell searches sequentially through each directory in the PATH list until it finds an executable program with the command name you typed.
USER
The USER variable contains your username. Any time you access a file or directory, the access permissions are checked against the value of USER.
HOME
The HOME variable contains the name of your home directory. When you issue the cd command with no directory argument, you will be placed in the directory defined in the HOME environment variable. The HOME variable is also where the shell will look for the user login scripts.
MAIL
The MAIL variable contains the name of the directory where your incoming mail is stored. When you start a mail program, the program will look in the directory stored in the MAIL environment variable for your incoming mail messages.
EDITOR
The EDITOR variable is used by programs that must invoke a text editor to provide the ability to edit or compose documents. One example is the elm program, which is used to read and send electronic mail. If you elect to compose a new mail message while in elm, the elm program will check the contents of the EDITOR variable to determine which editor to invoke.
HOST
The HOST environment variable contains the name of the host machine that is running your shell program. When you connect to a remote host through telnet or ftp, the name of your host is relayed to the remote machine, so the administrators of the remote machine can keep track of who is connecting, and from where.

Setting environment and shell variables

The exact mechanism for setting the environment and shell variables depends upon the type of shell you're using.

sh, or ksh
To set an environment variable in sh or ksh, use the syntax VAR=value;export VAR, where VAR is the name of the environment variable and value is the value you wish to assign. Do not put spaces on either side of the equals sign. The export command instructs the shell to propagate the value of the variable to all programs that are run by the shell. If an environment variable is reset, but not exported, the change will only apply to the shell itself. To set the EDITOR variable to the value emacs in ksh or sh, use the command:

EDITOR=emacs;export EDITOR
It is also possible to unset environment variables, with the unset command. Unsetting an environment variable removes the definition of the variable.

csh, or tcsh
To set an environment variable in csh, use the setenv command. The command has the syntax: setenv VARIABLE value. To set the EDITOR variable to the value emacs in csh, use the command:

setenv EDITOR emacs
For more information about the shell environment, consult the manual page for the shell you're using.

 
Contents
Previous
Next

Shell Programming

Whats a shell program?

So far we've mostly been refering to interactive shells that are attached to a terminal. All the shell features, job control, environment variables are available along with programming control statements in a batch environment. So what is a shell program? It's simply a text file containing commands that are either unix programs (like grep) and shell directives (like SETENV). The special thing that makes the file recognized by the kernel is the first line, which looks like: #!/bin/csh . Lets talk about this special character set #!.

The magic #!

This is a really powerfull function of the Unix operating system and is worth explaining at this point. If the execute bits are set on a file (--x--x--x) then if you type the file name in a shell the shell will pass the filename to the kernel to execute. The kernel will then interpet the file to see what it is. It can be one of two things, a compiled binary file (a C program for example), or a plain text file. If it's a plain text file, the kernel opens a child process for it and hands it to either a Bourne shell, if no #! record is found, or it replaces the child process with the program listed on the #! statement.

So in the case of the C shell script, the kernel starts a csh because of the #!/bin/csh directive. It also passes the contents of the line after the #! as arguments. The C shell reads them, (cause it's written that way) and excutes the subsequent lines in the file as if you were typing them at a terminal.

Don't fall asleep now! Think about this for a minute. There's nothing magic about the C shell. It's just a C program someone wrote. You could write your own program that reads some special form of input and run it the same way. Cool!

Example: I create my own plotting program (plot_vector).
Lets say I write a program that plots vectors in 2D space. I create it to sit in a loop forever waiting for commands from stdin. The commands It knows about are start plot, move x,y, draw x,y, plot, exit. Now This program works great interactively. To allow myself to script it I would change the program so it checks it commands and ignores things starting with #!, then write a script like such:

#! /home2/ejensen/bin/plot_vector
start plot
move 0, 0
draw 0, 1
draw 1, 1
draw 1, 0
draw 0, 0
plot
exit

Would run the program and plot a nice square for me.


C shell Constructs

Rather than repeat the Nutshell book here, I will simply show a couple examples of C shell scripts for discussion.

Example 1: Recursively update .cshrc files in peoples directories
#!/bin/csh
# recurively finds file name and modifies it
# Run from the students directory
# EJJ 10/13/98
#
set list=`find . -name .cshrc.local -print`
foreach one ($list)
echo Processing $one
echo umask 022 >> $one
end
echo .....done

Example 2: Archive a bunch of users home accounts, based on a list input

#!/bin/csh
#
# program to compress and archive on line inactive user accounts
# reads the name of a list file from argv[1]
# Eric Jensen 10/97
#
if ($#argv < 1) then
echo "usage: del_dead_acct "
echo " list_filename = a file containing a list of users to archive"
exit 1
endif
echo "Started archive of users...." > /tmp/dead_log
awk -F: '{print $6}' /etc/passwd > /tmp/tmp1$$
set list=`cat $argv[1]`
foreach usr ($list)
echo $usr
set home=`grep $usr /tmp/tmp1$$`
echo $home
tar cvf - $home | gzip > /home2/dead_accts/$usr.tar.gz
if ($status != 0) exit 1
echo $usr >> /tmp/dead_log
end
echo ".... ended archive" >> /tmp/dead_log
rm /tmp/tmp1$$



 
Contents
Previous
Next

5 - Unix Processes

 
Contents
Previous
Next

How they work - fork and exec

fork and exec are system calls (program calls that request services from the kernel) that create new processes.

A quick overview is needed at this point. When you are typing at the prompt, you are in a shell. When you enter a command, the shell forks itself (creates, "spawns" a duplicate), and the new shell execs (replaces itself) with whatever program you asked it to run. When that command terminates that shell dies. This is how every program on a Unix system runs.


 
Contents
Previous
Next

Interaction and Job Control

When you log in to a UNIX system, the kernel starts a shell for you, and connects the shell to your terminal. When you execute a command from the shell, the shell creates a child process to execute the command, and connects the child process to your terminal. By connecting the child process to your terminal, the shell allows you to send input to the child process, and receive output from it. When the child process finishes, the shell regains access to the terminal, redisplays the shell prompt, and waits for your next command.

Any task that requires you to actively participate (like word processing) must be in the foreground to run. Such jobs, termed "interactive," must periodically update the display, and accept input from you, and so require access to the terminal interface.

Other jobs do not require you to participate once they are started. For example, a job that sorts the contents of one file and places the results in another file, would not have to accept user commands, or write information to the screen while it runs. Some UNIX shells allow such noninteractive jobs to be disconnected from the terminal, freeing the terminal for interactive use.

Note that it is even possible to log out, and leave a background process running. Unfortunately, there is no way to reconnect a background job to the terminal after you've logged out.

Jobs that are disconnected from the terminal for input and output are called "background" jobs. You can have a large number of background jobs running at the same time, but you can only have one foreground job. That is because you only have one screen, and one keyboard at your terminal.

Background and foreground jobs

The process that is connected to the terminal is called the foreground job. A job is said to be in the foreground because it can communicate with the user via the screen, and the keyboard.

A UNIX process can be disconnected from the terminal, and allowed to run in the background. Because background jobs are not connected to a terminal, they cannot communicate with the user. If the background job requires interaction with the user, it will stop, and wait to be reconnected to the terminal.

Jobs that do not require interaction from the user as they run(like sorting a large file) can be placed in the background, allowing the user to access the terminal, and continue to work, instead of waiting for a long job to finish.

Starting a job in the background

To place a job in the background, simply add the ampersand character (&) to the end of your shell command.

EXAMPLE: To sort a file called "tempfile", and place the results in a file called "bar", you could issue the command

sort < tempfile > bar &

Examining your jobs

The UNIX command jobs allows you to see a list of all the jobs you have invoked from the current shell. The shell will list the job ID of each job, along with the status (running, stopped, or otherwise), and the command that started the job. The shell considers the most recently-created (or most recently-manipulated) job to be the current job, marked with a plus sign. Other jobs are referred to as previous jobs, and are marked with a minus sign. The commands related to job control will apply to the current job, unless directed to do otherwise. You may refer to jobs by job ID by using the percent sign. Thus, job 1 is referred to as %1, job 2 is %2, and so forth.

Suspending the foreground job

You can (usually) tell UNIX to suspend the job that is currently connected to your terminal by typing Control-Z (hold the control key down, and type the letter z). The shell will inform you that the process has been suspended, and it will assign the suspended job a job ID.

There is a big difference between a suspended job, and a job running in the background. A suspended job is stopped, and no processing will take place for that job until you run it, either in the foreground, or in the background.

Placing a foreground job into the background

If you started a job in the foreground, and would like to place it in the background, the first thing you must do is suspend the job with a Control-Z, freeing your terminal. Then, you can issue the UNIX command
bg
to place the suspended job in the background. The bg command can accept a job ID as an argument. If no job ID is given, bg assumes you are referring to the current (suspended) job.

Bringing a background job to the foreground

You can reconnect a background job to your terminal with the UNIX command
fg
The fg command will accept a job ID as an argument. Make sure to include the percent sign:
fg %2
will bring job 2 into the foreground. If no job ID is given, fg will assume you are referring to the current (suspended) job.

Starting a suspended job

If you have a suspended job that you'd like to resume running, first you must decide whether you want it running in the foreground, or the background. Find the job ID of the suspended job with the jobs command, and then use bg (to run the job in the background), or fg (to run the job in the foreground).

EXAMPLE:Start several jobs running in the background by entering the following:

xterm&
emacs&
View job status by entering the jobs command. Kill the emacs job by entering
kill %job number associated with emacs
View new job status by entering the jobs command. Suspend the xterm by bringing it to the foreground with
fg %job number associated with xterm
and suspend it by entering Ctrl-z. View job status by entering the jobs command. Kill the xterm job in similar fasion as the previous emacs job and then enter view job status one more time using the jobs command.

 
Contents
Previous
Next

Tracking and managing processes

Every process on the system is identifiable, and can be managed with the ps and kill commands.

ps

The ps or Process Status command queries the system for a list of proccess that exist on it at a givven moment. It has many flags which tailor it's output. This is one of the few Unix commands that varies in implementation form the BSD and SysV Unix derivatives. Check the Man page to be sure of the options available. Below is a fragment of a process list made on an SGI system using the ps -ef command.

UID PID PPID C STIME TTY TIME CMD
root 3816 141 0 - - 0:00
root 1 0 0 Nov 13 ? 0:05 /etc/init
root 2962 1 0 Nov 16 ? 0:01 xwsh -name winterm -name winterm
ejensen 2977 2962 0 Nov 16 pts/5 0:00 -tcsh
root 439 1 0 Nov 13 ? 0:00 /usr/etc/lpd
root 454 1 0 Nov 13 ? 0:09 /sbin/cron
ejensen 2999 2923 0 Nov 16 pts/3 0:00 telnet grendel
ejensen 3022 2977 0 Nov 16 pts/5 0:00 telnet hermite
root 3032 2850 0 Nov 16 ? 0:00 /usr/sbin/soundscheme
root 71 1 0 Nov 13 ? 0:01 /usr/etc/syslogd
root 136 1 0 Nov 13 ? 0:03 /usr/etc/routed -h -Prdisc_interval=45
root 141 1 0 Nov 13 ? 0:01 /usr/etc/portmap
root 566 1 0 Nov 13 ? 3:51 /usr/etc/mediad -n

Here it is again formatted with a little C program called pstree.

-+- 00001 root /etc/init
|--- 00439 root /usr/etc/lpd
|--- 00454 root /sbin/cron
|--- 00071 root /usr/etc/syslogd
|--- 00136 root /usr/etc/routed -h -Prdisc_interval=45
|-+- 00141 root /usr/etc/portmap
| \--- 03816 root
\-+- 02962 root xwsh -name winterm -name winterm
\-+- 02977 ejensen -tcsh
\--- 03022 ejensen telnet hermite

Do you see the parent/child relationship here? What do think the "defunct" process is?


kill

Now that we can identify processes on the system, we can send them a signal if need be with the kill command.

So in the previous example:
ejensen 3022 2977 0 Nov 16 pts/5 0:00 telnet hermite

If I issue the command kill -9 3022 my telnet session is abruptly canceled. Now if you've used Unix at all you've probably seen this command , and used it yourself to get out of a tight spot. But what's really happening here?

I you review the man page for kill you will discover that it sends a signal to the process. Signals are a communications mechanisim that allow two seperate processes that exist in their own private address space to send messages to each other. The kernel is the broker for these signals and programs use system calls to send and catch these signals.

So in the telnet example: I used the kill command, which is a general purpose signal sender, to send the -9 or SIGKILL, to the telnet process 3022. The telnet program was written to listen for signals and respond to certain ones in certain ways. It recieved the SIGKILL signal from me and honored it by aborting ungracefully right away. This is a "brute force" last resort type of use of the kill command. But there are many other signals available to send to a process, and many Unix programs are written to respond to these signals.

Table of signal values (from signal(5) man page)
SIGHUP 1 Exit Hangup [see termio(7)]
SIGINT 2 Exit Interrupt [see termio(7)]
SIGQUIT 3 Core Quit [see termio(7)]
SIGILL 4 Core Illegal Instruction
SIGTRAP 5 Core Trace/Breakpoint Trap
SIGABRT 6 Core Abort
SIGEMT 7 Core Emulation Trap
SIGFPE 8 Core Arithmetic Exception
SIGKILL 9 Exit Killed
SIGBUS 10 Core Bus Error
SIGSEGV 11 Core Segmentation Fault
SIGSYS 12 Core Bad System Call
SIGPIPE 13 Exit Broken Pipe
SIGALRM 14 Exit Alarm Clock
SIGTERM 15 Exit Terminated
SIGUSR1 16 Exit User Signal 1
SIGUSR2 17 Exit User Signal 2
SIGCHLD 18 Ignore Child Status Changed
SIGPWR 19 Ignore Power Fail/Restart
SIGWINCH 20 Ignore Window Size Change
SIGURG 21 Ignore Urgent Socket Condition
SIGPOLL 22 Exit Pollable Event [see streamio(7)]
SIGIO 22 Exit input/output possible signal
SIGSTOP 23 Stop Stopped (signal)
SIGTSTP 24 Stop Stopped (user) [see termio(7)]
SIGCONT 25 Ignore Continued
SIGTTIN 26 Stop Stopped (tty input) [see termio(7)]
SIGTTOU 27 Stop Stopped (tty output) [see termio(7)]
SIGVTALRM 28 Exit Virtual Timer Expired
SIGPROF 29 Exit Profiling Timer Expired
SIGXCPU 30 Core CPU time limit exceeded [see getrlimit(2)]
SIGXFSZ 31 Core File size limit exceeded [see getrlimit(2)]
SIGCKPT 33 Ignore Checkpoint warning [see cpr(1)]
SIGRESTART 34 Ignore Restart warning [see cpr(1)]
SIGRTMIN 49 Exit POSIX 1003.1b SIGRTMIN
SIGRTMAX 64 Exit POSIX 1003.1b SIGRTMAX

Lots of programs use these signals behind the scenes to work efficiently. Example - printing. A program runs all the time in the background named lpd. Lpd listens for print requests and manages a queue and spools the jobs to the printer. Now it would be very inefficient to have lpd activly scanning the print queue for a print job, so lpd is written to sleep and do nothing until it gets woken up by, in older Unix systems, a signal sent from the lpr command. Actually in newer Unixes, programs like this use sockets, another form of interprocess communication to do the job.





 
Contents
Previous
Next

5 - X windows

 
Contents
Previous
Next

Why Use a Graphical User Interface (GUI)?

Before we study X Windows in detail it is worth considering why we need GUIs.

GUIs provide an easy means of data entry and modification. They should provides an attractive and easy to use interface between human and machine. So easy in fact that a non-computer literate person could use the system.

GUIs provide a better means of communication than cumbersome text-based interfaces.

How do GUIs provide such facilities? By means of:

  • Extensive use of visual control items - Buttons, menus, icons, scroll bars etc.
  • Data can be manipulated intuitively on screen.
  • If a standard GUI is adopted then consistency of use across platforms and applications is afforded. Nearly all MS Windows (and Apple Macintosh) have the same look and feel so learning time for a new application is lessened.
  • Multiple applications can be run simultaneously on most machines these days. GUIs provide better screen management of such processes - we can assign one (or more) window to each application.
Although GUIs provide some very powerful advantages, there are a couple drawbacks to the GUI approach:
Efficiency
- As we will shortly see even the most basic windowing program can be quite large. (hello world prog will about 2 pages long!!.) This is because we will have to write or call upon many functions to control the windowing system - e.g. create, move, resize etc. windows; handle input via mouse and keyboard actions; control graphics.
Programming
- a different approach to programming is needed from the command-line approach. You have been used to top down structured programming approach. We will have to adopt a different strategy known as event-driven programming where the actions in our program will be triggered by mouse and keyboard actions.

 
Contents
Previous
Next

X Window System - Background

Developed at the Massachusetts Institute of Technology in 1984, X provides a graphics- window system for computer networks. Every computer network requires a system that will allow users to communicate with it. X provides such a system and employs graphics windows as its foundation. Each graphics window represents a particular application, such as a text editor, a debugger, an electronic mail manager, or a terminal emulator. X lets you open multiple windows and run several applications simultaneously.

The activity within one window executes independently of the activity in the other windows. For example, you can compile a FORTRAN program in one window, read electronic mail in another, run a clock in a third, and run a text editing program in a fourth window. This feature makes X very powerful because you can access many different applications--- even different computers---from a single terminal; you simply open new windows as needed.

First, and foremost, however, X is a windowing system, not a complete operating system. Unix is an operating system. X mediates the communication between individuals and a computer network. X creates windows that represent applications, but does not execute the applications itself. In the current Information Systems configuration, applications most often run on computers separate from an individual's computer terminal.

You can tailor X to suit your needs. For example, you can tell X to run certain applications automatically each time you start an X session. That saves you the trouble of starting them manually every time you log in.

The first popular and widely circulated release of X was X11R4. Many X window systems shipped by Unix vendors are based on X11R5. The current Open Group X11 Release is X11R6.3.


 
Contents
Previous
Next

X Window System - The Basics

As you can guess, the X Window System is a graphical user interface (GUI). The impressive thing about X is that it's network-extensible. This means that you don't have to sit in front of a computer's physically connected console to enjoy the GUI features (as is usually the case). X can be (and often is) run over the network to a remote computer's console (provided that the remote computer is also running X). The remote computer can be situated down the hall from you or across the world; to X it makes no difference.

Although X window system is used most frequently on Unix workstations, it can be used on almost any computer. X servers are now commonly available for Windows and the Mac environment.

Just as there are different "flavors" of Unix there are different X-like systems. These are vendor specific window systems that are based on MIT's X11, which is the original X. These systems have been configured by the vendor to work with a particular Unix "flavor". Each of these has a unique look and feel. The two major X variants are Motif and OPEN LOOK. OpenWindows, a graphical user environment based on the OPEN LOOK specification, is part of Sun's Solaris system. Almost everyone else uses a system based on Motif. However, since even Solaris now comes with the Common Desktop Environment (which is based on Motif), it appears that Motif may ultimately emerge as the victor.

 
Contents
Previous
Next

The X Protocol

X is a client-server protocol and it is the protocol which sets the standard for X. This protocol pertains to the way the client and the server communicate with each other at a low level. It has no effect on the way X will look on a terminal screen. This means that a number of user interfaces that all look different can still be called X. The look is determined by which utilities (called widgets and toolkits) a programmer used to develop the interface.
 
Contents
Previous
Next

X is Client-Server

However, the manner in which the terms client and server are used is a bit skewed in X. We will explore this more fully in the next section.
 
Contents
Previous
Next

Window Manager

Every X system uses a window manager. The window manager gives each window a title bar, border and makes it possible to resize, move and iconify windows. There are several window managers. The most common are the Tab Window Manager ( twm) from MIT, OPEN LOOK Window Manager (olwm) from UNIX International, Motif Window Manager (mwm) from the Open Software Foundation, and fvwm from FSF(?).

A window manager is an application program and not part of the window system. This means that the Motif Window Manager can be used with OPEN LOOK, etc. A single X server can provide access to several different window mangers.

Generally a window manager is initiated as part of a user's startup script as is the X system. However, both can be be started from the command line. The startup script is what pulls together all the elements that comprise a user's X environment. The startup script for X includes initialization files, resource files and startup files for the window manager.

 
Contents
Previous
Next

Startup Scripts

The startup script is known either as $HOME/.xsession when xdm, the X Display Manager, is used to begin an X session or as $HOME/.xinitrc if xinit, the X Window System initializer, is used. Although there are some differences between the scripts, the conventions for configuring them are the same. For the rest of this module xinit and .xinitrc will be used as examples.

The first thing xinit does is to find the initialization file .xinitrc and execute the commands found in it. It is the commands in this file that determine what programs will be started every time X starts. xinit terminates when the last command in .xinitrc has been executed. So the command to start the window manager is generally the last command in the file.

Resources are the variables used by a specific X client. They are read from the .xresources, by the xrdb program, as part of the startup script. The .xresources file contains information on the configuration of various X applications such as the size of windows, text fonts and color selection.

The startup files for the window manager are where the manager can be modified to suit the specific needs of a user. These preferences are stored in the initialization files for the window manager. In these files a user can create personalized menus to specify such things as what pressing each of the mouse buttons will do. All window managers have a file that holds the standard menus. If no special menus exist the window manager will use the standard menus.

 
Contents
Previous
Next

Common X Clients (Programs)

Common X programs include; xcalc, xclock, xwininfo and xterm. The first two are self explanatory, being a calculator and a clock. xwininfo brings up a "Window Information" window that displays information such as height, width and depth for any window the user selects with the mouse.

The xterm program is a terminal emulator. It presents the user with a window that functions as a pseudo-terminal. This means that each xterm window can have a different program or programs running in it. When a window is created it is controlled by the shell specified in the SHELL environmental variable. Unix commands can be entered in this shell as can commands to call up other X clients.

On SGI and HP workstations, the window environment is the first screen the user sees once the system is up. On Sun workstations xinit or openwin must be used to call up the OPEN LOOK environment, unless the system is running Solaris and CDE has been installed.


 
Contents
Previous
Next

X Window System - Setting the Display

The X window system has a special environment variable called DISPLAY (or its command line equivalent -display) which is used to tell a host (or a specific X client) the name of the host whose display to use.

Let us consider the following example: Hosts foo.indiana.edu and bar.indiana.edu are connected via a TCP/IP network and have the X environment installed. You are sitting in front of bar's console. You can telnet to foo and set the DISPLAY environment variable like this:

    foo% DISPLAY=bar.indiana.edu:0 (for Bourne style shells)
    foo% export DISPLAY

    or

    foo% setenv DISPLAY bar.indiana.edu:0 (for csh style shells)

You now start an X client (any X client) on bar (let's assume you run xterm):
    foo% xterm &
and the xterm window pops up on bar's console in front of you This happens because you told foo to display all X clients to bar by setting the DISPLAY environment variable. (Actually, there's a bit more to it; you have to first tell bar that it is ok for foo to use its display by issuing the following command on bar:
    bar% xhost + foo.smith.edu
The same thing can be accomplished on a per client basis by giving the client the command line argument -display:
    foo% xterm -display bar.smith.edu:0 &

 
Contents
Previous
Next

X Window System Files

Where the X window system files reside on disk depends on the vendor and the user environment. However, in a generic X environment, the X filesystem structure looks something like this (exceptions for various OSes are noted in parentheses):
~/.twmrc

        The configuration file for the twm window manager.
        Example.

~/.xinitrc

        Initialization file for xinit.  Used to start each
        application in the user's startup environment.  
Example.

~/.Xresources or ~/.Xdefaults

        A file containing resource definition (preferences) for X clients.
        Example.  These resouces can be used
        either instead of or in concert with the default resources.

~/.xsession

        A shell script which is used to start each application in the 
        user's startup environment if the the X session is started 
        using xdm.  Example.

/usr/bin/X11 (/usr/X11R6/bin in Linux, /usr/openwin/bin in SunOS/Solaris)

        Directory containing X binaries which include the X server(s), 
        window managers, clients, and utilities.

/usr/include/X11 (/usr/X11R6/include in Linux, /usr/openwin/include in SunOS/Solaris)
        
        Directory containing the X header files necessary for compiling
        X programs.

/usr/lib/X11 (/usr/X11R6/lib/X11 in Linux, /usr/openwin/lib in SunOS/Solaris)

        Directory containing system startup files, fonts, locale 
        information, application defaults, etc.  Details follow.

/usr/lib/X11/app-defaults

        Directory containing application defaults for X clients such as 
        xterm, etc.  Example.

/usr/lib/X11/config

        Directory containing site and OS specific configuration files.  
        This info is needed to compile X programs.

/usr/lib/X11/fonts

        Directory containing fonts used by X.

/usr/lib/X11/twm
        
        Directory containing the systemwide startup file for twm.

/usr/lib/X11/xdm

        Directory containing systemwide startup files for xdm.

/usr/lib/X11/xinit

        Directory containing systemwide startup files for xinit.
In addition, various X11 libraries go in /usr/lib (/usr/X11R6/lib/X11 in Linux).
 
Contents
Previous
Next

Environment Specifics:

  • OpenWindows:
    • ~/.xinitrc - main startup script
    • ~/.openwin-init - specifies which tools are run at startup
    • ~/.openwin-menu - items which appear under the desktop popup menus
    • ~/.desksetdefaults - preferences and configuration for desktop tools
    • ~/.OWdefaults - preferences and configuration for OpenWindows
    • ~/.Xdefaults - preferences and configuration general X applications
  • CDE:

 
Contents
Previous
Next

X Window System - Is it a Client or a Server?

Noone really knows why (perhaps the X11 project started before the preponderance of what we now call the client-server environment?) but X uses a client-server paradigm which is diametrically opposite to what one would expect. Since this may be a bit confusing, we will elaborate.

In modern client-server world, a client is usually a small program which runs on your desktop. It connects over the network to a remote server (which is a program running on a more substantial box somewhere) and sends commands which the server understands. These commands make the server do something and return results back to the client.

The X server, on the other hand, runs on your desktop. An X client may be running locally (such as an xterm running from your hard disk) or running on a remote machine. If it is running on a remote machine and displaying on your desktop, the remote machine is connected using the X protocol to the X server running on your desktop. The X server understands the X requests coming from the remote machine to display the remote xterm.

Read the previous two paragraphs repeatedly until this issue is completely clear.


 
Contents
Previous
Next

X-Windows System - Window Managers

To enable window operations, X runs a client called a window manager. The window manager handles operations like moving, resizing, and iconifying windows. The window manager handles the appearance and manipulation of windows while X supervises communication between windows, applications, and input/output devices (like the monitor/keyboard).

Without a window manager, windows still display but you cannot move or resize them. Also, if one window happens to lay on top of another, you cannot access the second window.

The window manager is a special client of the X server. It is the only client with the ability to move, resize, map, unmap, iconify, and deiconify windows. The window manager normally has no windows itself, with the exception of the menu windows the user can summon. It is possible to run an X session without using a window manager, but parts of windows hidden under other windows would be inaccessible.

Several different window managers exist, and each user can choose his or her own window manager. However, you can only use one window manager at a time. For example, suppose there are two window managers available: the Tab and Motif Window Managers. When you log on, you may use either the Tab or the Motif Window Manager, but not both.

Here are the default window managers under various Unixes:

  • 4Dwm (an enhanced version of mwm) - IRIX
  • dtwm (the CDE window manager) - HP-UX 10.20, Solaris (with CDE)
  • fvwm95 - Red Hat Linux
  • mwm - AIX, Digital UNIX
  • olwm - SunOS, Solaris
  • twm - Slackware Linux
  • vuewm - HP-UX 9.x
In class, we will cover the following commonly used window managers: For more information on the currently available X window managers (including a few newer, more interesting ones), you can check out this excellent net resource:
 
Contents
Previous
Next

Window Managers - twm

twm, which stands for Tab Window Manager or Tom's Window Manager (because it was written by Tom LaStrange), is probably the oldest window manager still in use. It provides the basic, no-frills window management functionality and, like most window managers, is highly customizable. Here is a screenshot.

twm comes bundled with most Unixes we will be considering in this class.

A newer, enchanced version of twm called tvtwm has also been released. It adds virtual windowing capabilities to the base twm.

 
Contents
Previous
Next

Starting twm

Though it is usually started by the systemwide xinitrc or xsession (or a user's .xinitrc or .xsession), twm can also be started on the command line by simply typing twm while the X server is up and running and no other window manager is currently managing the desktop.

When twm fires up, it reads any user customizations in $HOME/.twmrc and, if the file cannot be found, twm uses the systemwide twm startup file (which usually resides in /usr/lib/X11/twm/system.twmrc). If neither of these files is found, twm uses builtin defaults. Here is an example twmrc.

 
Contents
Previous
Next

Using twm

Popup Windows: When you open a new window, it will pop up on the screen. It will appear as an outline and will follow the pointer. This means that the window is waiting for the user to position it. By pressing any mouse button the window will be placed at it's current location. The left mouse button will keep the window at it's default size. The right button may allow the window to fill from it's current position to the bottom of the screen.

Scroll Bars: Most scroll bars perform according to the following:

  • The left mouse button will move down or to the right one screen.
  • The right mouse button will move up or to the left one screen.
  • The center mouse button will move to the relative position on the slider. If you press and hold the center button it will scroll the window in real time.
Cut and paste:
  1. Cut the desired text. This may be accomplished in any of the following ways:
    • To cut a single word, place the pointer on the word and click the left mouse button twice. This should highlight the word.
    • To cut a line, place the pointer anywhere on the line and click the left button three times.
    • To cut any other section of text. Place the pointer on the letter where you wish to start the cut. Press and hold the left button, move the pointer so all the text you wish to cut is highlighted. Release the button.
  2. Paste the text. Move the pointer to the window where you wish to paste the text (whatever was the last item cut) and press the center mouse button.
  3. NOTES:
    • Cut text is not removed from the source.
    • The size of the cut buffer is limited. Size of text cut should be limited to approximately 250 characters.

 
Contents
Previous
Next

Window Managers - olwm

olwm is the OPEN LOOK window manager from Sun and has Sun's OPEN LOOK look and feel. It is part of Sun's OpenWindows graphical user environment. Here is a screenshot.

Note that though olwm is part of Sun's OpenWindows environment, it can be used on other platforms as well (the only requirement is that the OPEN LOOK glyph and cursor fonts are available to the X server). In particular, the Linux distributions often come with olwm (if they have the xview package installed).

You might also want to read the olwm man page.

 
Contents
Previous
Next

Starting olwm

On Suns, olwm is usually started by /usr/openwin/lib/Xinitrc. However, it can also be started by just typing olwm at the Unix prompt. It reads $HOME/.openwin-menu upon startup or, if this file cannot be found, /usr/openwin/lib/openwin-menu to set up olwm defaults.
 
Contents
Previous
Next

Using olwm

Pointer Shapes: The mouse cursor, called the Pointer, indicates the kind of operation you are currently using. In OPEN LOOK, the pointer is an arrow. There are other pointer shapes to give you current information, like a watch cursor when a process is busy, or a question mark when a selection isn't appropriate for that situation.

Mouse Buttons: You can use either the standard three-button mouse or an optional two-button mouse or optional trackball. The three mouse buttons behave differently; but their operation is usually just a simple click (a single press and release), a double click (two clicks in rapid succession), or click and drag (pressing a button and holding it down while you move the mouse pointer).

Different operations are selected by the left, middle, and right mouse buttons. If you have a two-button mouse, the middle button operations are selected by pressing both buttons at the same time. (The actual configuration of the mouse buttons, such as swapping left and right buttons, can be modified by the xmodmap utility.)

OPEN LOOK has made some default assignments for the mouse buttons. The three button mouse has these assignments for its buttons:

    Left - SELECT - this selects objects or controls
    Middle - ADJUST - this extends or reduces the number of selected objects
    Right - MENU - this displays and chooses menus
Mouse Buttons and Modifier Keys: The operation of mouse buttons is affected by any modifier keys, such as Shift, Ctrl, and Alt, you have set. For instance, selecting the header bar of the window by pressing the left mouse button to move it won't work if you have the Ctrl or Alt key depressed. This is especially frustrating (and often overlooked) with other modifier keys, such as Caps_Lock and Num_Lock. If you find unexpected problems with your window manager operation, check the modifier keys. For instance, OPEN LOOK uses the Caps_Lock key to invoke micropositioning for the mouse, so you will see much slower mouse tracking.

Selecting Focus: The OPEN LOOK Window Manager can give focus to a window in two ways - either by placing the pointer in the window and clicking on it (this is the default choice), or by simply moving the pointer into the window. There is a configuration selection (called a resource) that selects this mode of operation. If you point and click to give a window focus, that window retains focus regardless of where the mouse pointer is located. Many people prefer this type of operation because they can work in one window and ignore the mouse position. Others prefer to be able to get window focus by moving the mouse only, without the need to click when they are in the window.

This is a significant difference in the way a user controls the screen. The operation of the window manager controls this function and many other significant operations depending on its configuration. These questions of configuration are important and are discussed at the end of this chapter.

Window Decoration: The border around the window, called its decoration, has areas marked that permit easy manipulation of the window. These are labeled below, along with brief descriptions of their operations.

    Title Bar - This is the block showing the title you (or the client) gave to this window.
    Resize corner - These are the handles to resize the window.
    Iconify button - This button reduces the window to an icon.
Moving a Window: You can move windows around on the screen to make your work more efficient. This is a primary function of the window manager. To move a window, place the mouse pointer in the title bar or in one of the window borders and press the left button down, then drag the mouse pointer to the new location of the window. An outline of the window will follow your movements. When the position of the window is correct, simply release the mouse button and the window appears at its new location.

Resizing a Window: You can change a window's size to make your work easier. Imagine that the window has elastic sides. By grabbing a corner (point the mouse on a corner and press the left button and hold it down), you can stretch the window to a larger size or let it contract to a smaller size. Dragging the lower left corner stretches the left and bottom sides (with the upper right corner remaining fixed). Try resizing a window - it's easier to learn by doing than by reading.

Iconifying and Deiconifying a Window: You can gain space on your screen by changing a window into an icon. The window will continue to function if it has a process running, but it is temporarily hidden from view. To iconify a window, simply move the pointer to the Iconify button and click on it. The window changes to an icon and moves to a row of icons, usually at the bottom of the root window. You can move the icons (they are windows), or define a different place for the icon row. The icon itself has an image and a label so you can distinguish them from each other. To deiconify an icon, move the mouse pointer to it and double click. The window will be restored to its original size and location on the screen.

Exiting olwm: You can exit the OPEN LOOK Window Manager by selecting the "Exit WM" line in the menu. Clicking on the last line "Exit" closes all of your windows and connections.


 
Contents
Previous
Next

Window Managers - mwm

mwm is the Motif window manager. It is based on the second proprietary interface (the first being Sun's OPEN LOOK) and defines a generic look and feel which is slowly becoming an industry standard. It was developed by Ellis Cohen of the Open Software Foundation. Here is a screenshot.

mwm attempts to create a three-dimensional appearance which is somewhat most aesthetic than that provided by many other window managers.

Because it is licensed software, mwm tends not to be bundled unless the Unix vendor has licensed it from Open Group (OSF's new name). You will find mwm by default under IRIX and HP-UX but not under SunOS/Solaris and Linux (where they need to be purchased separately).

 
Contents
Previous
Next

Starting mwm

As with other window managers, mwm can either be started from the standard startup files (.xinitrc or .xsession) or by typing
    % mwm &
at the command line. mwm looks for ~/.mwmrc first, then /usr/lib/X11/mwm/system.mwmrc. For information on how to customize mwm, see the O'Reilly book "X Window System User's Guide" by Quercia and O'Reilly.
 
Contents
Previous
Next

Using mwm

Selecting Focus: Again, you select the window to receive input by moving the pointer anywhere in that window. When you focus input on a window, it changes color. Also, when you are working with a stack of windows that overlap, selecting a window as the active window automatically raises that window.

Moving a Window: a) Place the pointer in the titlebar (the titlebar is the area of the window at the top which says "xterm" in the screenshot). The pointer changes to an arror cursor. b) Press and hold the left mouse button. c) Move the window by dragging the mouse. While dragging the pointer changes to a cross arrow and a window outline which tracks the movement of the window appears. In the center of the screen, a small box also appears with the x and the y coordinates of the window as you move it. d) Drag the cross arrow pointer with the window outline to the desired location. e) Release the left mouse button.

Minimizing and Maximizing a Window: There are two buttons at the top right corner of the titlebar. On the left is the minimize button (a little square with a dot in the middle); on the right is the maximize button (a square with a smaller square in it). To minimize a window, click on the minimize button at the top right. This will iconify the window on the desktop. To maximize it, click on the maximize button. This will make the window as big as it can be (which can be the size of the entire screen for many windows).

Resizing a Window: The entire frame of a window under mwm control is available for resizing. (This is a feature not available in many other window managers.) You can click on any part of the frame with the left mouse button where the pointer changes to an arrow pointing toward the window border and drag the mouse to resize the window.

The Windows Menu: Clicking the left mouse button the top left corner of the titlebar (in the area which looks like a small square with a - sign in it) brings up the window menu. Using this menu, you can restore, move, size, minimize, maximize, lower, or close the window. Note that all of these actions can also be performed using ALT + function key keystrokes.

Moving a Window: The easiest way to move a window is to place the pointer on the title bar, then click the left mouse button and to drag the mouse to the desired location.

Working with mwm Menus: Clicking the left mouse button on an empty part of the screen will bring up the root menu. This menu can be customized by editing the ~/.mwmrc file.


 
Contents
Previous
Next

Window Managers - fvwm

fvwm, written by Robert Nation, is a relative newcomer in the X world. However, due to its speed, efficient use of memory, and customizability, it is currently probably the most ubiquitous of the virtual window managers. fvwm was derived from twm.

fvwm was designed to be infinitely customizable (to the extent that there exists fvwm95, a Windows 95 lookalike), even as far as the look and feel is concerned. For example, you can customize it to look like twm or like mwm! Here is a screenshot of a fvwm managed desktop created using this .fvwmrc.

Another very attractive feature of fvwm is its use of add-on modules. Modules can be loaded dynamically and can be used to extend fvwm's capabilities beyond most other window managers.

You can get more information about fvwm at the official fvwm home page.

 
Contents
Previous
Next

Starting fvwm

fvwm can be started in the usual manner from a user's startup file ( .xinitrc or .xsession) or by typing its name at the command prompt:
    fvwm &
The first file fvwm looks for is ~/.fvwmrc. All user customizations reside there. If this file is not found, fvwm looks for the systemwide configuration file in /usr/lib/X11/fvwm/system.fvwmrc.
 
Contents
Previous
Next

Configuring fvwm

Here is an excellent resource on configuring fvwm version 1 (version 2 is the most recent one).


 
Contents
Previous
Next

xdm, the X Display Manager

xdm is a special X client which is designed to to start the X server automatically (from the Unix /etc/rc* system startup file) and to keep it running. (X can also be started manually via xinit as we have seen before.) If xdm is running, you will most likely see a graphical login screen (for example, in HP-UX and IRIX).

In its most basic implementation, xdm emulates the function of getty and login programs. It puts up a graphical login prompt and manages the login session. Once a user logs in, xdm reads the user's ~/.xsession and fires up the X applications defined in it. When the user logs out, xdm makes sure that all user processes have ended and puts up the login screen again for the next user to log in.

However, xdm can be used in a more powerful way. Users can design their own sessions, running several clients and setting personal resources (such as keyboard, pointer, and display characteristics). Special xdm files can also be customized to manage several connected display (both local and remote) and to set system-wide X resouces (for example, client default features).

 
Contents
Previous
Next

XDMCP

XDMCP stands for "X Display Manager Control Protocol". It is a mechanism supported by xdm which allows an Xterminal to request a session from a remote host.
 
Contents
Previous
Next

xdm files

reside in /usr/lib/X11/xdm and include:
Xsession        the systemwide file which tells xdm which X apps to start
                if ~/.xsession doesn't exist    

xdm-config      Configuration parameters for xdm.

xdm-errors      Contains xdm errors.

Xaccess         Defines access control for XDMCP connections.

Xlogin          Contains commands which initialize the login screen and 
                provides hooks for alternative login programs.  

Xreset          Run after the server is reset, but before the Login 
                window is offered.

Xresources      A list of resources to be loaded onto the display 
                using xrdb.  As the authentication widget reads this database 
                before starting up, it usually contains parameters for 
                xlogin.

Xservers        Contains a list of displays to manage.

Xstartup        Prevents logins while /etc/nologin exists.

 
Contents
Previous
Next

X User Environments

Now that we have seen a number of popular X window managers, we can discuss a number of complete user environments. All of these except the Sun OpenWindows are based on the Motif user interface.
 
Contents
Previous
Next

Common X Clients

While there are a large number of X clients in use today, a few still top the list as basic ones used most of the time by users. This list includes:
  • xterm
  • xclock
  • xsetroot
  • xbiff
  • xhost
  • xcalc
Be sure to read man pages for these and know how to use them.


 
Contents
Previous
Next

Common X Clients - xterm

xterm is perhaps the most important and the most often used X client of all. It provides VT100 and Tektronix terminal emulation in the X environment.

For detailed info on xterm, please read the man page.

 
Contents
Previous
Next

Starting xterm

The simplest way to start xterm is to just type its name at the command prompt:
    % xterm &
However, a plethora of startup options can be specified on the command line. Here is a list of the most commonly used ones:
    % xterm -sb -bg blue -fg white -fn 6x13 -title sun -name sun -e tcsh &
Explanation:
  • -sb adds a scrollbar to the window
  • -bg blue changes the window background color to blue. The color names can be found in /usr/lib/X11/rgb.txt.
  • -fg white changes the window foreground (text) color to white.
  • -fn 6x13 changes the window font to 6x13 (the abbreviations for fonts can be found in /usr/lib/X11/fonts/100dpi/fonts.alias).
  • -title sun makes the string "sun" title of the window.
  • -name sun makes the string "sun" the name of the window. You define both title and name because in the iconic form, it is the name of the window which is displayed, not the title.
  • -e tcsh tells xterm to execute tcsh in the window.
 
Contents
Previous
Next

Copying and Pasting Text

You can copy text from one xterm window and paste it into another xterm or X client. To copy text, you place the cursor where you want the selection to start and hold down the left mouse button. Now drag the cursor to highlight and select text. When you release the left mouse button, the text stays highlighted and is copied into a buffer (hitting any key will unhighlight the text but it will still remain in the buffer). You can now move the cursor to another xterm window and click on the middle mouse button to paste the selection.

Double clicking the left mouse button on a word selects the whole word. Triple clicking selects the entire line you are on.

 
Contents
Previous
Next

Using xterm Menus

Menu 1 - Main Options

If you are in an xterm and hit CTRL + left mouse button, you get this menu. It allows you to set certain modes and to send signal that affect the xterm process.

Here are the choices with explanations:
Secure KeyboardToggling this option on causes all user input to be directed only to the xterm window. This is useful to secure your X display when typing passwords etc. The foreground and background colors are exchanged in the secure keyboard mode. Only one X client can secure the keyboard at a time. Secure keyboard is disabled automatically if you iconify the xterm.
Allow SendEvents Toggling this on will cause synthetic key and button events (generated using the X protocol SendEvent request) to be interpreted. (Allowing such events creates a big security hole so this is not recommended.)
Log to FileToggling this on will start logging the terminal session to a file called ~/XtermLog.xxxx where xxxx represents the PID of the xterm process.
Redraw WindowRedraw the current xterm window.
Send [SIGNAL] signal

where SIGNAL can be

STOP
CONT
INT
HUP
TERM
KILL

Send the appropriate signal to the xterm process. Most of these signals can also be sent by a "kill -[SIGNAL] xterm-pid".
quitSends a SIGHUP to the process group of the process running under xterm which is usually a shell. This kills the xterm process and the window disappears from the screen.

Menu 2 - VT Options

If you are in an xterm and hit CTRL + middle mouse button, you get this menu. It provides VT102 setup functions. Most are analagous to a real VT102 (the exception is scrollbar).
Enable Scrollbar Adds a scollbar to the left side of the xterm window.
Enable Jump Scroll Turns on jump scrolling so that the window moves text several lines at a time rather than line by line.
Enable Reverse Video Reverses foreground and background colors.
Enable Auto Wraparound Turns on automatic wrapping of long lines.
Enable Reverse Wraparound Enables the wrap around from the leftmost column to the rightmost column of the previous line. Allows you to backspace to the previous line and overstrike data or erase data with the space bar.
Enable Auto Linefeed Turns on the generation of an automatic linefeed. This is useful for programs which generates a carriage return but not a linefeed.
Enable Application Cursor keys Generate ANSI escapes sequences rather than standard cursor movement when you use the arrow keys.
Enable Application Keypad Generate a control function rather than numeric character when you use the numeric keypad.
Scroll to Bottom on Key Press Indicates that pressing a key while using the scrollbar causes the cursor to be respositioned at the bottom of the scroll region. For example, if you have scrolled up the window to see past history, as soon as you begin typing your next command the cursor jumps to the bottom of the screen.
Scroll to Bottom on Tty Output Indicates that receiving output to the window while using the scrollbar causes the cursor to be repositioned at the bottom of the scroll region.
Allow 80/132 Column Switching Allows xterm to recognize the DECCOLM escape sequence which switches the terminal between 80 and 132 column mode.
Enable Curses Emulation Enables the curses fix. Several programs that use the curses cursor motion package have soem difficulties with VT102-compatible terminals. The bug occurs when you run the more program on a file containing a line that is exactly the width of the window and that is followed by a line beginning with a tab. The leading tabs may disappear. This mode causes the tabs to be displayed correctly.
Enable Visual Bell Causes the terminal window to flash whenever an event occurs that would ordinary cuase your terminal to ring a bell.
Enable Magin Bell Turns on the margin bell (like in typewriters).
Show Alternate Screen Informs you that you are looking at the alternate screen. You cannot select this mode.
Do Soft Reset Resets the terminal scroll region from partial scroll to full scroll. Use this commmand when a program has left the scroll region set incorrectly.
Do Full Reset Clears the window, reset tabs to every eight columns, and reset the terminal modes such auto wraparound and jump scroll to their initial states.
Show Tek Windows Show the current contents of the Tektronix window. You cannot input to that window until choose Switch to Tek Mode.
Switch to Tek Mode Brings up a Tektronix window.
Hide VT Window Removes the VT window but does not destroy it. It can be brought back by choosing Select VT Mode from the Tek Options menu.

Menu 3 - VT Fonts (Changing Fonts Dynamically)

xterm allows you to select a few fonts dynamically. The font in the current xterm window may be changed by pressing the CTRL key and the right mouse button simultaneously. This will bring up a menu. Still holding the right button select the font desired. Release the button and the font in the xterm will change.

The VT Fonts menu items are:
Default Switch to whatever font the xterm was started with.
UnreadableSwitch to nil2 font (default under Solaris)
TinySwitch to 5x8 font (default under Solaris)
SmallSwitch to 6x10 font (default under Solaris)
MediumSwitch to 8x13 font (default under Solaris)
LargeSwitch to 9x15 font (default under Solaris)
HugeSwitch to 10x20 font (default under Solaris)
Escape Sequence Allows you to select a font previously toggled using an escape sequence.
Selection Allows you to toggle a font whose name you have previously selected with the mouse or using the select button of the xfontsel client.
The settings for the Tiny, Small, Medium, Large, and Huge fonts by default are constant width fonts from the directory:

    /usr/openwin/lib/X11/fonts/misc (SunOS, Solaris)
    /usr/lib/X11/fonts/misc (IRIX, HP-UX)
    /usr/X11R6/lib/X11/fonts/misc (Linux)
(look in fonts.alias for the mappings). They are defined in the application defaults file for xterm. This file can be found in the directory;
    /usr/openwin/lib/app-defaults/XTerm (SunOS, Solaris)
    /usr/lib/X11/app-defaults/XTerm (IRIX, HP-UX)
    /usr/X11R6/lib/X11/app-defaults/XTerm (Linux)
Look for the strings font1, etc.

You can change the default font setting by defining resources font1-font6 in your ~/.Xresources file like this:

    XTerm*VT100*font1: nil2
    XTerm*VT100*font2: 5x8
    XTerm*VT100*font3: 5x9
    XTerm*VT100*font4: 8x13
    XTerm*VT100*font5: 9x15
    XTerm*VT100*font6: 10x20
To see what these fonts look like, run the xfontsel application or try opening xterm with different font aliases in the fonts.alias file in the X11 fonts directories.
 
Contents
Previous
Next

Using the Tektronix Window

If you have an application which makes use of a Tektronix terminal and which switches to Tektronix mode correctly, xterm will fire up the Tektronix window.

Under the Tektronix window, a CTRL + middle mouse button gets you the Tek Options menu.
Large Characters
#2 Size Characters
#3 Size Characters
Small Characters
Selecting one of these options sets the point size of the text displayed in the Tek window.
PAGE Clears the Tek window
RESET Closes the Tek window
COPY Writes a file of the Tektronix text and graphics commands
Show VT Window Shows the current contents of the VT102 window. You cannot input to that window until you choose Switch to VT Mode.
Switch to VT Window Makes the associated VT102 window active for input.
Hide Tek Window Removes the Tek window but does not destroyt it. IT can be brought back by choosing Switch to Tek Mode from the VT Options menu.

 
Contents
Previous
Next

Fixing xterm after a Resize

How xterm is resized depends on the window manager running. If xterm seems to act strangely after resizing, issue the command
    . /usr/bin/X11/resize (Bourne shell)

    or

    eval `/usr/bin/X11/resize` (c shell)

to fix it.
 
Contents
Previous
Next

xterm and Terminal Emulation

Shell startup script such as .login and .profile often deal with setting terminal type for that shell. This usually involves setting the TERM environment variable and/or running tset. Setting the terminal type is not required for xterm windows since it has its own way of dealing with terminal types. Several entries in termcap or terminfo such as "xterm", "vt100", "vt102", and "ansi" work with standard size (80x24) xterm windows. The xterm client automatically searches the terminal database for these entries (in order) and sets the TERM environment variable according to which entry it finds first.

In light of this, you may want to remove any lines in the startup files which set the terminal type (or set it to "xterm").

Also, your vendor may supply its own version of terminal emulator which may or may not be based on xterm. Examples are hpterm, dxterm, aixterm, etc.

 
Contents
Previous
Next

xterm and the Login Shell

If csh is your default shell, you may know that .cshrc is read for every csh command run, and that the .login file is read only once, at the beginning of each "login" shell.

X effectively redefines the meaning of the "login" shell. Since X enables the ability to run multiple windows, the usage of .login becomes confused. (You probably don't want to be told that you have new mail and see the message of the day every time you open a new xterm.) So by default, xterm shells are not login shells so .login is not read when you open an xterm (however, it still reads .cshrc). (You can still call a login shell in an xterm by giving it the -ls option or by setting the resource XTerm*login-Shell to true.)

The .cshrc file therefore has to assume more responsibility in the X environment. For example, you have to make sure that you are doing the following in your .cshrc:

  • defining any changes to the default path,
  • setting your favorite prompt,
  • defining your favorite aliases,
Please also note that if you are using xinit to start X, your .login file is executed when you first login in prior to starting the X server. If you use xdm, your .login is never executed at all unless you run xterm with the -ls option.


 
Contents
Previous
Next

Exercise 1

Log into your workstation.
  • Start an xterm with the font 8x13 and title "myxterm" and name "myxterm".
  • Iconify this xterm and confirm that the name setting took.
  • Kill the xterm you just started.
  • Restart the xterm with the same options as before except this time specify the command line option which will fire up tcsh in the xterm window when it appears.
  • Confirm that tcsh is the current shell by doing a
    • echo $shell
 
Contents
Previous
Next

Exercise 2

This assumes that you are still logged in on your workstation and that the X server is running.
  • From the xterm you started in exercise 1, log in to a different workstation (any workstation) with the same username you have on your workstation.
  • Set the display and issue the xhost command appropriately (on your workstation and the remote workstation) such that typing xterm on the remote machine displays the xterm window on your workstation.
  • Log off the remote machine and kill the remote xterm window by typing "exit" in it.


 
Contents
Previous
Next

5 - Other Unix topics, Mail, utilities, .....

 
Contents
Previous
Next

Mail Issues

Mail is a important and relatively complex issue which I will only deal with nominally here. I've compiled a list of issues related to Pine, the email client of choice at Smith, mostly explaining how to deal with attachments and other types of computers.

One important detail to get about mail. E-mail, no matter whose computer you use, has to abide by certain conventions and standards that allow it to pass along through the various relays along the internet and make it to the intented client intact. The two standards most of interest to us are SMTP and MIME.


What is the Simple Mail Transfer Protocol (SMTP)?

The Simple Mail Transfer Protocol, or SMTP, is the de facto standard for electronic mail transport across the Internet. When you send an e-mail message, SMTP packages your message in a kind of envelope and relays it to its destination. Multiple servers are often involved in the transport of the message, and as it passes through them, each one timestamps and tags it. Thus, when the message arrives, the recipient can get an idea of where it's been, and when it was sent. SMTP also handles error messages, sending notifications to senders when there is difficulty delivering their mail.

When you set up a POP or IMAP client, such as Eudora, Netscape, or Outlook Express, you must specify an SMTP server. This is for your outgoing mail and may be different from the server you use to check incoming mail.

The Simple Mail Transport Protocol was introduced in a series of RFCs, notably 772, 780, and 788, with the current base specification appearing in 1982's RFC 821:

  
http://info.internet.isi.edu:80/in-notes/rfc/files/rfc821.txt
Work continues on SMTP, however, and over the years its capabilities have been significantly enhanced. Modern programs that implement SMTP, such as sendmail, are based on these Extended SMTP (ESMTP) standards. Important RFCs that cover extensions or otherwise discuss SMTP include:

What is MIME?

MIME, the Multi-purpose Internet Mail Extensions, is a freely available specification that offers a way to interchange text in languages with different character sets, and multimedia e-mail among many different computer systems that use Internet mail standards.

If you were bored with plain text e-mail messages, thanks to MIME you now can create and read e-mail messages containing these things:

  • character sets other than ASCII
  • enriched text
  • images
  • sounds
  • other messages (reliably encapsulated)
  • tar files
  • PostScript
  • FTPable file pointers
  • other stuff
MIME supports not only several pre-defined types of non-textual message contents, such as 8-bit 8000Hz-sampled mu-LAW audio, GIF image files, and PostScript programs, but also permits you to define your own types of message parts.

Before MIME became widespread, you might have been able to create a message containing, say, a Postscript document and audio annotations, but more often then not, the message was encoded in a proprietary, non-transportable format. That meant that you couldn't easily handle the same message on another vendor's workstation, or even get it intact through a mail gateway in the first place. Now, depending on the completeness of your MIME-capable mail system, there's a good chance that it'll "just work." (See section 1.2 of the original source FAQ for some warnings on this subject).

One of the best things about MIME is that it's a "four-wheel drive protocol", (to borrow a description applied originally to PhoneNet by Einar Stefferud). MIME was carefully designed to survive many of the most bizarre variations of SMTP, UUCP, and other Procrustean mail transport protocols, such as BITNET and MMDF, that like to slice, dice, and stretch the headers and bodies of e-mail messages.


In Pine, how do I attach a file to an e-mail message?

Encoding the file

This step isn't necessary if you are sending the file to someone you know is using a MIME compliant mail program (e.g. Pine). You can also skip this section if the file is encoded before you FTP it to your e-mail computer. (For example, Fetch permits you to Binhex a file before transferring it.) Finally, if the file is a text file encoding is not necessary. (Note, word- processed documents are not text files.)
  1. Login to your e-mail computer
  2. Type the following command:
  3.  
      uuencode filename destname > localname
    Replace "filename" by the name you gave the attached file when you transferred it; replace "destname" with the name you want the file will have when you have sent it and it's been decoded by the recipient; replace "localname" with the name you want for the new encoded version of file which will be created by this command on your e-mail computer directory.
  4. The file is now uuencoded and saved to your home directory, under whatever name you chose for "localname".
  5. When you're finished, enter Pine.

Attaching the file to a mail message in Pine

If the recipient is using a MIME compliant mailer (such as Pine), then follow these steps:
  1. Compose a message as you normally do.
  2. While the cursor is still highlighting the headers, press CTRL/J to attach a file, then CTRL/T to see a list of files in your home directory.
  3. Using the arrow keys select the file you just transferred.
  4. Press enter to attach file. You will be prompted for a comment to put in the message. It is a good idea to use this comment to tell the recipient what kind of file the attachment is (e.g., BinHexed WordPerfect for the Mac, etc.) so they know how to handle it.
  5. Press CTRL/X to send the message.
If you are sending a uuencoded file or a plain text document, then follow these steps:
  1. Compose a message as you normally do.
  2. With the cursor in the body of the message, press CTRL/R to attach a file and CTRL/T to see a list of files in your home directory.
  3. Using the arrow keys, select the text file or file you just uuencoded (i.e., "localname").
  4. Press enter and the text file or uuencoded document will be included in the body of the message.

  5. If the file is uuencoded, it is a good idea to inform the recipient of this either in the subject line or in the body of the text.
 
Contents
Previous
Next


In Pine, how do I save a file attached to an e-mail message to my hard drive?

There are three general steps to transferring an attachment from an e-mail message in Pine to the hard drive on your local computer:
  • Export the attachment to a file
  • Decode the file (if necessary)
  • Transfer the file to your local computer

Export the attachment to a file

  1. Select the message that has the file attached to it, and open it as normal.
  2. Press V to invoke the "View Attach" command.
  3. You will see a list of that message's attachments. Select the attachment you want with the arrow keys, and press S for "Save".
  4. You should see a prompt that says:
  5.   Copy attachment to file in home directory:
    Type in the name you want to give the file, press Enter or Return, and Pine will save it to your home directory.
  6. When you're done, Press E for "Exit Viewer".

Decode the file

Attachments often contain binary data, but the current methods used to transfer e-mail messages only allow the basic ASCII text characters to be transmitted. Several methods have been developed to encode binary data using only ASCII text characters. Also, other encoding methods have been developed that allow you to store information with a smaller number of characters. Attachments that contain binary data will be encoded using one or more of these methods. Once the attachment is extracted to a file, you will probably need to decode this file. Pine, however, knows how to decode two encoding formats, base64 and quoted printable. If the headers on the attachment are properly formatted, then Pine will do the encoding as it is extracting, and the resulting file will already be decoded. You will not need to decode the file if the attachment was unencoded text or was automatically decoded by Pine. Otherwise, you will need to decode the file yourself. Below is a list of common encoding methods (with filename extensions that commonly identify them) and references for instructions for decoding them.

Note: Some of these texts give directions for doing the decoding on your local computer. If you choose to use these methods, then you will (obviously) need to transfer the file before decoding. Also, methods other than the ones in the references below are available. Programs are available for decoding all of these encoding formats for the Macintosh and there are programs for IBM-compatible computers that will handle most of these.

uuencode (uu, uue)
compress (Z)
zip (zip)
Gzip (gz, z)
tar (tar)
BinHex (hqx)
Stuffit (sit)


In Pine, how do I decode a uuencoded mail message I received?

A uuencoded file typically contains non-text data (e.g., graphics, programs and word processing files) converted into ASCII text. To uudecode a message with Pine, while you are looking at the message, press the pipe key ("|"). Pine will prompt you for a command. Enter uudecode and then press return or enter. This will decode the message and save it in a file on your home directory. The name of this file will be the last word in the "begin" line of the uuencoded message. So, in the following example, the file will be called myfile.zip:
  begin 644 myfile.zip
You can then use the file or transfer it to your own computer.

In Unix, how do I uuencode a binary file?

The uuencode command uses the syntax:
  uuencode original_filename final_filename > encoded_filename
Replace original_filename with the name of your binary file. Replace final_filename with the name that you want the file to have when it is eventually decoded (usually the same as original_filename). Replace encoded_filename with the name you want to give the uuencoded version of the binary as it will appear in your directory.

For example, to convert a binary file named myfile.zip into a uuencoded (text) file named myfile.uue, you would enter at the shell prompt:

  uuencode myfile.zip myfile.zip > myfile.uue
The original binary file myfile.zip is unchanged, and the uuencoded file myfile.uue is now located in your current directory. The greater-than character (>) is used to redirect output; it takes the output of the uuencode command and directs it into the file myfile.uue (rather than onto your screen).

The purpose of the uuencode program is to translate a binary file that contains unprintable (non-text) characters into a format that is entirely readable. This prevents mail, news, and terminal programs from misinterpreting non-text characters as special instructions. A text file produced by uuencode may thus be mailed as an ordinary mail message and later uudecoded into its original binary form by the recipient. The uuencoded file is usually larger than the original binary file, because the unprintable characters in the binary file are expanded into multiple readable characters


How do I send a WordPerfect or MS Word file as an e-mail message?

Standard e-mail can only handle text (ASCII) files. Most word processors save their files in a non-ASCII format, intended for use only by that word processor. Most word processors also allow files to be saved in ASCII format, but doing so strips out or ignores special formatting and fonts (such as boldfacing, underlining and foreign characters). If you don't mind converting your file into a "plain text" format, then you can use the following method to send a word processed file as an e-mail message:
  1. Save the file as you normally would (to preserve the original version).
  2. Save the file again, but use the "Save As" option from the "File" menu, and specify ASCII or text as the file format. Be sure to give the file a different name -- perhaps changing the extension to .txt. In WordPerfect, for example, select "Text Export" from the "Save As" dialog box. In Microsoft Word, select "Text only".
    • FTP the file to your Unix email machine.
  3. Start to compose a message, and then either read the contents of the text file into your "compose" buffer, or else specify that the file should be an "attachment".
  4. Send the message.
The instructions above are the most straightforward way of sending a word-processed file as an e-mail message, but there is another way: encoding the file into a non-text ASCII file which can be e-mailed, such as Binhex, MIME, or uuencode.

Many e-mail programs, including Pine and most workstation-based e-mail programs, will automatically MIME encode any non-ASCII file that is included as an attachment.

The advantage of encoding is that the file format of your word processor (including all special fonts, formats and characters) is preserved. The disadvantage of encoding is that the recipients of your message might not have the utilities on their computers to decode your message. Furthermore, they might not use the same word processor as you, and theirs might not be capable of converting your word processor's file format. Therefore it is usually safer to cut and paste the text of your file into the body of your e-mail message.


What is POPmail?

"POPmail" refers to e-mail software that is installed on your personal computer which sends and receives mail very quickly from a shared computer's electronic post office. The "POP" in POPmail stands for "post office protocol." Personal computers seldom have the network resources required to serve as an independent post office, which is why most people use shared systems or POPmail for e-mail.

POPmail software on your personal computer (the "POP client") quickly logs into a shared computer which is an e-mail post office (a "POP server"), and transfers received mail from your account on that system to your workstation. When you send a message from your workstation, the POP client transfers it to a dedicated mail system for transmission on the Internet.

POPmail programs include Eudora for Macintosh, PC-Eudora for Windows, Pegasus for Windows, Explorer, and Netscape.

POPmail requires that you have an account on a shared computer system . It is to this account that incoming mail goes first, and this is your "preferred e-mail address." However, instead of making you log in from your workstation to the shared computer (using your workstation as a "dumb" terminal), POPmail programs automatically connect to the POP server only long enough to transfer messages. You use the power of your workstation in your favorite user-friendly environment (Macintosh or Windows) to compose, read, and manage mail. This keeps POPmail fast, even during rush hours on "shared" computers.

POPmail programs are also usually rich in features: document attachment, automatic document encoding/decoding, user lookup, internal address books, font selection, signature files, and multiple mail management options.

Of course, POPmail has disadvantages too: if you use POPmail on campus, and dial in to your shared system account to use e-mail from home, you could end up managing your e-mail in two different places. Furthermore, if you "pop" your mail to your personal computer without keeping a copy of it on your shared computer account, you'll need to back up your e-mail files regularly.


What is IMAP (Internet Message Access Protocol)?

IMAP (Internet Message Access Protocol -- formerly known as the Interactive Mail Access Protocol) is a protocol for e-mail clients to retrieve e-mail messages from, and work with the mailboxes on, a mail server. The latest version, IMAP4, is similar to POP3 but offers additional and more complex features. With IMAP4, for example, you can work with your e-mail on the server -- such as searching your e-mail messages for keywords -- while your e-mail remains on the server. You can download messages to your computer and leave them on the server to be accessible from a different computer. IMAP uses SMTP for communication between the client and server.

IMAP was developed at Stanford University in 1986. For more information about IMAP, see Stanford's Web page at:
http://www-camis.stanford.edu/projects/imap/ml/imap.html


Printmail utility

Printmail is a free utility (courtesy of elm) that allows you to format a mail message for printing.

printmail | lpr

will print all the mail in your default mailbox. Run the man page on it to learn more.


 
Contents
Previous
Next

What is FTP, and how do I use it to transfer files?

What is FTP?

FTP is an acronym for "file transfer protocol." As the name implies, FTP is used to transfer files between computers on a network, generally the Internet. You can use FTP to exchange files between your computer accounts, transfer files to or from an account to a desktop computer, or access software archives on the Internet. Most important, FTP follows a open internet standard on all computing platforms, so once you learn it you know it no matter where you go.

What is anonymous FTP?

Many universities, government agencies, companies, and private individuals have set up publicly accessible archives on the Internet. There are thousands of these sites that contain a myriad of programs, data files, and informational text. Maintainers of such sites set aside a set of public directories and files that may be read by the rest of the world via FTP, usually in a directory named /pub. Specific directions or information about a site is generally contained in greeting messages or files with names like README.

Remember that anonymous FTP is a privilege granted by the organization that owns the computer to which you are connecting, and you should show good manners in your usage. Don't transfer files you don't need or an excessive amount of material, and try to restrict your transfers to off-peak hours. Many FTP sites are used very heavily, and you may need patience to connect.

How do I use FTP?

There are several ways to use FTP. The most convenient way is generally with a World Wide Web browser, which can connect to an FTP address just as it can to a Web (HTTP) address. Using a Web browser for FTP transfers makes it easy for you to browse large directories and read and retrieve files. Your Web browser will also take care of some of the details of connecting to a site and transferring files. To use your Web browser to connect to the site ftp.math.smith.edu, you would use ftp:// in front of the host name, with a / after the name:
  ftp://ftp.math.smith.edu/
While this method is convenient, Web browsers are often slower, less reliable, and have fewer features than other ways of using FTP.

You can also use FTP in a command line format from your Unix account, or your Unix or DOS workstation. To start FTP, enter at the command line prompt:

  ftp hostname
Replace hostname with the name of the FTP site (e.g. ftp.math.smith.edu or ftp.netscape.com).

When you connect to another computer with FTP in this way, you must enter a name and password. To connect to an account of your own, enter your own username and password. To connect to most "anonymous" FTP sites, enter the username "anonymous" and your e-mail address as the password. As a shortcut, you can usually abbreviate your e-mail address as

  username@
Replace username with your own username. The FTP program will automatically add your site name after the @ sign.

Here are some common FTP commands, with examples of how you would enter them at the FTP prompt. Note that in this list, "remote" refers to the computer to which you are connecting with the FTP program.

  help           Display the available commands.
                 Examples: help          (list commands)
                           help put      (help with put command)
                           help dir      (help with dir command)

  open address   Open an FTP session with a remote computer.
                 Example: open ftp.indiana.edu

  dir            Display the directory contents on the remote computer,
                 along with file sizes and permissions.
                 Examples: dir           (list current directory)
                           dir pub       (list files in "pub")
                           dir ..        (list files in parent directory)

  cd dirname     Change directory to "dirname" on remote computer.
                 Example: cd pub         (change to directory "pub")

  lcd dirname    Change local directory on the computer on which
                 you started the FTP session.
                 Examples: cd /tmp       (switch to /tmp directory)
                           cd ~/         (switch to your home directory)

  binary         Set file transfer type to binary, necessary when
                 transferring files (like programs or images) that
                 aren't stored as text files.
                 Example: binary

  get filename   Get a file from the remote computer and store it in
                 the current directory on the computer on which you
                 started the FTP session.
                 Example: get theirfile  (grab a copy of "theirfile")

  mget filenames Get more than one file from the remote computer and
                 store them in the current directory on the computer
                 on which you started the FTP session.
                 Example: mget hisfile herfile
                    (grab copies of "hisfile" and "herfile")

  put filename   Put a file from your local directory into the current
                 directory of the remote computer.
                 Example: put myfile     (send a copy of "myfile")

  mput filenames Put more than one file from your local directory into
                 the current directory of the remote computer.
                 Example: mput myfile ourfile
                    (send copies of "myfile" and "ourfile")

  close          Close the current connection, but stay in FTP.

  quit           Close the current connection, and quit the FTP program.
You can also use the FTP program non-interactively in a shell script, and using the expect command.

 
Contents
Previous

File Packaging/Compression tools

Unix has a good set of archival and compression tools. The following table lists the main ones:
tar
zip
compress
gzip

In Unix, what is "tar", and how do I use it?

"tar" stands for "tape archiving", which is one use for the command. However, tar's more common use is to combine two or more files for storage or distribution.

To combine multiple files and/or directories into a single file, use the following command:

  tar -cvf file.tar inputfile1 inputfile2
Replace "inputfile1" and "inputfile2" with the files and/or directories you want to combine. You can use any name in the place of "file.tar", though you should keep the ".tar" extension. If you don't use the "f" directive, tar assumes you really do want to create a "tape archive" instead of joining up a number of files. The "v" directive tells tar to be "verbose" and report all files as they are added.

To separate an archive created by tar into separate files, enter at the shell prompt:

 
  tar -xvf file.tar
You can use gzip in conjunction with tar to create compressed archives. You can create a compressed archive with the following command:
  tar -cvf - inputfile1 inputfile2 | gzip > file.tar.gz
Note: If gzip isn't available on your system, use the Unix compress command instead. In the example above, replace gzip with compress and change the ".gz" extension to ".Z" (compress looks specifically for an uppercase 'Z').

To separate a gzipped tar archive, enter

  gunzip -c file.tar.gz | tar -xvf -
Similarly, to separate a tar archive compressed with the Unix compress command, replace gunzip with uncompress

In Unix, how do I create or decompress zip files?

To create a zip file, enter at the Unix prompt:
  zip filename inputfile1 inputfile2
Replace filename with the name you want to give the zip file. The .zip extension will automatically be appended to the end of the filename and will include the period. Replace inputfile1 and inputfile2 with the names of the files you wish to put into the zip archive. You can include any number of files here or an asterisk (*) to include all files in the current directory.

To include the contents of a directory or directories into a zip archive, use the -r flag:

  zip -r filename directory
This will create the archive filename.zip that contains the files and sub-directories of directory.

Files created by zip can normally be decoded by programs like PKZIP, but especially if the zip archive is to be transferred to DOS, you should use the -k flag. This will simulate a zip file created by PKZIP and will shorten long Unix filenames.

To de-archive a zip file in Unix, use the unzip command. Enter at the Unix prompt:

  unzip filename
Replace filename with the name of the zip archive.

For more information about zip and unzip, see their manual pages:
man zip
man unzip


In Unix, how can I read a file that ends in .Z, .z, or .gz?

A file whose name ends in .Z, .z, or .gz has been compressed, so that it takes up less disk space. Unfortunately, compressed files can't be read like normal files, so they must first be expanded, or "uncompressed". How you do that depends on the program used to compress the file in the first place.

Files whose names end in .Z were compressed with the "compress" program. To expand such a file, enter at the Unix prompt:

  uncompress filename
Replace "filename" with the name of the file. It doesn't matter if you include the ".Z" at the end.

Files ending in .z or .gz were compressed with "gzip", a newer and much better program than compress. To uncompress a gzipped file, enter at the Unix prompt:

  gunzip filename
Again, replace "filename" with the name of your file. It doesn't matter whether you include the ".gz".