quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif The Shell

Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell. The shell is your interface to the operating system. It acts as a command interpreter; it takes each command and passes it to the operating system. It then displays the results of this operation on your screen. There are several shells in widespread use. The most common ones are described below.

   

space.gif

Shell

Description

Bourne shell (sh)

Original Unix shell written by Steve Bourne of Bell Labs. Available on all UNIX systems. Does not have the interactive facilites provided by modern shells such as the C shell and Korn shell. The Bourne shell does provide an easy to use language with which you can write shell scripts.

C shell (csh)

Written at the University of California, Berkley. As it name indicates, it provides a C like language with which to write shell scripts.

Korn shell (ksh)

Written by David Korn of bell labs. It is now provided as the standard shell on Unix systems. Provides all the features of the C and TC shells together with a shell programming language similar to that of the original Bourne shell.

TC Shell (tcsh)

Available in the public domain. It provides all the features of the C shell together with EMACS style editing of the command line.

Bourne Again Shell (bash)

Public domain shell written by the Free Software Foundation under their GNU initiative. Ultimately it is intended to be a full implementation of the IEEE Posix Shell and Tools specification. Widely used within the academic commnity. Provides all the interactive features of the C shell (csh) and the Korn shell (ksh). Its programming language is compatible with the Bourne shell (sh).

Your login shell is usually established by the local System Administrator when your userid is created. You can determine your login shell with the command:

   

space.gif

echo $SHELL

   

space.gif

Each shell has a default prompt. For the 5 most common shells:

   

space.gif

$ (dollar sign) - sh, ksh, bash

% (percent sign) - csh, tcsh

   

space.gif

Depending upon the shell, certain features will be available. The table below summarizes the features available with the 5 most common shells. Note: these features will be described in detail later.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Processes

Whenever you enter a command at the shell prompt, it invokes a program. While this program is running it is called a process. Your login shell is also a process, created for you upon logging in and existing until you logout.

   

space.gif

UNIX is a multi-tasking operating system. Any user can have multiple processes running simultaneously, including multiple login sessions. As you do your work within the login shell, each command creates at least one new process while it executes.

   

space.gif

Process id: every process in a UNIX system has a unique PID - process identifier.

   

space.gif

ps - displays information about processes. Note that the ps command differs between different UNIX systems - see the local ps man page for details.

   

space.gif

To see your current shell's processes:

   

space.gif

% ps

PID TTY TIME CMD

46450 pts/9 0:00 ps

46801 pts/9 0:00 -csh

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Controlling processes associated with the current shell

Most shells provide sophisticated job control facilities that let you control many running jobs (i.e. processes) at the same time. This is useful if, for example, you are editing a text file and want to interrupt your editing to do something else. With job control, you can suspend the editor, go back to the shell prompt, and start work on something else. When you are finished, you can switch back to the editor and continue as if you hadn't left.

   

space.gif

Jobs can either be in the foreground or the background. There can be only one job in the foreground at any time. The foreground job has control of the shell with which you interact - it receives input from the keyboard and sends output to the screen. Jobs in the background do not receive input from the terminal, generally running along quietly without the need for interaction (and drawing it to your attention if they do).

   

space.gif

The foreground job may be suspended, i.e. temporarily stopped, by pressing the Ctrl-Z key. A suspended job can be made to continue running in the foreground or background as needed by typing "fg" or "bg" respectively. Note that suspending a job is very different from interrupting a job (by pressing the interrupt key, usually Ctrl-C); interrupted jobs are killed off permanently and cannot be resumed.

   

space.gif

Background jobs can also be run directly from the command line, by appending a '&' character to the command line. For example:

   

space.gif

$ find / -print 1>output 2>errors &

[1] 27501

$

   

space.gif

Here the [1] returned by the shell represents the job number of the background process, and the 27501 is the PID of the process. To see a list of all the jobs associated with the current shell, type jobs:

   

space.gif

$ jobs

[1]+ Running find / -print 1>output 2>errors &

$

   

space.gif

Note that if you have more than one job you can refer to the job as %n where n is the job number. So for example fg %3 resumes job number 3 in the foreground.

   

space.gif

To find out the process ID's of the underlying processes associated with the shell and its jobs, use ps (process show):

   

space.gif

$ ps

PID TTY TIME CMD

17717 pts/10 00:00:00 bash

27501 pts/10 00:00:01 find

27502 pts/10 00:00:00 ps

   

space.gif

So here the PID of the shell (bash) is 17717, the PID of find is 27501 and the PID of ps is 27502.

   

space.gif

To terminate a process or job abrubtly, use the kill command. kill allows jobs to referred to in two ways - by their PID or by their job number. So

