Beruflich Dokumente
Kultur Dokumente
The user should realise that the name UNIX does NOT refer to a single well-defined collection
of software (even though the name was originally meant to imply "unique system"!), and that
there is no such thing as "standard UNIX". UNIX is rather a generic term for a number of
superficially similar collections of software that differ in their detailed modes of operation,
depending on the supplier of the OS software (usually but not always the same supplier of the
CPU). These apparently arbitrary variations are a major source of confusion and frustration
particularly for novice users.
2. Basic concepts.
2.1. The UNIX nut: kernel and shell.
The main control program in a UNIX OS is called the kernel. However the kernel does not
allow the user to give it commands directly; instead when the user types commands on the
keyboard they are read by another program in the OS called a shell which parses, checks,
translates and massages them in various ways to be described later, then passes them to the
kernel for execution. (The analogy is with a nut - as in walnut - the important part of which is
the kernel inside; the shell is merely the face that the nut presents to the outside world!) Modern
shells aim to reduce the amount the user has to type by providing facilities such as command
recall and edit, command spelling correction, command or filename completion, and "wildcard"
characters (characters in filenames that represent multiple possibilities).
There are a number of different shells available, with names such as sh, csh, tcsh, ksh, bash, each
with different rules of syntax; these are partly though not completely responsible for the diversity
of UNIX. Once the command has been interpreted and executed, the kernel sends its reply,
which may simply be a prompt for the next command to be entered, either directly to the display
monitor, or more usually if a windowing system (e.g. X-windows) is in operation, via a display
manager (e.g. xdm). This is a program responsible for deciding where and in what form the
output will appear on the display monitor. If for any reason the kernel cannot perform the
command requested (wrong syntax, for example), the reply will be an error message; the user
must then re-enter the corrected command.
The kernel and the shell programs running in the CPU are examples of processes; these are self-
contained programs that may take over complete control of the CPU. Although there can only be
one kernel process running in a particular CPU, there may be any number of shell and other
processes, subject of course to memory limitations.
Some commands to the shell are internal (or built-in), that is they only involve the shell and the
kernel. Others are external and may be supplied with the OS, or may be user-written. An
external command is the name of a file which contains either a single executable program or a
script. The latter is a text file, the first line of which contains the name of a file containing an
executable program, usually but not necessarily a shell, followed by a sequence of commands for
that program. A script may also invoke other scripts - including itself! Its purpose is simply to
avoid having to re-type all the command it contains.
A command may also be an alias for an internal or external command (e.g. the user may not like
the UNIX name "rm" for the command which deletes files, and may prefer to alias it to "delete").
The external command may optionally cause execution of the shell process to be temporarily
suspended, and then run another program, which may then take over input from the keyboard
and mouse and send output for display. The shell may or may not wait for the program to finish,
before it wakes up again and cause its prompt to be displayed. It is very important that the user
be continuously aware of which process is currently reading keyboard input: the shell or another
program, because they usually speak completely different languages!
The above is an example of a parent process - the shell, and a child process - the external
program. In fact the child could just as well have been, and often is, another invocation of the
same shell, or of a different shell, and the child process can be the parent of other child and so on
(almost) ad infinitum. Consequently a typical UNIX system has many processes either waiting
or running.
Many UNIX commands by themselves perform very simple tasks; the power of UNIX derives
from its capability to combine these in order to perform more complex tasks. Commands may be
run sequentially so that the output which results when each command acts on its input data
becomes the input for the next command in the sequence - this is called a pipe. Shell scripts may
also make use of pipes and other control structures such as variables, condition tests and loops.
UNIX also has extensive facilities for inter-process communication (in addition to the pipe
mechanism).
The user needs to be aware that there are 2 basic ways of running jobs (i.e. commands, programs
and scripts) on a UNIX system: foreground and background. There is also batch mode, but
this is similar to background, with the exception that the jobs are held in a batch queue for
sequential execution instead of all being run simultaneously. In foreground mode the parent
process is suspended while the child process runs, taking over the keyboard and monitor; when
the child process terminates the parent process resumes from where it was suspended.
In background mode the parent process continues to run using the keyboard and monitor as
before, while the child process runs asynchronously. In this case it is advisable that the child
process get all its input and send all its output to or from files and not to or from the keyboard
and monitor, in order to avoid confusion with the parent process's input and output. When the
child process terminates, either normally or by user intervention, the event has no effect on the
parent process, though the user is informed by a message sent to the display. The user may also
elect to place a child process already running in foreground into the background and resume the
parent process, or to bring a background process into the foreground. The user may actively
terminate (kill) any process, provided he/she owns it (the user owns any process he/she initiates
plus all descendants of that process), or downgrade (but not upgrade!) the priority of any owned
process.
Like other OS's UNIX organises information into files, and related files may be conveniently
organised in directories. Files may contain text, data, executable programs, scripts (which are
actually just data for a scripting program such as a shell), and may also be links to other files, or
to physical devices or communications channels.
The directory structure is hierarchical with the root directory, indicated by a forward slash (/), at
the base of the tree. This may contain files as well as other directories such as /bin, /etc, /lib,
/tmp, /usr, and /d. Actually in this example the root directory will contain directory files called
bin, etc, lib, tmp, usr and d, each of which contains a list of files and their locations on the disk
for each of the corresponding directories. Each directory may contain further ordinary files and
directory files, and so on. The / character is used to delimit the components of the name. For
example the /d directory may contain directories such as /d/user1 and /d/user2 and these may
contain the users' home directories such as /d/user1/tom, /d/user1/dick and /d/user2/harry, which
are the directories that users tom, dick and harry respectively normally see when they first login
to the system.
The entire directory hierarchy may reside on a single physical disk, or it may be spread across
several disks; the boundaries between physical disks cannot be seen merely by looking at the
directory hierarchy. Your system administrator will decide where your home directory is to be
both physically and logically located.
Every file in the hierarchy is identified by a pathname, this is merely a description of the the
path you have to traverse to get from the root directory to the file. E.g. the file junk.data (not a
wise choice for a file containing data you wish to keep!) in directory /d/user1/tom/keep has the
pathname /d/user1/tom/keep/junk.data . This is an absolute pathname, because it starts with a / .
The pathname of a directory may be optionally terminated by a / ; e.g. in the example above
/d/user1/ means the same as /d/user1 . Strictly, a filename is distinct from a pathname; a
filename is just one of the components of the pathname delimited by /'s.
Relative pathnames which have a start point anywhere but / in the hierarchy may be used
instead, and are often more convenient. Starting from /d/user1/tom/keep the relative pathname
of the previous example is just junk.data or ./junk.data (because "." means the current directory).
Starting from user tom's home directory it would be keep/junk.data. User dick in his home
directory might refer to the same file either by its absolute pathname or by the name
../tom/keep/junk.data (".." means the parent directory which contains the current directory). User
harry residing in /d/user2/harry would have to use the absolute pathname or
../../user1/tom/keep/junk.data .
A useful shorthand for the home directory is ~ (though this syntax is not supported by all shells),
so Tom can also refer to the same file by the absolute pathname ~/keep/junk.data, and Harry can
use ~tom/keep/junk.data .
Another shorthand is the use of "wildcard" characters in filenames, technically called filename
globbing. The * character in a filename represents any string of characters, including no
characters; the ? character represents any single character; a string of characters between []
represents any one of those specific characters. These wildcard characters may be used more
than once, and may appear in combination in a pathname. Thus * by itself would be expanded to
a complete list of filenames in the current directory, whereas ? would be expanded only to a list
of 1 character filenames (if any), ?? to a list of 2 character filenames, and so on. If there are files
called "fee", "fie", "foe" and "fum" in the current directory, the strings f* and f?? would be
expanded to the full list of names, whereas f?e and "f[eio]e" would be expanded to "fee fie foe"
("fum" doesn't match the last two patterns). It is important to understand that it is the shell that is
responsible for this wildcard expansion, and it does it completely mechanically without regard
for any "intended" meaning (see example of the mv in Unix exercises section).
Last, but not least, note that UNIX is always fussy about the case of letters in commands,
usernames, passwords and filenames; so Junk.data is not the same file as junk.data .
Each instance of the shell has its own environment of aliases, local variables and global (or
environment) variables. Aliases and local variables are local to the shell, and are not passed on
to any other process, whether parent or child. Global variables are inherited from the parent by
the child, but not vice versa. Local variables are conventionally given lower case names, while
global variables have upper case names. Global variables include the home directory (HOME),
the path (PATH), the current directory (PWD), the shell (SHELL), and the terminal type
(TERM).
The path is a list of directory filenames which is searched for external commands or scripts
whenever the command or script name does not contain a directory specification; if there is more
than one instance of the file in the path, the first one found is executed. Note that the alias list is
always searched first, followed by the list of built-in commands, followed by the directories in
the path in sequence. If the command contains a directory specification (e.g. ./run) none of the
alias, built-in command or path lists are checked.
The general form of a UNIX command is: command [option(s)] [argument(s)] (the [] here
indicate that the items they contain are optional; they are not part of the syntax).
If a typing error is made the line may be changed using the left/right arrow keys to move the
cursor and the Backspace key to delete the character to the left of the cursor; new characters are
inserted before the cursor. When the line is correct use the Enter key to execute the command.
It is not necessary to move the cursor to the end of the line before pressing Enter. The command
may be cancelled before execution by using Ctrl-u (hold down the Ctrl key and press the u key).
If an incorrectly spelled command is entered and spelling correction is enabled in the shell, the
shell will attempt to correct the mistake and ask for verification.
If the shell has filename completion enabled, use of the Tab key after part of a command or
filename has been typed will cause the shell to attempt completion of the name, up to the
character where the result is unique. Use of the Ctrl-d key will cause all names that match what
has been typed to be listed.
The options and arguments if present must be separated from the preceding item by a least 1
space. However multiple options may or may not need to be separated from each other by at
least 1 space; multiple arguments are always separated from each other by at least 1 space.
Options usually start with -, but occasionally it is a +, and sometimes the - or + may be omitted.
A line may contain several commands (each possibly followed by options and/or arguments)
separated by semicolons (;). The commands are executed in sequence, just as if they had been
typed on separate lines. If it is necessary to continue a command onto the next line, end the line
with a backslash (\), press Enter and continue typing on the next line.
Knowing how to get information from the computer is always a first priority! The most common
method is by means of the "manual pages", by typing man command where command is what
you want information on. Unfortunately this doesn't help if you don't know what you are looking
for, and UNIX's command names are far from obvious! You can try typing man -k word .
This searches the headings of all the manual pages for word, and may produce both useless and
useful information on what to try next! Some systems have info installed; this is much better
organised than man, though it may not be complete. Type info followed by h if you're a first-
time user, then follow the instructions. Typing help on some systems gives you information on
the built-in shell commands; alternatively try man csh (or whatever shell you're using).
3. UNIX commands.
3.1. Process and screen control.
Note that some of the Ctrl key functions may have been altered by an stty command; use stty -a
to get the current settings.
Key Function
Ctrl-c Kill foreground process
Ctrl-z Suspend foreground process
Ctrl-d Terminate input, or exit shell
Ctrl-s Suspend output
Ctrl-q Resume output
Ctrl-o Discard output
Ctrl-l Clear screen
Key Function
Tab Complete filename up to next non-unique character
Ctrl-d List filenames matching partial name
Operator Function
< Redirect standard input from file
<< Redirect standard input from command source
> Redirect standard output to file
>! Redirect standard output and overwrite file
>> Redirect standard output and append to file
>>! Redirect standard output to file or append to file
>& Redirect standard output/error to file
>>& Redirect standard output/error and append to file
>>&! Redirect standard output/error to file or append to file
| Pipe standard output to standard input
|& Pipe standard output/error to standard input
`command` Replace command with its output
This table and the next should be used in conjunction with the DOS "help" and UNIX "man"
commands on the particular operating system in use to check the definitive specification of the
command options and arguments, as these often vary between different implementations of the
OS. Note that {} indicates a required variable argument; [] indicates an optional one.
3.6.3. Networking.
3.6.4. Programming.
Note that the above file and buffer manipulation commands are not needed for versions of Emacs
that run in an X-window and have a File/Buffers menu bar. In the following "motion"
commands note that the Alt Gr key sometimes works as Alt.
Some of the above functions are not implemented in some versions of Emacs.
6. Unix exercises.
# <-- Note this means a comment - you don't have to type the comments!
# DON'T FORGET TO USE LINE RECALL AND EDIT - IT WILL SAVE YOU A LOT OF TYPING!
cat file1.c
cat file2.c
mv file?.c file?.C # Note Unix distinguishes lower and upper case!
cat file1.C # What happened to your .c files? Explain why!
cat file2.C
unix-beg.html
© IJT 06-Oct-1999