$ kill %1

or

$ kill 27501

   

space.gif

would terminate the find process. Actually kill only sends the process a signal requesting it shutdown and exit gracefully (the SIGTERM signal), so this may not always work. To force a process to terminate abruptly (and with a higher probability of sucess), use a -9 option (the SIGKILL signal):

   

space.gif

$ kill -9 27501

   

space.gif

kill can be used to send many other types of signals to running processes. For example a -19 option (SIGSTOP) will suspend a running process. To see a list of such signals, run kill -l.

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Controlling other processes

You can also use ps to show all processes running on the machine (not just the processes in your current shell):

   

space.gif

$ ps -fae

   

space.gif

ps -aeH displays a full process hierarchy (including the init process).

   

space.gif

Many UNIX versions have a system utility called top that provides an interactive way to monitor system activity. Detailed statistics about currently running processes are displayed and constantly refreshed. Processes are displayed in order of CPU utilization. Useful keys in top are:

   

space.gif

s - set update frequency k - kill process (by PID)

u - display processes of one user q - quit

   

space.gif

On some systems, the utility w is a non-interactive substitute for top.

   

space.gif

One other useful process control utility that can be found on most UNIX systems is the killall command. You can use killall to kill processes by name instead of PID or job number. So another way to kill off our background find process (along with any another find processes we are running) would be:

   

space.gif

$ killall find

[1]+ Terminated find / -print 1>output 2>errors

$

   

space.gif

Note that, for obvious security reasons, you can only kill processes that belong to you (unless you are the superuser).

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Redirection input and output

The output from programs is usually written to the screen, while their input usually comes from the keyboard (if no file arguments are given). In technical terms, we say that processes usually write to standard output (the screen) and take their input from standard input (the keyboard). There is in fact another output channel called standard error, where processes write their error messages; by default error messages are also sent to the screen.

   

space.gif

To redirect standard output to a file instead of the screen, we use the > operator:

   

space.gif

$ echo hello

hello

$ echo hello > output

$ cat output

hello

   

space.gif

In this case, the contents of the file output will be destroyed if the file already exists. If instead we want to append the output of the echo command to the file, we can use the >> operator:

   

space.gif

$ echo bye >> output

$ cat output

hello

bye

   

space.gif

To capture standard error, prefix the > operator with a 2 (in UNIX the file numbers 0, 1 and 2 are assigned to standard input, standard output and standard error respectively), e.g.:

   

space.gif

$ cat nonexistent 2>errors

$ cat errors

cat: nonexistent: No such file or directory

$

   

space.gif

You can redirect standard error and standard output to two different files:

   

space.gif

$ find . -print 1>errors 2>files

   

space.gif

or to the same file:

   

space.gif

$ find . -print 1>output 2>output or

$ find . -print >& output

   

space.gif

Standard input can also be redirected using the < operator, so that input is read from a file instead of the keyboard:

   

space.gif

$ cat < output

hello

bye

   

space.gif

You can combine input redirection with output redirection, but be careful not to use the same filename in both places. For example:

   

space.gif

$ cat < output > output

   

space.gif

will destroy the contents of the file output. This is because the first thing the shell does when it sees the > operator is to create an empty file ready for the output.

   

space.gif

One last point to note is that we can pass standard output to system utilities that require filenames as "-":

   

space.gif

$ cat package.tar.gz | gzip -d | tar tvf -

   

space.gif

Here the output of the gzip -d command is used as the input file to the tar command.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Pipes

The pipe ('|') operator is used to create concurrently executing processes that pass data directly to one another. It is useful for combining system utilities to perform more complex functions. For example:

   

space.gif

$ cat hello.txt | sort | uniq

   

space.gif

creates three processes (corresponding to cat, sort and uniq) which execute concurrently. As they execute, the output of the who process is passed on to the sort process which is in turn passed on to the uniq process. uniq displays its output on the screen (a sorted list of users with duplicate lines removed). Similarly:

   

space.gif

$ cat hello.txt | grep "dog" | grep -v "cat"

   

space.gif

finds all lines in hello.txt that contain the string "dog" but do not contain the string "cat".

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Filters

A filter is a command that processes an input stream of data to produce an output stream of data. Command lines which use a filter will include a pipes to connect it to the stdout of one process and the stdin of another process.

   

space.gif

For example, the command line below takes the output of "who" and sorts it. The sorted output is then passed to the lp command for printing. In this example, sort is a filter.

   

space.gif

who | sort | lp

   

space.gif

Both filters and pipes demonstrate a basic UNIX principle: Expect the output of every program to become the input of another, yet unknown, program to combine simple tools to perform complex tasks.

   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com