Você está na página 1de 114

STUDENT REFERENCE

UNIX Basics

globsyn technologies
XI - 11 & 12, Block - EP, Sector - V, Salt Lake Electronics Complex,
Calcutta - 700091, India

Globsyn
SR/UNIX/401/0204/SC/1.1

All rights reserved. No part of this book shall be reproduced, stored in a


retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from the
publisher. No patent liability is assumed with respect to the use of the
information contained herein.

Globsyn
UNIX

Globsyn
UNIX

Module Objectives

Introduction to UNIX -----------------------------------------------------------------------------------------2


History of UNIX ------------------------------------------------------------------------------ 3
UNIX – a Multi- User Operating System ----------------------------------------------------- 3
Unix Shell ---------------------------------------------------------------------------------------------------------5
Unix Architecture ----------------------------------------------------------------------------- 6
Types of Shell --------------------------------------------------------------------------------- 7
Shell Commands ------------------------------------------------------------------------------ 7
Filter ----------------------------------------------------------------------------------------- 41
Introduction to vi Editor ----------------------------------------------------------------------------------- 63
Introduction ---------------------------------------------------------------------------------- 64
Before You Begin---------------------------------------------------------------------------- 64
Starting the vi Editor------------------------------------------------------------------------- 65
Getting Out of vi ----------------------------------------------------------------------------- 65
The Two Modes of vi ------------------------------------------------------------------------ 66
Some Simple vi Commands ----------------------------------------------------------------- 66
Text Buffers in vi ---------------------------------------------------------------------------- 67
Cutting and Yanking ------------------------------------------------------------------------- 67
Pasting --------------------------------------------------------------------------------------- 68
Settings for vi -------------------------------------------------------------------------------- 68
Abbreviations and Mapping Keys to Other Keys -------------------------------------------- 69
The EXINIT Environment Variable and the .exrc file--------------------------------------- 70
Recovering Your Work When Something Goes Wrong with Your Terminal--------------- 70
Warning About Using vi on the Workstations----------------------------------------------- 71
Shell Programming ------------------------------------------------------------------------------------------ 72
Introduction to shell programming ---------------------------------------------------------- 73
Variables ------------------------------------------------------------------------------------- 74
Accept and Display User Input -------------------------------------------------------------- 77
Environment Variables ---------------------------------------------------------------------- 80
Positional Parameters ------------------------------------------------------------------------ 84
Command substitution ----------------------------------------------------------------------- 85
Conditional statement – the if construct ----------------------------------------------------- 87
Conditional statement – the case construct -------------------------------------------------- 95
Iteration – the while statement --------------------------------------------------------------- 98
Iteration – the until statement -------------------------------------------------------------- 101
Iteration – the for statement---------------------------------------------------------------- 101
Controlling loops – break and continue ---------------------------------------------------- 103
Introduction to EXCEED ---------------------------------------------------------------------------------108
About Exceed ------------------------------------------------------------------------------ 109
X Window Systems and Exceed ----------------------------------------------------------- 109
Exceed Applications ----------------------------------------------------------------------- 109
Connecting to Hosts ----------------------------------------------------------------------- 110

SR/UNIX/401/0204/SC/1.1 1
UNIX

Introduction to UNIX

At the end of this chapter, you will be able to:

Appreciate History of UNIX


Appreciate UNIX as Multi-user Operating System

SR/UNIX/401/0204/SC/1.1 2
UNIX

History of UNIX

Unix history goes back to 1969 and the famous "little-used PDP-7 in a corner" on which
Ken Thompson, Dennis Ritchie (the R in K&R) and others started work on what was to
become Unix. The name "Unix" was intended as a pun on Multics (and was written
"Unics" at first -- UNiplexed Information and Computing System).

For the first 10 years, Unix development was essentially confined to Bell Labs. These
initial versions were labeled "Version n" or "Nth Edition" (of the manuals), and were for
DEC's PDP-11 (16 bits) and later VAXen (32 bits). Some significant versions
include:

V1 (1971): 1st Unix version, in assembler on a PDP-11/20. Included file system, fork(),
roff, ed. Was used as a text-processing tool for preparation of patents. Pipe() appeared
first in V2!

V4 (1973): Rewritten in C, which is probably the most significant event in this OS's
history: it means Unix can be ported to a new hardware in months, and changes are easy.
The C language was originally designed for the Unix operating system, and hence there
is a strong synergy between C and Unix.

V6 (1975): First version of Unix widely available outside Bell Labs (esp. in
universities). This was also the start of Unix diversity and popularity. 1.xBSD (PDP-11)
was derived from this version. J. Lions published "A commentary on the Unix Operating
System" based on V6.

V7 (1979): For many, this is the "last true Unix", an "improvement over all preceding
and following Unices" [Bourne]. It included full K&R C, uucp, Bourne shell. V7 was
ported to the VAX as 32V. The V7 kernel was a mere 40 Kbytes!

UNIX – a Multi-User Operating System

A multi-user operating system consists of one single computer, and several terminals are
attached to it. The users of the different terminals can access the operating system and the
resources through the network. The terminals can be of two types:

Ø Dumb: The terminal having its own VDU, Keyboard and Mouse but does not
have a CPU of its own. The processing is done only at the machine where the
operating system is loaded.

SR/UNIX/401/0204/SC/1.1 3
UNIX

Ø Smart: The smart terminals are having their own CPU’s and can work
independently. This can also connect to another operating system or access other
resources.

UNIX is known as portable true 16-bit multi-user operating system. It supports multiple
users to be logged on concurrently, thus leading to multi-tasking and time-sharing
operating system.

Multi-Programming

The concept of executing more than one program simultaneously by different users is
known as multi-programming.

Time–Sharing

The concept of multi-programming is implemented through time sharing and multi-


tasking. As the only CPU is taking care of the different programs for execution there, the
entire time of the CPU is to be distributed amongst the different programs waiting for the
execution. Here every program is assigned a specific period of CPU time, after that it is
passed to another process waiting in the queue.

Multi-Tasking
A program is broken into different tasks and each task is treated as separate activity. So,
when a program is waiting for the completion of one task, the CPU, rather than wasting
time, starts the execution of another task.

It is also the responsibility of the process management system to allocate the CPU time to
all the processes in execution (a single login may have more than one process). UNIX
maintains its own security mechanism for restricting the file tampering.

Differentiate between Dumb and Smart terminal?

SR/UNIX/401/0204/SC/1.1 4
UNIX

Unix Shell

At the end of this chapter you will be able to:

Appreciate UNIX Architecture


Know Types of Shell
Learn Shell Commands
Appreciate Filters

SR/UNIX/401/0204/SC/1.1 5
UNIX

Unix Architecture

Unix is known to be layered operating system. It mainly consists of three components.


They are:

Ø Kernel: It is the heart of the Unix OS. It is the innermost layer of the operating
system that provides a uniform interface to the hardware. When the machine
starts, the Boot Loader passes the control to kernel. It is responsible for managing
the peripherals such as VDU, mouse, keyboard etc. It also deals with memory
management and process management. The kernel does not deal directly with the
user; instead it creates a new startup, interactive process for the user–shell.
Ø Shell: As user can’t interact directly with the hardware, shell acts as an interface
between the end-user and the kernel. It is another utility program and is created
automatically by the OS the moment user logs on. This shell is known as login
shell. The shell is acting as a command interpreter between the end-user and the
kernel. The shell is responsible for background processing, I/O redirection, pipes
and filtering, variable and programming language constructs.

End-User

Commands

Shell

Kernel

Hardware

Application programs: These are the special programs created by the user for
performing specific tasks. These utilities are provided with the operating system software
and are also available from the software vendors. They are generally developed in C
language or using the shell programming.

What is the function of kernel?

SR/UNIX/401/0204/SC/1.1 6
UNIX

Types of Shell
The instructions given by the user are submitted to the OS kernel via its shell in a format
that the kernel can understand. Similarly the result returned by the kernel is interpreted in
a user-friendly way by the shell. Any error messages displayed on the screen are also the
task of the shell.

Apart from this, a shell may house several utilities, compilers, facilities for programming
constructs etc.

Unix supports many shells, as follows:

Ø sh: This is the UNIX Bourne Shell, developed at AT&T and is named after the
person who has developed it, Stephen R. Bourne.
Ø c shell: This is another shell developed at the University of California, which
supports some syntaxes resembling with ‘C’ language.
Ø korn shell: This is Korn Shell, developed after the name of the developer David
Korn, which comprises of the best features of C Shell and Bourne Shell.
Ø Others include:
• tcsh: An enhanced version of the C shell.
• ash: A shell suitable when memory runs low on the machine.
• zsh: A Bourne compatible shell.

Shell Commands

The efficiency of a Shell lies in its power to do processing and command interpretation,
which are the most important functions of a shell. UNIX variants, which were originally
built with the idea of supporting technical people, had a lot of features provided by their
shells. These shells supported various types of commands, utilities and powerful
programming constructs, which had the ability to create programs as good as those
written in a language.

UNIX shell commands can be broadly divided into three categories:

Ø System: These commands give information about the environment, where the user is
working. Examples of such commands are who, date, clear, banner, passwd, etc.

Ø File & Directory: To manipulate the files and directories we need some commands
like: pwd, ls, mkdir, cd, rmdir, mv, cp, rm, cmp, comm., chown, chgrp, chmod, head,
tail etc.

Ø Communication: This is an important aspect in multi-user systems. There are


commands like write, mail etc.

SR/UNIX/401/0204/SC/1.1 7
UNIX

The date Command

System commands give information about the environment in which the user is working.
Some of the system commands are as follows:

Lets start with the date command, which is supposed to display the current date and time

$ date

The output is as follows:

To customize the output, the date command can be used with arguments. To provide
argument with the date command, -u option is used. The syntax for issuing the date
command along with an argument is as follows:

date –u +format

Some of the frequently used date format commands are given below:

Ø To display the full weekday name, the %A option is used.

$date –u +%A

Ø To display the full month name, the %B option is used.

$date –u +%B

SR/UNIX/401/0204/SC/1.1 8
UNIX

Ø To display the date in MM/DD/YY format, the %D option is used.

$date –u +%D

Ø To display current time in hh:mm:ss (24 hour clock) format, the %T option is used.

$date –u +%T

The collective information of all the field descriptors that can be used with the date
command are given in the table below:

Field descriptors Description


%a Abbreviated weekday name.
%A Full weekday name.
%b Abbreviated month name.
%B Full month name.
%c Current date and time.
%d Day of the month, 01 to 31.
%D Date as MM/DD/YY.
%H Hour, 00 to 23.
%I Hour 01 to 12.

SR/UNIX/401/0204/SC/1.1 9
UNIX

%j Day of the year, 001 to 366.


%M Minute, 00 to 59.
%S Second, 00 to 61.
%T Time as hh:mm:ss (24 hour clock).
%u Weekday as decimal number, Monday represented as 1.
%U Week number of the year, Sunday as the first day of the week.
%V Week number of the year, Monday as the first day of the week,
ranging from 01 to 53
%w Day of the week, Sunday represented as 0.

The date command can also be used along with its options without the use of –u. in this
case, the field descriptors (%a, %b etc.) must be specified within quotes.

$date +”%A, %B %d”

The output of the above command is given below:

View the manual pages of UNIX commands


Sometimes, for a beginner, it is required to get information about a particular UNIX
command like the different options of a command. UNIX provides the man command
through which a user can get help on a particular command. The man command should
be followed by the command name on which the user desires the help. The man
command displays the manual pages of the desired command.

$ man date

The above command displays the manual page of the date command. A part of the output
is given below:

SR/UNIX/401/0204/SC/1.1 10
UNIX

Login and Logout


The most important feature of any multi user system is its security feature. UNIX
authenticates by the login prompt.

Login screen is prompted every time the user wants to start a new session in UNIX. The
screen asks for a valid name & password and if correct it gives the “$” prompt to an
ordinary user or a “#” prompt to the administrator (root user).

To terminate a session, logout from the shell by typing ‘exit’ or Ctrl + d.

uname : KNOW YOUR MACHINE’S NAME


By default, the uname utility will write the operating system name to standard output.
When options are specified, symbols representing one or more system characteristics will
be written to the standard output. The format and contents of the symbols are
implementation-dependent
If your machines is connected to a network, then it must have a name.

If your network is connected to the Internet, then the name is required to frame your
machine’s domain name. The uname command with –n option tells you the machine
name in your network :

$ uname –n
Kaushik

SR/UNIX/401/0204/SC/1.1 11
UNIX

Many unix networking utilities use the machine’s name as an argument. In order to copy
files from some machine, one has to know that machine’s name. uname with –r option
shows OS’s version number. For example :

$ uname –r
3.2

which expands to UNIX system V Release 3.2.

the other options are :

-a
Behave as though all of the options -mnrsv were specified.

-m
Write the name of the hardware type on which the system is running to standard output.

-n
Write the name of this node within an implementation-dependent communications
network.

-r
Write the current release level of the operating system implementation.

-s
Write the name of the implementation of the operating system.

-v
Write the current version level of this release of the operating system implementation.

SR/UNIX/401/0204/SC/1.1 12
UNIX

Displaying calendar

In UNIX it is possible to display the calendar of any year or month from 1 AD upto 9999
AD. The following command need to be typed from the ‘$’ prompt

$ cal

The cal command can also be used to display the calendar of a particular month and year.

$ cal 5 1975

where 5 is the month and 1975 is the year. The output of the following command is given
in the next page.

SR/UNIX/401/0204/SC/1.1 13
UNIX

The cal command without any argument displays the


calendar of the current month along with its previous and
next month. The cal command with a year displays all the
12 months of that year e.g. cal 1975. cal command with
the month number and the year number will display the
calendar of that specific month of that year.

Online calculator
UNIX provides the user with its on-line calculator. The user needs to type at the ‘$’
prompt is:

$ bc

After issuing this command UNIX will wait for some input. User will enter some
mathematical expressions and the moment the return key is pressed, the calculated value
will be displayed on the screen.

To come out of the calculator, type quit and then press <Enter> or press <ctrl+d>. The $
prompt will be displayed.

The calculator can be used to calculate the square root of a


particular number. The sqrt(number) function is used to
achieve the purpose.

Current directory of the User

When users logs into the UNIX system, for the sake of security a user is directly taken to
a particular directory. In this directory he has the permission to create files and
directories. This directory is called that user’s home directory. Suppose, five users are
working in the UNIX environment. Therefore, for each user directories like user1, user2,
user3 and so on is generally created under /usr directory. So, when user2 logs into the
system, UNIX will directly enter into /usr/user2 as his home directory, which means

SR/UNIX/401/0204/SC/1.1 14
UNIX

/usr/user2 is the default working directory for user2. This is a security measure adopted
by UNIX so that users are forced to work in their respective home directories. This
situation is not possible with Novell Netware. To display the directory where the user is
currently working, the pwd command is used.

$ pwd

It will display the current working directory of the user along with the full path.

List of logged-in users


To know the people working currently in the UNIX, the command to be used is:

$ who

The above command will give three-column output

The first column shows the user name, the second column (pts/0, pts/1) shows the device
names of their respective terminals (this name can be different for different UNIX variant
like LINUX), the third column shows the date and time of logging in.

The frequently used options used with who command are:

Ø who –H displays the same output as above but with column headings.

SR/UNIX/401/0204/SC/1.1 15
UNIX

$who -H

Ø who am i command displays the status of the user who has invoked the command.

$ who am i

List of Processes

To see the processes currently running for a particular user, the command to be used is:

$ps

The frequently used options with the ps command are:

Ø $ps –A will display all the processes running in the system


Ø $ps –u username will display the processes initiated by a particular user

The nohup command: Usually when a process is running and the user logs out of the
system, the process gets terminated forcefully. If it is desired that the process will run
though user has logged out, the nohup command is used.

$nohup vat file|wc -c

SR/UNIX/401/0204/SC/1.1 16
UNIX

Walking through Directories


Lets consider a situation, where an Inventory Control System, a Pay Roll System, an
Invoicing System needs to be stored in the same server. If there is any problem in any
system, will it be possible to find out the desired file? If all files are in such condition,
then it will be very difficult for maintenance. Directories are created to group a particular
set of files. Directories are logical grouping of files for easier accessibility. It can be
compared with different shelves in a library containing books of different subjects. For
example, the physics books are kept in one shelf whereas all the chemistry books are kept
in another shelf. In the same way, for storing files in computer, we group them on the
basis of similarities depending upon some kind of parameters like purpose, the date of
creation of a file.

Let us look into a directory structure diagrammatically:

Science

Physics Chemistry Biology

Once we have learnt the directory structure, let us try to implement them using UNIX
commands. One of the basic needs of any user for working with UNIX is to switch
between the directories.

Suppose a user (user1) has logged on to the system and issues the following command
and presses <Enter>

$ pwd
/home/user1

which shows, his home directory is /home/user1

Now if he executes

$ cd /home/user2

SR/UNIX/401/0204/SC/1.1 17
UNIX

This indicates, he has now changed his current directory to /home/user2, which can be
visualized by

$ pwd
/home/user2

To change from the current directory to the parent directory, the command is:

$ cd ..

$ pwd
/home

To go to the root directory, the command is:

$ cd /
$ pwd
/

The parent directory is the directory, from which the current


directory has been born. In the directory structure, parent
directory is higher in order.

Creating Directories

To create a directory or a subdirectory in UNIX, the mkdir command is used. A


directory can be created from the home directory or any other subdirectory under the
home directory. The command that needs to be issued from the $ prompt is as follows:

$mkdir temp

A directory named temp will be created. This is called relative path reference.

The complete path can also be specified with the mkdir command.

$mkdir /home/user1/temp

This is called absolute path reference.

Absolute Path Reference is the reference of a path in relation


with root..
Relative path Reference is the reference from the current
directory.

SR/UNIX/401/0204/SC/1.1 18
UNIX

Moreover, multiple directories can be created using a single mkdir command. The
directory names that need to be created must be separated by a space.

$mkdir temp1 temp2 temp3

With the above command, three directories called temp1, temp2 and temp3 will be
created under the current directory.

mkdir –m option is used to set the mode (read, write, execute)


for the directory to be created. The modes will be discussed
later in the chmod command.

Removing Directories
A directory or a subdirectory can be removed with the help of rmdir command.

$rmdir temp

Like mkdir, the full path can also be specified while removing a directory.

$rmdir /usr/user1/temp

But a directory can be removed if and only if the following two conditions are satisfied:

The Directory must be empty.


The directory cannot be the current directory or any directory,
higher in the hierarchy.

A directory can be removed forcefully even though if it is not empty. Suppose, a


subdirectory called temp2 exists under a directory called temp1. In this case temp1 can
be removed forcefully though it contains a subdirectory. The following command helps
to achieve the purpose.

$rm –r temp1

The directory temp1 along with all its subdirectories and files will be removed.

The rm –r <directory_name> needs to be used cautiously as it


forcefully removes a directory along with all the subdirectories.

SR/UNIX/401/0204/SC/1.1 19
UNIX

Displaying contents of a Directory


To display the contents of a directory, the ls command is used.

$ls

The above command will display the long listing of files and subdirectories under the
current directory.

The ls command followed by the directory name displays the list of subdirectories and
files under the specified directory. The ls command is used in the following way:

The ls command can also be used with the directory name along with the full path.

$ls /home/user1

The output of the above command is given below:

Some of switches that are frequently used with ls command are given below:

Ø ls –l : This option provides a more detailed description of the files and subdirectories
of the current directory.

$ls -l

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 20
UNIX

1---------2-----3-----------4------------5-------------6--------------7

The column description of the output is given below:

Column Number Description


1 Denotes file type, ordinary (-), directory (d), special files (b, c or p)
and the permissions for different users of the system.
2 Number of links, minimum of 2 for a directory to depict connection
with its parent.
3 User name.
4 Group name, where user belongs.
5 Size of the file in bytes
6 Date and time of creation or modification.
7 Name of the file or directory

Ø ls –t : This option displays the long listing of files and directories in a sorted manner.
Sorting is done on the time of creation or modification. The latest modified file is
displayed first.

$ls –l -t

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 21
UNIX

Ø ls –a: This option displays all the files along with the hidden files. At this stage lets
consider hidden file as some files, which needs to be kept away from the users. In
UNIX, the hidden file names start with a period (.).

$ls -a

The output of the above command is given below:

Ø ls –x : This option sorts the files alphabetically and prints the output row wise.

$ls -x

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 22
UNIX

Ø ls –F : This option helps to distinguish between files and directories. In the output, the
directories are suffixed by a slash (/) symbol.

$ls –F

Copy
Ø To create duplication, we use the cp command.

Ø A file may need to be copied from one directory to another directory cp command
serves that purpose also.

The usage of cp command is given below:

cp <Source_filename> <Destination_filename>

The above usage will copy a file within the same directory but with a different name.

SR/UNIX/401/0204/SC/1.1 23
UNIX

cp <Source_filename> <Destination>

The above usage will copy a file from source directory to a different directory. The name
of the copied file will be same.

Suppose, a file called file1 is present under a directory called temp1. To copy this file
from temp1 to another directory called temp2 (both temp1 and temp2 directories are
present under /usr/user1 and it is also the current working directory), the following
command needs to be issued from the $ prompt:

$cp temp1/file1 temp2

The file will be copied with the same name.

The detailed output is given below:

SR/UNIX/401/0204/SC/1.1 24
UNIX

The above command can also be specified using absolute path reference. The usage is
given below:

$cp /home/user1/temp1/file1 /usr/user1/temp2

Now, if we want to copy the file called file1 with a different name to the temp2 directory,
then the following command needs to be issued:

$cp /home/user1/temp1/file1 /usr/user1/temp2/copiedfile

So file1 under temp1 directory will be copied to temp2 directory with the name
copiedfile.

If a file has to be copied from a different directory to the current


directory, then a “.” must be specified in place of <Destination>,
because . indicates the current directory

Some of the frequently used switches of cp command are given below:

Ø cp –i: This option asks the user for confirmation before copying a file, which is
already existing in the destination directory. It will copy the file only when the user
provides affirmation. If –i is not used, it will directly overwrite the existing file
without prompting the user for confirmation.

$cp –i temp1/file1 temp2

The output of the above command is given below:

The cp command without any option does not ask for any
confirmation, before overwriting. So, be careful!, even some files
may be lost.

SR/UNIX/401/0204/SC/1.1 25
UNIX

Ø cp –r : This option copies the whole file hierarchy. Suppose two directories called
dir1 and dir2 are created. Beneath dir1 one subdirectory called dir3 is created and
within dir3 one file called file1 is created. The skeleton of the said directory structure
is given below:

/usr/user1

dir1 dir2

dir3

file1

Now, issue the following command from the $ prompt remaining in the home directory
(/usr/user1) only:

$cp –r dir1 dir2

After copying, the above directory structure will look like the picture given below. The
copied directories and files are represented by dotted lines.

SR/UNIX/401/0204/SC/1.1 26
UNIX

/usr/user1

dir1 dir2

dir3 dir1

file1 dir3

file1

The sample output is given below:

SR/UNIX/401/0204/SC/1.1 27
UNIX

Moving and Renaming a file


To move a file around the UNIX file system, the mv command is used. The difference
between mv command and the cp command is that after moving using mv command, the
file will not be present in the source directory. The usage of the mv command while
moving a file from directory to another is given below:

mv <Source> <Destination>

The above command will move the file from the source directory to the destination
directory.

$mv demo1/file demo2

A file can be moved from one directory to another directory keeping the same name or
providing a different name. The command given below moves a file with a different
name.

$mv demo3/file demo4/file1

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 28
UNIX

If the target file already exists while moving a file, then the user
will be prompted for a confirmation. If the confirmation reply is
‘y’ then the existing file will be overridden other wise no action
will take place. The mv –f option overrides the existing file
without asking for any permission.

Multiple files can be moved with a single mv command. Use the following:

$mv file1 file2 demo4

where file1 and file2 will be moved to the directory demo4.

The same mv command can be used to rename a particular file within the same directory
only. The usage of mv command to rename a file is given below:

mv <Old_filename> <New_filename>

The above command will change the old file name to a new file name.

$mv demo4/file1 demo4/file2

The output of the above command is given below:

Removing a file
To delete an existing file, the rm command is used. The command given below deletes a
file from the specified directory. The directory name need not be specified for the current
directory.

$rm demo4/file2

SR/UNIX/401/0204/SC/1.1 29
UNIX

The single rm command can be used to delete multiple files.

$rm demo3/file1 demo3/file2

To remove a file in an interactive mode i.e, the user will be prompted for the
confirmation, the rm –i command is used. The command given below deletes a file in an
interactive mode.

$rm –i demo3/file1

The output of the above command is given below:

The rm command when used with –r option deletes all the files in a particular directory.
It deletes the directory also. It is a recursive delete.

$rm –r test

SR/UNIX/401/0204/SC/1.1 30
UNIX

The output of the above command is given below:

Comparison of two files


The cmp command is used to compare content of two files. This command compares two
files and returns the byte position and the line number at which the first difference has
occurred. Suppose two files called file1 and file2 has been created. The contents of two
files are given below:

file1: This is A.

file2: Thes is B.

Now issue the following command from the command prompt:

$cmp file1 file2

The output of the above command is given below:

Comparison of two sorted files


To compare the contents of two sorted files line by line, the comm command is used.
This command selects or rejects lines common to both the files. Suppose two files called
file1 and file2 is created. The content of these two files are given below:

file1 file2

SR/UNIX/401/0204/SC/1.1 31
UNIX

My File My File
The first one. The second one.

Now, issue the following command from the command prompt:

$comm file1 file2

The above command produces a three-line output. The first column displays the line
unique to the first file (file1). The second column displays the line unique to the second
file (file2). The third column displays the line common to both the files.

The output of the above command is given below:

With the comm command, the flags 1,2 or 3 can be used to customize the output. The
detail is given in the table below:

Command Description
comm –1 file1 file2 Prints lines of file2 only.
comm –2 file1 file2 Prints lines of file1 only.
comm –3 file1 file2 Prints unique lines of file1 and file2.
comm –12 file1 file2 Prints only the common lines of file1 and
file2.
comm –13 file1 file2 Prints the unique line of file2 only.
comm –23 file1 file2 Prints the unique line of file1 only.
comm –123 file1 file2 No display.

The comm command displays the position and line where the first difference has
occurred between the content of two files whereas the cmp command displays the
common and unique lines of two files.

SR/UNIX/401/0204/SC/1.1 32
UNIX

Display specific lines of a file

UNIX provides two useful commands to view portions of a file - head and tail. The head
is used to display from the beginning of a file, while tail is used to display lines from the
end.

Two switches can be used with head and tail:

-n: To display the number of lines


-c: To display the number of characters

The usage is as follows:

$ head –n5 file1 -> displays the first 5 lines of file file1
$ head –c10 file1 -> displays the first 10 characters of file file1

The usage of tail is similar to the head:

$ tail –n5 file1 -> displays the last 5 lines of the file file1
$ tail –c10 file1 -> displays the last 10 characters of the file file1

If head and tail is used without –n or –c, it will display the


whole content of the specified file.

Permissions
Whenever a file or a directory is created in UNIX, these have some permission set to it.
These permissions are known as File Access Permissions (FAP). The chmod command
of UNIX is used to change the protection or permission bits of a file. Only the
administrator or the owner of the file can set the permissions on a file. The usage of
chmod command is given below:

chmod <type of user+permission> filename

The above command requires the knowledge of two things, namely to whom the
permission will be given and what are those permissions.

The types of users in UNIX are:

Ø Owner: The user who has created a file. It is symbolized as u.

SR/UNIX/401/0204/SC/1.1 33
UNIX

Ø Group: Users are put inside groups, who have similar requirements. It helps for
easier system administration. It is symbolized as g.

Ø Others: They are users, in the systems, which are not a part of the same group as the
owner of a file. It is symbolized as o.

Ø Administrator: It is the supreme owner of the whole system, and hence everything is
in its control. So an administrator is a class by himself and need not bother about any
permission.

In UNIX, each and every file has three types of permissions. They are:

Ø Read : Represented as r.
Ø Write : Represented as w.
Ø Execute : Represented as x.

Now, let us recall the ls –l command to know user permissions on a particular file.

In the above figure, it can be seen that the owners of the files has read and write
permission whereas the group and other users has only read permission.

To change permissions, the following two methods can be used:

Ø Text Method

To grant the execute permission to the owner, the command is:

$ chmod u+x shell

SR/UNIX/401/0204/SC/1.1 34
UNIX

Similarly, to revoke read permission from others, the command is:

$ chmod o–r shell

Ø Octal Number Method

Though we can grant or revoke permissions by combining owner, group and others using
the Text Method but it still becomes cumbersome at times.

So a simpler technique was devised using the octal number system, where the
permissions have the following values:

Ø Read = 4
Ø Write = 2
Ø Execute = 1

These values have been calculated taking 2n , where n is the position starting from 0. So
collectively for owner, group and others if all permissions are granted then the command
will be as following:

$ chmod 777 file1

SR/UNIX/401/0204/SC/1.1 35
UNIX

Thus this is a faster method of changing permissions.

A very interesting result is obtained if the owner issues the following command:

$ chmod 000 file1

Thus by revoking all permissions from himself, he has to turn to the administrator for
help. Windows NT users will recall that this is the case of giving No access to Everyone
and then Taking Ownership.

Changing ownership
After changing permissions for your files it is time to be benevolent to others by giving
them ownership of your files and even changing group ownership.

Only the current owner of the file has the authority to issue the following commands, and
of course the administrator.

Suppose, there are five users in a particular company and each user is creating their own
files. At the end of the day, the administrator of the system wants to takes the ownership
of the files so that the next day those will not be able to modify those files.

To change ownership, use the following:

$ chown <new_owner> <file>

when a particular user needs to be transferred to another group, the chgrp command is
used.

To change the group, use the following:

$ chgrp <new_group> <file>

Once changed, the previous owner of the group has no further


authority.

Search a file
Sometimes it becomes essential to locate the existence of a particular file. The find
command helps to perform the task.

$ find <path> -name “<filename>” –print

path : is the location to search

SR/UNIX/401/0204/SC/1.1 36
UNIX

-name : is a switch, which takes the name to search


-print : is a switch, which prints the result on the VDU

The find command can also be used with another very useful switches, which helps to
locate a directory (d) and ordinary (f) file.

$ find <path> -type d –print displays all directories in the path specified.

$ find <path> -type f –print displays all ordinary files in the path specified.

$ find <path> -name “<filename>” –print displays the file

Some of the useful switches used with find command are given below:

Ø find –atime n : This option will return a value of true if the file specified has been
read n days ago.

$find . –name “try1” –atime 1 -print

Ø find –ctime n : This option will return a value of true if the file specified has been
created or modified n days ago.

$find . –name “try1” –ctime 1 -print

SR/UNIX/401/0204/SC/1.1 37
UNIX

The difference between find and ls command is that ls displays


the content of a specified directory whereas find searches in the
specified directory as well as in all the sub-directories also. The
find command also varies from grep as grep searches a
particular pattern within a string.

Wildcards
While issuing commands, a common phenomenon is to issue the same command for
similar types of file names. For e.g. all data files with a .dat extension should be backed
up, so to issue the same command for fifty files, it is time saving and more efficient to do
this with a single command. This can be achieved by implementing wildcards. Just as the
name suggests wildcards represent a sequence of characters.

UNIX supports 3 types of wildcards:

? Represents a single character


* Represents multiple characters
[] Represents any one character from the list given in the parenthesis

Ø $ ls a[bcd] will list files or directories in the current location having names ab or ac or
ad.

Ø $ ls a* will list files or directories in the current location having names starting with a
and followed by any one character.

Communication commands

UNIX provides some commands to the users to communicate with each other. Some of
the communication commands supported by UNIX are as follows:

Ø wall

The wall command is the short form of Write for All. The administrator of the system to
make universal announcements like at the time of system shutdown so that the users can
save their files on the disk, uses this command. The wall command is located in the /etc
directory and must be invoked as:

# /etc/wall <Enter>
message <Enter>
ctrl+d<Enter>

SR/UNIX/401/0204/SC/1.1 38
UNIX

All the users who are the logged in the system will get the message flashed on their
terminals along with a beep sound.

Only the super users can issue the wall command.

The sample output of wall command is given in the next page.

Notice the # prompt in the above diagram which is used for the broadcast.

Ø write

In UNIX, two users can communicate between themselves provided both the users are
logged in. The write command connects one terminal to another terminal and facilitates
the user to pass on messages back and forth. At one time, communication can be possible
between two users only. But before starting the communication, the write permission has
to be set for both the terminals by typing the following command:

$mesg y

Now the user is ready to start communication. Issue the following command:

$ write <user_name>
message

If the communication needs to be stopped, then mesg should be set to n. Moreover, if


multiple users has logged in with the same user name, then the user who has logged in
first can only communicate otherwise the terminal name (like tty01, tty02) needs to be
specified.

SR/UNIX/401/0204/SC/1.1 39
UNIX

Ø talk

It is a visual communication program, which copies lines from one terminal to another.
A diagrammatic representation is shown in the next page:

$ talk user2

USER1 USER2

2
$talk user1
The process is initiated by user1 and then user2 has to respond by replying when the
communication process is established. Let us see the display of the above process on the
screen.

USER1 USER2

SR/UNIX/401/0204/SC/1.1 40
UNIX

Some useful key combinations while talking are:

Ctrl + l : Reprints screen


Ctrl + h : Erase
Ctrl + w : Erase a character
Ctrl + c : Exit

Filter

A filter is defined as a special program, which takes input from standard input device and
sends output to standard output device.

The input can also be taken from a file and similarly the output can be redirected to
another file.

UNIX treats all devices as files and the shell provides three data streams (a stream is a
collection of characters):

Ø 0 – Standard Input stream (the keyboard) or stdin


Ø 1 – Standard Output stream (the screen) or stdout
Ø 2 – Standard Error stream (the screen) or stderr

Now if we wish to take inputs or send outputs to and from other sources the respective
data streams must be redirected. Thus we also have the following redirection symbols:

Ø < - Input Redirection


Ø > - Output Redirection
Ø | - Pipe Redirection, which can represent both input as well as output redirections.

A few examples of filters used in UNIX are cat, wc, sort, more, grep, cut, etc.

Filter cat

The command used to concatenate files is cat. So by just issuing the command:

$ cat

expects input from the keyboard (the standard input) and sends output to VDU (the
standard output). As soon as the user presses <Enter>, the contents of the buffer are
flushed out and the values are displayed on the console. This process will continue until
the user chooses <CTRL + d> to terminate it.

SR/UNIX/401/0204/SC/1.1 41
UNIX

The output of the above command is given below:

By default, cat expects input from standard input device, i.e,


keyboard and sends output to standard output device i.e, VDU.

Variations of cat

Some of the variations of cat command with input and output redirections are given
below:

$cat file1

The above command accepts input from a file called file1 and redirects the output to the
standard output device (VDU). This implies that the cat command is accepting the input
from a file and sends the output to the standard output file.

The above command can also be written in the following way using the input redirection
symbol:

$cat<file1

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 42
UNIX

Using the above logic, we may take input from the stdin, and redirect the content to a file,
i.e. we shall create our own file using the following syntax:

$ cat >file1

It will take input from the keyboard till <Ctrl + d>.

The only drawback of creating a file using the above way is that the file cannot be
modified (till any text editor is used) after hitting the <Enter> key.

In the above command, if the file called file1 already exists, the existing text will be
overwritten. To prevent this, the following command can be issued:

$cat>>file

The command above will append the new text to the existing one.

The usage of stderr is given below:

$cat hello 2>err


$cat err

In the above command the file hello does not exist. The error message is redirected to a
file called err instead of sending it to the standard output device.

The command [cat file1>file2] is similar to [cp file1 file2]

Lets take another example

$ cat fl1 > fl2 2> err

If the file fl1 exists, readable and the read permission is granted then the output will be
stored in fl2, other wise the system defined error message will be stored in the file err.
This is to be noticed that in either case nothing will be displayed

SR/UNIX/401/0204/SC/1.1 43
UNIX

Filter wc
UNIX provides a filter to keep track of the number of lines, characters and words of a
particular file. The filter that is used to perform the task is wc. The usage of wc filter is
given below:

$wc file1

The output is given below:

Lines Characters

Words
If one likes to view only one value at a time then the following switches may be used:

Switches Description
-l Displays number of lines.
-w Displays number of words.
-c Displays number of characters.

Some typical usages of wc is given below:

Ø $wc –l file1

The above command will display only the number of lines in file1. The output is given
below:

SR/UNIX/401/0204/SC/1.1 44
UNIX

Ø $ls | wc -l

The above command will give a total count of files and directories in the current
directory. The ls command redirects the output to wc filter instead of sending it to the
standard output device. The output of the above command is given below:

See the use of Pipe (|)

Ø $wc<file1>count

The above command facilitates wc to accept input from the file called file1 and then
redirects the output to another file called count, instead of sending the output to standard
output device. Now, display the content of count using the cat command.

The output is given below:

Filter more
Sometimes when the output of a particular command is not accommodated within one
full screen, the first part of the display scrolls up. To have one page display at a time,
there is a useful filter called more.

SR/UNIX/401/0204/SC/1.1 45
UNIX

The usage of more is as follows:

$ more file1

The above command will display the contents of the file one screen full at a time. To
move to the next line, press the Enter key.

Another usage of more is to display the contents of a directory page wise:

$ ls | more

The above command can also be used with the pg filter.

$ls –l | pg

more differs from pg in respect that with the help of more, we


can navigate back and forward with the help of b and f keys
respectively. In case of pg, we can scroll back and forward using
the page number.

Arranging output
To arrange data sequentially either in ascending or descending order in order, the sort
filter is used. The sort filter reorders lines depending on the ASCII values of the
characters. The sorting starts from the first character of each line and proceeds to next
character only when the characters in two lines are identical. By default, the lines are
sorted in ascending order.

The usage of sort command is given below:

$sort

The above command will wait for any input from standard input device. After pressing
<Ctrl+d>, it will display the output in a sorted manner in the standard output device.

Press <ctrl+d>

SR/UNIX/401/0204/SC/1.1 46
UNIX

$sort file1

The output of the above command is given below:

By default, the sort command starts sorting from the first field (field number starts from
0). The field can be explicitly specified from where the sorting should start using
+fieldnumber switch where fieldnumber denotes the value added to the default start
field.

The command sort takes space as default delimiter.

The example given below starts sorting from the second field.

$sort +1 file1

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 47
UNIX

Now, to sort the file only depending on the second field, the following command is used:

$sort +1 –2 file1

In the above command, -2 is restricting the sort for the second field only.

Some of the frequently used switches with sort are given below:

Ø sort –r: By default, sort arranges the output in ascending order. To arrange the output
in descending order, sort –r option is used.

$sort –r file1

SR/UNIX/401/0204/SC/1.1 48
UNIX

Ø sort –t : If a file contains fields having delimiter other than a space, sort –t can be
used to specify that delimiter.

$sort –t’|’ file1

The fields in file1 are separated by a pipe (|). In the above command, this delimiter has
been specified using sort –t.

Ø sort –o : As sort is a filter, sorted output can be redirected to a file using > operator or
–o option. The usage of sort –o is given below:

sort –o <sortedfile> <originalfile>

Now, issue the following command from $ prompt:

$sort –o sortfile file1

The output of the above command is given below:

Ø sort –n: In UNIX, sorting can be done on numeric fields also. The command below
will perform the task.

$sort +1n file1

SR/UNIX/401/0204/SC/1.1 49
UNIX

sort can be used with –n and –r simultaneously to arrange the output in reverse order
based on the numeric field.

$sort file1 +1nr

Moreover, sort can be used with uniq command to eliminate the redundant lines from a
file.

$sort file1|uniq

SR/UNIX/401/0204/SC/1.1 50
UNIX

Filter tee
As already discussed, with the help of pipes, the output of one command is sent as input
of another command and the ultimate output is achieved. But let us take an example
where we want to display the number of users that are currently working in the system
and at the same time we want to store the output of who command into a file. In order to
get that, UNIX provides the tee filter. The tee command enables the output of a command
to be redirected to a file and at the same time passes it to the input of a pipe.

$who | tee file3 | wc -l

The output of the above command is given below:

tee –a is used to append the output to a file.

Filter grep

UNIX provides a filter called grep that searches for string. The grep stands for Globally
Search for Regular Expressions and Print out. The type of search expressions that
grep filter can handle is enormous and if used with a combination with sort, the result
can be obtained efficiently. The usage of grep is given below:

$ grep <search_expression> <file>

The following example searches a specified expression in a particular file and will
display the lines containing the expression:

$grep Hello file1

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 51
UNIX

If more than one word needs to be specified as a grep pattern,


the pattern must be enclosed within quotes.

Some of the search expressions are given below:

CHARACTER MEANING EXAMPLE EX. DESCRIPTION


[ ] Matches any one of a set grep “lab[abc]” Means “laba” or “labb”
of characters or “labc”
[ ] with hyphen Matches any one in the grep “lab[a-c]” Means “laba” or “labb”
range or “labc”
^ Pattern following it must grep “^lab” The word “lab” must
occur at the beginning of occur at the beginning
a line of a line
[^ ] Must not contain any grep “lab[^a-c] Any other pattern but
character(s) specified “laba” or “labb” or
within [ ] “labc”.
$ Pattern preceding it must grep “lab$” The word “lab” must
occur at the end of each occur at the end of the
line line
.(dot) Matches a single character grep “lab.” The pattern is “lab”
followed by any
character.
\ Ignores any special grep “lab\[abc\]” Means literally
meaning of the character “lab[abc]” as the
following it. pattern

Some frequently used switches with grep command are given in the table below:

Option Description
-n Prints each line with the matching pattern along with the line number.

SR/UNIX/401/0204/SC/1.1 52
UNIX

-c Prints the total count of the lines matching the pattern.


-v Prints all those lines that do not match the pattern.
-i Ignores cases while searching for a pattern.

Some typical usages of grep filter are given below:

Ø $ls –l | grep –v “^-“

The above command will display all the directory and special files under the current
directory.

Ø $grep –v “^$” file1 > newfile

The above command will delete all the blank lines from a file called file1 and the final
output will be redirected to a file called newfile.

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 53
UNIX

Ø $grep –v “^ *$” file > newfile

Sometimes a line that looks blank is not really blank. It can contain some spaces. The
command given above deletes all those blank lines that contain spaces.

Ø $grep –n Hello file1

The command given above will display the lines containing the word hello with the line
numbers. The output is given in the next page:

Ø $grep –c Hello file1

The command given above will display the count of lines containing the text Hello in the
file called file1.

Ø $grep –i hello file1

The above command will display the lines containing the text hello irrespective of the
case specified during the search.

SR/UNIX/401/0204/SC/1.1 54
UNIX

Filter tr
The tr filter of UNIX has multiple roles. It can be used for the following two purposes:

Ø Case conversion

The command given below will wait for any input from stdin and then it will convert all
the upper case characters to lower case.

$tr “[A-Z]” “[a-z]”

The output of the above command is given below:

$tr he HE

The above command will wait for input from stdin and then it will convert all h and e to
H and E. The output of the above command is given below:

Ø Squeeze characters

The tr command can also be used to convert multiple occurrences of column separator to
single. The –s switch with tr filter helps to achieve the purpose.

The command given below squeezes multiple occurrences of space in the output of who
command to single space.

SR/UNIX/401/0204/SC/1.1 55
UNIX

$who | tr –s “ ”

The output of the above command is given below:

$ls | tr –s “ “

The parameter used with -s, must be the same as that of the
column separator of the source data

The cut filter

When it is required to display only a part of the columns from the output of certain
commands like ls –l, who, or specified columns of a data file, the filter cut comes in
handy.

The frequently used switches with cut filter are given in the table below:

Switch Description
-f Field or column number that is to be cut
-d Cut upto the column delimiter (must be uniform throughout all the
columns). The default delimeter of cut filter is tab.
-c The position of the character to be cut

Some typical usages of cut filter are given below:

SR/UNIX/401/0204/SC/1.1 56
UNIX

Ø $cat file | cut –c1

The above command will redirect the content of the file called file1 to cut filter. The cut
filter will extract the first character of each line and will display it.

The output of the above command is given in the next page.

Ø $cat file1 | cut –c 1,2

The above command will extract first and second character of each line from file1 and
will display it. The output of the above command is given below:

Ø $cat file1 | cut –c1-5

The above command will extract all the characters lying between first and fifth position
of each line of the file called file1and will display it.

SR/UNIX/401/0204/SC/1.1 57
UNIX

Ø $cat file1 | cut –c3-

The above command will extract the character from third position of each line till the end
of the line.

Ø $cat file1 | tr –s “ “ | cut –d “ “ -f1,2

The above command will redirect the content of file1 to tr filter. The tr filter will
squeeze the multiple occurrences of spaces between the fields into single space.
Ultimately the cut filter will take the column delimiter as space (mentioned as –d “ “) and
will extract the first and the second field.

SR/UNIX/401/0204/SC/1.1 58
UNIX

The /etc/hosts file

You start with the most fundamental file that you need for communicating with other
computers on your network — the hosts file.

When you access another host by name on the Internet or any intranet/internet, your
computer needs to know the remote host's IP address. You can get remote host addresses
from DNS (Domain Name System) or from your computer's hosts file. This file lists the
names and addresses of other hosts known by your computer.

When you need to know about thousands of hosts on the Internet, maintaining the hosts
file is really too cumbersome a mechanism. Imagine having to spend all that time
updating it as computers come and go or relocate on the Internet! In that case, you need
DNS to locate remote hosts.

IP address are used by many TCP/IP utilities like ftp, telnet. For example :

ftp 194.9.200.77 or
telnet 194.9.200.77

but it’s impractical to remember machines by their IP addresses, while network


understands only the numbers and not the names. On smaller networks, the name-address
mappings are placed in the file /etc/hosts on each and every host of the network. This
database allows the applications to look up a name and find out its corresponding IP
address. A look at a sample file reveals its flexile structure :

127.0.0.1 localhost # the loopback address


Kaushik
Suvendu
Suvro

For each machine in the network, this table contains a line mapping the IP address to its
respective host name. It contains at least 2 fields for each line, one for each type of the
address.

SR/UNIX/401/0204/SC/1.1 59
UNIX

Typical Examples Using Filters

Ø Count the number of users currently logged in as user1.

$who | grep ‘user1’ | wc -l

The output of the above command is given below:

Current Users Count

Logged in as user1

Ø Find the largest file in the current directory

$ls –l | tr –s “ “ | cut –d” “ –f5,9 | sort –nr | head –n1

The output of the above command is given below:

Squeeze Numeric sort


(desc)
Cut (Field No. 5 and 9)
Head

SR/UNIX/401/0204/SC/1.1 60
UNIX

Ø The example given below will display the name and the designation of an employee
who is not a clerk and earns the minimum salary. For this purpose, let us first see the
content of the file file1:

Now, issue the following command from the $ prompt:

$grep –iv clerk file1 | sort +2n | cut –d” “ –f1-2 | head -1

Ignore the case and line


matching the pattern Cut (Field No. 1 and 2)

Numeric Sort (asc order)

Ø Display the line number 2 and 3 of a particular file

$cat file1 | tail –5 | head -2

The above command will first extract the last five lines of the file and then it will display
the first two lines from that. The output is given below:

SR/UNIX/401/0204/SC/1.1 61
UNIX

SR/UNIX/401/0204/SC/1.1 62
UNIX

Introduction to vi Editor

At the end of the chapter, you will be able to:

Know the vi Editor and how it works


Know different modes of vi
Understand the process of cutting and yanking
Know the vi level settings in .exrc
Set the vi environments

SR/UNIX/401/0204/SC/1.1 63
UNIX

Introduction

The vi editor is a screen-based editor used by many Unix users. The vi editor has
powerful features to aid programmers, but many beginning users avoid using vi because
the different features overwhelm them. This book help beginners get accustomed to using
the vi editor, but also contains sections relevant to regular users of vi as well. Examples
are provided, and the best way to learn is to try these examples, and think of your own
examples as well... There's no better way than to experience things yourself.

Before You Begin


The vi editor uses the full screen, so it needs to know what kind of terminal you have.
The prompt looks like this:

TERM = (vt100)

If you know your terminal is a vt100 (or an emulator that can do vt100), just hit return for
the terminal type when you log in. If you have an hp terminal, type "hp" for the terminal
type and hit return.

If you make an error when you log in and type the wrong terminal type, don't panic and
log out. You can type the following commands to fix the settings:

First, tell your shell what type of terminal you have. (If you're not sure what your shell is,
type this command to see what shell you have: echo $SHELL.) For the examples given,
the terminal type is "vt100". Substitute it with whatever terminal type you have. For C
shell (/bin/csh), the command is this:

set term=vt100

For Bourne Shell (/bin/sh) or Korn Shell (/bin/ksh), the commands are the following:

export TERM
TERM=vt100

Next, reset your terminal with this command:

tset

Now that the terminal type is (hopefully) correctly set, you are ready to get started with
vi.

SR/UNIX/401/0204/SC/1.1 64
UNIX

Starting the vi Editor

The vi editor lets a user create new files or edit existing files. The command to start the vi
editor is vi, followed by the filename. For example, to edit a file called temporary, you
would type vi temporary and then return. You can start vi without a filename, but when
you want to save your work, you will have to tell vi which filename to save it into later.
When you start vi for the first time, you will see a screen filled with tildes (A tilde looks
like this: ~) on the left side of the screen. Any blank lines beyond the end of the file are
shown this way. At the bottom of your screen, the filename should be shown, if you
specified an existing file, and the size of the file will be shown as well, like this:

"filename" 21 lines, 385 characters

If the file you specified does not exist, then it will tell you that it is a new file, like this:

"newfile" [New file]

If you started vi without a filename, the bottom line of the screen will just be blank when
vi starts. If the screen does not show you these expected results, your terminal type may
be set wrong. Just type :q and return to get out of vi, and fix your terminal type. If you
don't know how, ask a lab monitor.

Getting Out of vi

Now that you know how to get into vi, it would be a good idea to know how to get out of
it. The vi editor has two modes and in order to get out of vi, you have to be in command
mode. Hit the key labeled "Escape " or "Esc" . If you were already in the command mode
when you hit "Escape ", don't worry. It might beep, but you will still be in the command
mode.
The command to quit out of vi is :q. Once in command mode, type colon, and 'q',
followed by return. If your file has been modified in any way, the editor will warn you of
this, and not let you quit. To ignore this message, the command to quit out of vi without
saving is :q!. This lets you exit vi without saving any of the changes.

Of course, normally in an editor, you would want to save the changes you have made.
The command to save the contents of the editor is :w. You can combine the above
command with the quit command, or :wq. You can specify a different file name to save
to by specifying the name after the :w. For example, if you wanted to save the file you
were working as another filename called filename2, you would type: w filename2 and
return.
Another way to save your changes and exit out of vi is the ZZ command. When in
command mode, type zz and it will do the equivalent of :wq. If any changes were made
to the file, it will be saved. This is the easiest way to leave the editor, with only two
keystrokes.

SR/UNIX/401/0204/SC/1.1 65
UNIX

The Two Modes of vi

The first thing most users learn about the vi editor is that it has two modes: command and
insert. The command mode allows the entry of commands to manipulate text. These
commands are usually one or two characters long, and can be entered with few
keystrokes. The insert mode puts anything typed on the keyboard into the current file.

vi starts out in command mode. There are several commands that put the vi editor into
insert mode. The most commonly used commands to get into insert mode are a and i.
These two commands are described below. Once you are in insert mode, you get out of it
by hitting the escape key. If your terminal does not have an escape key, ^[ should work
(control-[). You can hit escape two times in a row and vi would definitely be in command
mode. Hitting escape while you are already in command mode doesn't take the editor out
of command mode. It may beep to tell you that you are already in that mode.

How to Type Commands in Command Mode


The command mode commands are normally in this format: (Optional arguments are
given in the brackets)
[count] command [where]
Most commands are one character long, including those which use control characters.
The commands described in this section are those which are used most commonly the vi
editor.

The count is entered as a number beginning with any character from 1 to 9. For example,
the x command deletes a character under the cursor. If you type 23x while in command
mode, it will delete 23 characters.

Some commands use an optional where parameter, where you can specify how many
lines or how much of the document the command affects, the where parameter can also
be any command that moves the cursor.

Some Simple vi Commands

Here is a simple set of commands to get a beginning vi user started. There are many other
convenient commands, which will be discussed in later sections.

enter insert mode, the characters typed in will be inserted after the current cursor
position. If you specify a count, all the text that had been inserted will be repeated that
many times.
h
move the cursor to the left one character position.

SR/UNIX/401/0204/SC/1.1 66
UNIX

i
enter insert mode, the characters typed in will be inserted before the current cursor
position. If you specify a count, all the text that had been inserted will be repeated that
many times.

j
move the cursor down one line.

k
move the cursor up one line.

l
move the cursor to the right one character position.

r
replace one character under the cursor. Specify count to replace a number of characters

u
undo the last change to the file. Typing u again will re-do the change.

x
delete character under the cursor. Count specifies how many characters to delete. The
characters will be deleted after the cursor.

Text Buffers in vi

The vi editor has 36 buffers for storing pieces of text, and also a general purpose buffer.
Any time a block of text is deleted or yanked from the file, it gets placed into the general
purpose buffer. Most users of vi rarely use the other buffers, and can get along without
the other buffers. The block of text is also stored in another buffer as well, if it is
specified. The buffer is specified using the " command. After typing ", a letter or digit
specifying the buffer must be entered. For example, the command: "mdd uses the buffer
m, and the last two characters stand for delete current line. Similarly, text can be pasted
in with the p or P command. "mp pastes the contents of buffer m after the current cursor
position. For any of the commands used in the next two sections, these buffers can be
specified for temporary storage of words or paragraphs.

Cutting and Yanking

The command commonly used command for cutting is d. This command deletes text
from the file. The command is preceded by an optional count and followed by a
movement specification. If you double the command by typing dd, it deletes the current
line. Here are some combinations of these:

d^

SR/UNIX/401/0204/SC/1.1 67
UNIX

deletes from current cursor position to the beginning of the line.

d$
deletes from current cursor position to the end of the line.

dw
deletes from current cursor position to the end of the word.

3dd
deletes three lines from current cursor position downwards.

There is also the y command which operates similarly to the d command which take text
from the file without deleting the text.

Pasting

The commands to paste are p and P. The only differ in the position relative to the cursor
where they paste. p pastes the specified or general buffer after the cursor position, while
P pastes the specified or general buffer before the cursor position. Specifying count
before the paste command pastes text the specified number of times.

Settings for vi

You can customize the way VI behaves upon start up. There are several edit options
which are available using the :set command mode)

Some of these options have values set with the equals sign '=' in it, while others are either
set or not set. (These on or off type of options are called Boolean, and have "no" in front
of them to indicate that they are not set.) The options shown here are the options that are
set without any customization. Descriptions of some of these are given below, with an
abbreviation. For example, the command set autoindent, you can type :set autoindent or
:set ai. To unset it, you can type :set noautoindent or :set noai

autoindent (ai)

This option sets the editor so that lines following an indented line will have the same
indentation as the previous line. If you want to back over this indentation, you can type
^D at the very first character position. This ^D works in the insert mode, and not in
command mode. Also, the width of the indentations can be set with shiftwidth, explained
below.

exrc
The .exrc file in the current directory is read during startup. This has to be set either in
the environment variable EXINIT or in the .exrc file in your home directory.

mesg

SR/UNIX/401/0204/SC/1.1 68
UNIX

Turn off messages if this option is unset using :set nomesg, so that nobody can bother
you while using the editor.

number (nu)

Displays lines with line numbers on the left side.

shiftwidth (sw)
This option takes a value, and determines the width of a software tabstop. (The software
tabstop is used for the << and >> commands.) For example, you would set a shift width of
4 with this command: :set sw=4.

showmode (smd)
This option is used to show the actual mode of the editor that you are in. If you are in
insert mode, the bottom line of the screen will say INPUT MODE.

warn
This option warns you if you have modified the file, but haven't saved it yet.
window (wi)
This option sets up the number of lines on the window that vi uses. For example, to set
the vi editor to use only 12 lines of your screen (because your modem is slow) you would
use this: :set wi=12.

wrapscan (ws)
This option affects the behavior of the word search. If wrapscan is set, if the
word is not found at the bottom of the file, it will try to search for it at the
beginning.

wrapmargin (wm)
If this option has a value greater than zero, the editor will automatically "word
wrap". That is, if you get to within that many spaces of the left margin, the word
will wrap to the next line, without having to type return. For example, to set the
wrap margin to two characters, you would type this: :set wm=2.

Abbreviations and Mapping Keys to Other Keys

The abbreviate command in vi lets you set up abbreviations for specific strings. The
command looks like this : :ab string thing to substitute for. For example, if you had to
type the name, "Humuhumunukunukuapua " but you didn't want to type the whole
name, you could use an abbreviation for it. For this example, the command is entered like
this:

:ab gtl Globsyn Techonoligies Ltd

Now, whenever you type gtl as a separate word, vi will type in the entire word(s)
specified.

SR/UNIX/401/0204/SC/1.1 69
UNIX

To remove a previously defined abbreviation, the command is unabbreviate. To remove


the previous example, the command would be ":una gtl" To get your listing of
abbreviations, simply just type :ab without any definitions.

Another EX editor command that is useful for customization is the mapping command.
There are two kinds of mapping commands. One for command mode, and the other for
insert mode. These two commands are :map and :map! respectively. The mapping
works similarly to the abbreviation, and you give it a key sequence and give it another
key sequence to substitute it with. (The substituted key sequences are usually vi
commands.)

The EXINIT Environment Variable and the .exrc file


There are two ways to customize the vi editor. If you create a file called .exrc in
your home directory, all the commands in there will be read when vi starts up.
The other method is to set an environment variable called EXINIT. The options
will be set in your shell's setup file. If you use /bin/csh (C-Shell), the command is
as follows, and is put in the .cshrc file:
setenv EXINIT '...'

If you use /bin/sh or /bin/ksh, the command is as follows, and is put into the .profile file:

export EXINIT
EXINIT='...'

Don't put in ... as the example says. In this space put the commands that you want to set
up. For example, if you want to have auto indent, line numbering, and the wrap margin of
three characters, then the setenv command (for C shell) looks like this:

setenv EXINIT 'set ai nu wm=3'

If you want to put more than one command in the setenv EXINIT thing, separate the
commands with a vertical bar (|). For example, to map the 'g' command to the 'G'
character in command mode, the command is :map g G, and combined with the above
command, you get this:

setenv EXINIT 'set ai nu wm=3|map g G'


If you want to create the file called .exrc, you can put exactly the same things in the file
as shown in the quotes after the EXINIT.

Recovering Your Work When Something Goes Wrong with


Your Terminal

The vi editor edits a temporary copy of your file, and after the editing is complete, or
when you tell it to save, it puts the contents of the temporary copy into the original file. If

SR/UNIX/401/0204/SC/1.1 70
UNIX

something goes wrong while you are editing your file, the vi editor will attempt to save
whatever work you had in progress, and store it for later recovery. (Note: If vi dies while
you were working on any file, it sends you an email message on how to recover it. The -r
option stands for recovery. If you were editing the file vitalinfo, and you accidentally got
logged out, then the -r option of the 'vi' editor should help. The command would look
somewhat like this: vi -r vitalinfo After using the -r option once, though, you MUST
save what you have recovered to the actual file... The -r option only works once per
failed vi session.

Warning About Using vi on the Workstations

There are two things to be aware of when using the workstations: Editing the same file
many times at once, and changing the size of the screen.

Because vi edits a copy of your original file and saves the contents of that copy into the
original file, if you are logged on more than once and are editing the same file more than
once using vi, if you save on one window and then you save on the other window, the
changes made to the file on the first save would be overwritten. Make sure that you only
run one copy of vi per file.

If you use a terminal program from a workstation, you can change the size of the screen
by dragging the sides of the window. If the size is not working properly, the command to
type is this:

eval `resize`

If that doesn't work the command would be this:

eval `/usr/bin/X11/resize`

If the size is wrong, the editor will not operate correctly. If you have any problems with
the screen size, ask the monitors in the computer lab for help setting the sizes correctly.

SR/UNIX/401/0204/SC/1.1 71
UNIX

Shell Programming

At the end of the chapter you will be able to:

Create and Execute a Shell Script


Work with Variables
Accept and Display User Input
Work with Positional Parameters
Write Conditional Statements
Write Iteration Statements
Control loops using break and continue

SR/UNIX/401/0204/SC/1.1 72
UNIX

Introduction to shell programming

As we know that a shell in UNIX acts as a command interpreter. Besides command


interpretation, shell provides the facility of performing a particular task using a collection
of UNIX commands acting as a single unit. Some of these features have already been
discussed in the filter session. Now, we will use a collection of such commands in the
form of a program or script. Such programs that are built out of the shell commands are
known as shell programming. Hence a shell script is a collection of shell commands
logically connected together through programming constructs.

A shell script, when executed works as a command itself. The shell script also accepts
arguments like any other UNIX commands.

Let us consider the following set of commands:

$cat file1
$ls –l
$rm file1
$ls

The above commands will be executed one after the other but independently. Now,
suppose all these set of commands are written in a single file (say shellfile). If you
display the content of the file using the cat command, all the above lines will be
displayed.

Now issue the following command from the $ prompt:

$sh shellfile

The above command will execute all the commands written within the file called
shellfile. The sh command creates another shell (mainly child shell) and within that shell
the commands written within the script gets executed. After execution is over, the child
shell is destroyed and the control goes back to the parent shell.

A script file can be executed by merely typing the filename from


the $ prompt provided the file has the execute permission. By
default, when a file is created, it is created with read and write
permission (rw for owner and r for group and others) The
chmod command can be used to give execute permission to the
file.

Comments and Programming style

SR/UNIX/401/0204/SC/1.1 73
UNIX

It is always a good practice of writing a program in such a way that others while reading
the program can understand easily what each step within the program is doing. This can
be done with the help of indentation and comments. The comment entry can be made
with the help of hash (#) sign. If the sign is provided at the beginning of the line, the shell
will ignore that line. If the sign is given in between a line, the shell will ignore remainder
of the line.

Variables
Unlike other programming languages, variables in shell scripts do not have any
associated data types; that is, they are not declared as integers or characters. All variables
in UNIX are treated as character strings. However, It is possible to manipulate variables
mathematically.

Variables in UNIX are of two types:

Ø Shell or user-defined variables: These types of variables can be created and then
used by the user.

Ø System or environment variables: These types of variables are maintained by the


UNIX system. User can only use them.

Shell variable

Ø Creating variables

In UNIX, variables need not to be explicitly declared. Instead, variables can be created at
any point of time within the program, whenever required and simply assigning a value to
the variable can be done.

<variablename>=<value>

There should not be any spaces on either side of the assignment operator
(=).
The command given below creates a variable called name and assigns the value of Tom
to the variable.

$name=’Tom’ or name=Tom

If a variable is created in the following way

$name=Tom Cruise

then the variable name will contain a value of Tom only. To store the full value, the
command needs to be written in the following way:

SR/UNIX/401/0204/SC/1.1 74
UNIX

$name=”Tom Cruise”

Consider the following variable declaration:

$num=1

In the above declaration a variable called num is created with a value of 1. But the
variable num will not be treated as a numeric variable. Instead it contains a character
value of 1. However, any mathematical operation is possible on this variable, which will
be discussed later in this chapter.

Variables can be created either in shell scripts or at the shell prompt. Any variable
created within a shell script is lost when the script stops executing. However, a variable
created at the prompt will remain until the shell is terminated by logging out of the
system. Moreover a variable created in one shell cannot be accessed if the user has
switched to any other shell.

Ø Accessing the value of variable

In UNIX, the value of any variable can be accessed with the help of a $ sign in front of
the variable name. Let us consider the following example:

$name=“Tom Cruise”

Now, if we want to display the value of the variable on the screen, the following
command needs to be issued from the $ prompt:

$echo $name #echo helps to display a message on the screen.

Ø Scope of a shell variable

In UNIX, a variable can be declared and accessed locally or a variable declared within a
particular shell can be accessed throughout all other shells.

Let us now discuss the scope of variable in details:

Local variable

When a variable is referenced, it is known only to the shell that created it. If a new shell
is created by typing sh, this new shell is born unaware of the parent shell’s variables.
Now the same variable name can be given a different value within the new shell without
the parent knowing about it. Such a variable is called local variable.

The figure given below explains the above statements:

SR/UNIX/401/0204/SC/1.1 75
UNIX

name declared in the Parent shell

Creates a new child shell

name not declared in the


child shell

Return to the parent shell

name still exists

Creates a new child shell

name not declared in the new


child shell

Global variable

A variable declared in a particular shell can be made accessible from any other shell.
Thus a local variable can be made global with the help of export command. The usage of
the export command is given below:

$export <variablename>

The figure given in the next page explains the export command:

SR/UNIX/401/0204/SC/1.1 76
UNIX

name declared in the Parent


shell

name made global so that it can be


accessible from different shells

Creates a new child shell

name is accessible from


the child shell

By default, variables are local to a shell. The export


command does help to propagate a value to the shells that
are higher up in the hierarchy.

Accept and Display User Input


Besides assigning a value to a variable at the $ prompt, the shell also allows to enter a
value into a variable within the shell script. This is done using read command.

The script given below prompts the user to enter a name with the help of echo command.
The name entered by the user is stored in a variable with the help of read command.
After that the value of the variable is displayed, again with the help of echo command.
The name of the file is demo1 within which the script is written.

$vi demo1

echo "Enter a name"


read name
echo "The name is : $name"

To display the content of a variable, the variable name must be preceded with a $ sign.
The output of the above script is given below:

SR/UNIX/401/0204/SC/1.1 77
UNIX

The echo command displays the enclosed text on the screen. By default the echo
command displays the text and then puts a newline character at the end of the text. The
newline character causes the cursor to move to the next line after the text is displayed.
Using certain characters in the command can change this default.

Some of the variant usages of echo command are given below:

Ø The following command will display the text and sound the system bell.

$echo “This is a demo of beep sound \007”

Ø The following command will display the text and keeps the cursor at the same line.

$echo “This option keeps the cursor in the same line \n”

Ø The command creates a new line, displays the text and keeps the cursor in the same
line.

$echo -n “\n This option creates a new line, keeps the cursor in the same line”

If user is using a bash shell then the command given below


will create a new line and keep the cursor in the same line.
$echo –e “\n This is a demo of bash shell”

More options of echo:

Options Explanation
echo ${x+5} No output because variable x is not declared.
echo ${x-5} The output is - 5 still the variable x is not declared and hence does
not contain any value.
echo ${x=5} The output is - 5 and the variable x is created and initialized with a
value of 5.
echo ${x?”no”} The output is - 5 because the variable is already created.
echo ${y?”no”} The output is - y: no because the variable y has not been created.

SR/UNIX/401/0204/SC/1.1 78
UNIX

A sample output is given in the next page:

Perform calculations using expr


UNIX does not support numeric variables. All variables are treated as character strings.

Let us have a look at the variable declaration given below:

$ctr=25

With the above command, the variable ctr contain the characters 2 and 5 and not the
number 25. However, in order perform efficient shell programming, it is imperative that
we should be able to manipulate variables mathematically. In other words, it is important
that to simulate numeric variables. This can be possible with the help of expr command.

The figure given illustrates the use of expr command:

SR/UNIX/401/0204/SC/1.1 79
UNIX

The code given below sum up two numbers using a shell script.

The operators that are frequently used while performing any mathematical operations are:

Operators Function
+ Addition
- Subtraction
\* Multiplication (\ is given to ignore the special
meaning of * because * is a wildcard).
/ Division
% Returns remainder

Blank spaces must be given on either side of operators. The


grave accent must be used with expr.

Environment Variables

Besides user-defined variables, a shell also has some special variables called environment
variables. Some of the environmental variables are given below:

Ø The HOME variable

Every user in UNIX system has an associated directory called the home directory.
Whenever a user logs in, he or she is taken to the corresponding home directory.
Assigning a value to the environmental variable HOME specifies the home directory.

SR/UNIX/401/0204/SC/1.1 80
UNIX

$HOME=/usr/user1

The command given below displays the home directory:

$echo $HOME

Ø The PATH variable

The PATH variable contains a list of all full path-names of directories that are to be
searched for an executable program. A colon (:) separates these names. The . denotes the
current path.

The command given below displays the path:

$echo $PATH

The output of the above command is given below:

Ø The PS1 variable

This variable contains the system prompt, the $ symbol. Setting the value of this variable
to the desired prompt can change the system prompt.

The command given below changes the system prompt:

$PS1=”Hello Prompt”

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 81
UNIX

The commands given in the table below displays various usages of PS1 variable:

Command Effect
PS1=”\u” Changes the prompt with user name.
PS1=”\t” Changes the prompt with current time.
PS1=”\d” Changes the prompt with current date.

Ø The LOGNAME variable

This variable contains the user’s login name or user name. The contents of this variable
should not be changed by the user, but should be only displayed.

$echo $LOGNAME

The output of the above command is given below:

Ø The TERM variable

This variable informs the system about the type of the terminal user is working with.
Accordingly, the shell sets the environment. Assigning a different value to the TERM
variable can change the type of the terminal.

$TERM=vt100

Other environmental variables are MAIL, IFS, SHELL, PS2.

The env command is used to display values of all


environment variables.

The .profile file

SR/UNIX/401/0204/SC/1.1 82
UNIX

As soon as the user logs into a UNIX system, the system automatically searches for a file
called .profile in the user’s home directory. The .profile file of UNIX is very much
similar to autoexec.bat of MS-DOS. This files contains different environment variables
initialized to different values. Along with these, it also contains some other commands
for initializing the session.

Let us see the content of .profile of user1:

$cat .profile

It is advisable that .profile file should not be modified unless


absolutely required.

Special characters in UNIX

The UNIX operating system provides some characters with special meanings. If these
characters are used in a shell script as a part of a variable name, they may lead to behave
the program incorrectly.

Character Meaning
$ The beginning of a shell variable name
| Pipes the standard output to next command
# Starts a comment
? Matches one character
* Matches one or more characters
> Output redirection operator
< Input redirection operator

SR/UNIX/401/0204/SC/1.1 83
UNIX

>> Output redirection operator (to append to a file)


[a-z] All characters from a to z
[a, z] Either ‘a’ or ‘z’
& Executes a process in background.

Positional Parameters

Till now, we have seen that scripts either don’t need any input from the user at all or it
takes the input from the user every time it is executed. But sometimes it becomes
convenient to provide a script all the information at the time of execution only, so that it
don’t require any input from the user while running. This type of situation arises when a
script is running in the background because it don’t have any scope to interact with the
user while it is running as a script running in the background disconnects it from the
terminal. It is also useful when one script run another script and passing it information.

Let us consider the command given below:

$sh shellfile arg1 arg2 arg3

The above command will execute the file called shellfile passing three arguments.
Internally, the special variables $1, $2, $3 get set to arg1, arg2, arg3 respectively. The
number of arguments go upto $9.

The character $ is used to represent substitutable parameters.


So each and every parameter must be prefixed with a $.

Some of the parameters that are automatically set by the shell are given in the table in the
next page:

Parameters Description
# Number of parameters passed during execution.
? The status of the last executed command.
$ The process ID of the current shell.
! The process ID of the last background command invoked.
* Returns the actual parameters passed.

Given below is a typical shell script using positional parameters:

echo "This is a demo on positional parameters"


echo "The number of parameters passed are : $#"
echo "The actual parameters are : $*

SR/UNIX/401/0204/SC/1.1 84
UNIX

Now, to execute the shell passing arguments, issue the following command:

$sh demo2 “Kaushik” “Saurav” “Soumya”

The output of the above shell script after executing is given below:

Command substitution

Recall the usage of pipes in joining commands by sending the standard output of one
command as standard input for another. Another way of using more than one command
in a single command line is through command substitution.

Suppose a user wants to display the following message on the screen:

The date is (output of the date command)

To do so, the user needs to enter the following command:

$echo “The date is `date`”

The command date is enclosed in single back quotes (also called grave accent). The
shell first replaces the enclosed command of the output, and then executes the entire
command.

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 85
UNIX

Command substitution can also be used to store the output of a command in a variable.

The command given below counts the number of files in the current directory whose
names end with .cc and the result is store in a variable called ctr.

$ctr=`ls *.cc | wc –l`

The output of the above command is given below:

The example given below sets the value of the date variable:

$set `date`

The set command sets the output of the date command into the positional parameters.

SR/UNIX/401/0204/SC/1.1 86
UNIX

Conditional statement – the if construct


Most of the programming languages have some way to decide whether to execute of
piece of code or not based on a condition. If the condition is true then the code will get
executed and if it is false then the code will not get executed. The reverse can also
happen. In UNIX, shell provides the facility of if….then….else construct through the
conditional execution can be implemented. The if construct is used in conjunction with
test command.

The usage of if construct is given below:

if test <condition>
then
command
else
command
fi

Moreover, multiple conditions can be specified within one if…fi block. This can be done
with the help of if…then…elif…then…else statement. The usage is given below:

if test <condition>
then
command
elif <condition>
then
command
else
command
fi
In the above statements, it the test condition returns an exit status of 0 (the condition is
true), the command immediately following the condition will be executed and the control
will come out of the if block. Otherwise the next condition in the elif statement will be
evaluated. In this way the program execution will proceed. If all the conditions return
false, the control of the program will go to the else part and will execute the command
specified in the else statement.

Moreover, some logical operators can be used along with the if condition. Some of the
logical operators supported by UNIX shell are given in the table below:

Operators Function
-o or || OR
-a or && AND
! NOT

Some typical examples using if cons truct

SR/UNIX/401/0204/SC/1.1 87
UNIX

Ø The script given below will accept a grade from the user. If the grade entered is a then
it will display the message Excellent. If anything entered other then a, it will display
Good.

echo "Enter the grade"


read grade
if test $grade = 'a'
then
echo "Excellent"
else
echo "Good"
fi

The output of the above code is given below:

Ø The code given below will check for multiple conditions using –o (OR) operator. The
test condition will check for the first condition. If the exist status is false, it will check
the next condition. If any of the condition returns an exit status of 0, the command
immediately following it will be executed.

echo "\nEnter a grade: \c"


read grade
if test $grade = 'a' -o $grade = 'b' -o $grade = 'c'
then
echo "\nValid grade"
else
echo "\nInvalid grade"
fi

The output of the script is given below:

SR/UNIX/401/0204/SC/1.1 88
UNIX

The same above program can be written with the help of elif statement.

The code is given in the next page:

echo "\nEnter a grade: \c"


read grade
if test $grade = 'a'
then
echo "\nValid grade"
elif test $grade = 'b'
then
echo "\nValid grade"
elif test $grade = 'c'
then
echo "\nValid grade"
else
echo "\nInvalid grade"
fi

• String comparisons with test

SR/UNIX/401/0204/SC/1.1 89
UNIX

UNIX provides some switches and operators to compare two or more strings. Some of
the string comparison operators are given in the table below:

Options Function
-z s1 True if the length of string s1 is zero
-n s1 True if the length of string s1 is nonzero
s1 = s2 True if strings s1 and s2 are identical
s1 != s2 True if the strings s1 and s2 are not identical.

The script given below uses all the string comparison operators to compare two strings:

echo "Enter the first string"


read str1
echo "Enter the second string"
read str2
if test $str1 = $str2
then
echo "Both the strings are equal"
fi
if test $str1 != $str2
then
echo "The strings are not equal"
fi
if test -z $str1
then
echo "Length of str1 is zero"
fi

if test -n $str2
then
echo "Length of str2 is greater than zero"
fi

The output of the above script is given below:

SR/UNIX/401/0204/SC/1.1 90
UNIX

• Numeric comparison with test

In case of numeric comparison, shell provides some operators to compare between two
integer values only, the decimal values are simply truncated. The complete set of numeric
comparison operators are given in the table below:

Options Effects
n1 –eq n2 True if the integers n1 and n2 are algebraically equal
n1 –ne n2 True if n1 and n2 are not equal
n1 –gt n2 True if n1 is greater than n2
n1 –ge n2 True if n1 is greater than or equal to n2
n1 –lt n2 True if n1 is less than n2
n1 –le n2 True if n1 is less than or equal to n2

Some typical examples using numeric operators

Ø The script given below finds the highest of three numbers entered by the user using
numeric operators:

echo "Enter the first number"


read num1
echo "Enter the second number"
read num2
echo "Enter the third number"
read num3
if test $num1 -gt $num2 -a $num1 -gt $num3
then
echo "The highest number is : $num1"
elif test $num2 -gt $num1 -a $num2 -gt $num3
then
echo "The highest number is : $num2"
else
echo "The highest number is : $num3"
fi

A part of the output is given below:

SR/UNIX/401/0204/SC/1.1 91
UNIX

As discussed earlier, test returns an exit status. If the condition is true, it returns a value
of 0 otherwise 1. The values returned by test are assigned to the parameter $?. The
following commands given from the $ prompt describes the said fact.

Ø The script given below checks two conditions. The first check is for number of
arguments passed from the command line while executing the script. The second
check uses the grep filter to search a pattern. For this script, let us first create a file
called file1 having the following content:

SR/UNIX/401/0204/SC/1.1 92
UNIX

Now, following is the code:

if test $# -ne 3
then
echo "Number of arguments are invalid"
exit
elif grep "$1" $2 > $3
then
echo "Matching pattern found"
else
echo "Matching pattern not found"
fi

In the above code, the elif part checks whether the pattern string which is passed as the
first argument ($1), is present in the file which is passed as the second argument ($2). If it
finds the pattern, the matching lines are redirected to another file passed as third
argument ($3). Ultimately the code displays appropriate messages.

The output of the above code is given below:

Instead of using the test keyword, [ and ] can also be used.


So, test $num1 –eq $num2 is equivalent to [ $num1 –eq
$num2 ]. There should be a space on the inner sides of [
and ]

• File comparison using test

test can be used to check the various file attributes like whether the specified file exists
or not. It can also check whether the file is an ordinary or directory file. The set of
switches used with test for checking various file attributes are given in the table below:

Options Function

SR/UNIX/401/0204/SC/1.1 93
UNIX

-e True if file exists


-r True if file exists and is readable
-w True if file exists and writeable
-x True if file exists and executable
-f True if file exists and ordinary file
-d True if file exists and directory
-s True if file exists and is non empty

The code given below describes the file operators. The script will accept a filename from
the command line as an argument and will check the various file attributes.

if test -e $1
then
echo "File exists"
else
echo "File does not exist"
fi
if test -d $1
then
echo "Directory file"
else
echo "Not a directory file"
fi
if test -f $1
then
echo "Ordinary file"
else
echo "Not an ordinary file"
fi
if test -r $1
then
echo "File is readable"
else
echo "File is not readable"
fi

Now, after executing the above code by passing a filename (file1 in this case) as
parameter, the following output is obtained:

SR/UNIX/401/0204/SC/1.1 94
UNIX

Wildcards cannot be used with test. For example test –w


*.cc is an invalid checking.

Conditional statement – the case construct

This construct is used in shell scripts to perform a specific set of instructions depending
on the expression and is often used in place of the if construct. This type of construct,
matches an expression for more than one alternative and depending on that different
desired commands are executed.

The general syntax of case construct is given below:

case <expression> in
value1) command;;
value2) command;;
*) command
esac

In the above syntax, if case matches value1 with the expression, then it executes the
command associated with it. If it does not, it goes down and matches value2 and this way
it proceeds. If none of the value matches, it executes the command associated with
asterisk (*). Each command is terminated with a pair of semi colon and the entire
construct is closed with esac.

Some typical examples using case construct

Ø The script given below displays a menu and depending on the option selected, it will
execute the respective command.

echo " 1. Listing of directory"


echo " 2. Today's date"
echo " 3. Currently working directory"
echo " 4. Quit from the program"
echo -n "Enter your choice : "
read choice
case $choice in
1) ls -l;;
2) date;;
3) pwd;;
4) exit;;
esac

SR/UNIX/401/0204/SC/1.1 95
UNIX

The output of the above script is given below:

Ø The script given below can act as a calendar service. In the script, the expression is
fetched by command substitution. The first filed of the date command is the day of
the week itself and depending on the day a particular task is performed.

case `date | cut -d" " -f1` in


Wed) ls -l;;
Thu) man who;;
Fri) cal;;
esac

Before executing the above code, let us see the current date:

$date

Now, execute the above code and it will display the calendar, as the current weekday is
Fri.

SR/UNIX/401/0204/SC/1.1 96
UNIX

Ø The script given below handles both the uppercase and lowercase of expression with
the help of |. It has also used the wildcard characters.

echo " y/Y. Listing of directory"


echo " n/N. Today's date"
echo " YES/yes/Yes/YEs/yES. Currently working
directory"
echo " NO/No/nO/no. Quit from the program"
echo -n "Enter your choice : "
read choice
case $choice in
y|Y) ls -l;;
n|N) date;;
[yY][eE][sS]) pwd;;
[nN][oO]) exit;;
esac

The output of the above command is given below:

SR/UNIX/401/0204/SC/1.1 97
UNIX

Iteration – the while statement

Like other programming languages, UNIX also provides some looping constructs. The
looping constructions help to perform a task repeatedly till the condition is true. The
while construct is one of the looping construct in UNIX.

The usage of while loop is given below:

while test <condition>


do
execute commands
done

The set of instruction written within the do…done block is executed till the condition
specified with the while statement return true exit status.

Some typical examples using while loop

Ø The script given below accepts a name from user and displays it. This task continues
till the condition specified is other than y.

SR/UNIX/401/0204/SC/1.1 98
UNIX

reply="y"
while test $reply = "y"
do
echo "Enter your name"
read name
echo "Your name is : $name"
echo "Wish to continue (y/n)"

read reply
done

The output of the above command is given below:

Ø The script given below displays all the number between 1 and 10.

ctr=1
while [ $ctr -le 10 ]
do
echo "$ctr"
ctr=`expr $ctr + 1`
done

The output of the above code is given below:

SR/UNIX/401/0204/SC/1.1 99
UNIX

With the help of while statement, we can purposely create an infinite loop. The
administrators of the system require these types of loops. For example, the administrator
of the system wants to see the free disk space after every five minutes or the processes
running in the system. This can be done with the statements where the while control
commands do not go for any condition checking but only returns a true value. That
means, the loop will continue till it is forcefully terminated.

The script given below is an example of while true loop. In this script free disk space and
processes running in the system will be displayed after every five minutes. The script will
run in the background.

while true
do
df -t
ps
sleep 300
done

In the above script, df –t will display the free disk space and ps will display the processes
running in the system. The sleep command will make the scrip halt for specified number
of seconds.

Now run the script in the background in the following manner:

$ sh prog13 &

The & sign is used to run any program in the background. Now, after every five minutes
the screen will fill with df and ps output.

SR/UNIX/401/0204/SC/1.1 100
UNIX

As told, the background process needs to be terminated forcefully. For this purpose, issue
the following command from the command prompt:

$kill $!

The above command will terminate the last background process.

Iteration – the until statement

The until and while statement are complementary to each other. As while continues the
loop till the condition returns a true value, until statement runs the loop till the condition
is false.

The usage of until statement is given below:

until [ condition ]
do
execute commands
done

An example using until statement is given below:

rep=”y”
until [ $rep = “y” ]
do
echo “\nEnter your name :\c”
read name
echo “\nYour name is $name”
echo “\nWish to Continue :\c”
read rep
done

Iteration – the for statement


Unlike while and until statement, for loop in UNIX do not check for any condition,
instead it uses values from a list.

The syntax of for loop is given below:

SR/UNIX/401/0204/SC/1.1 101
UNIX

for variable in list


do
execute commands
done

In the above usage of for loop, the loop body is executed, as many times as there are
items in the list.

A typical example of for loop is given below:

for var in a b c d
do
echo " The value of var is : $var"
done

The output of the above command is given in the next page:

The script given below returns the total number of ordinary files in the current directory.

ctr=0
for var in `ls -l`
do
if test -f $var
then
ctr=`expr $ctr + 1`
fi
done
echo "The number of ordinary file : $ctr"

SR/UNIX/401/0204/SC/1.1 102
UNIX

The output of the above code is given below:

Controlling loops – break and continue

A for, while or until loop can be controlled with a break or continue command. The
break command exits the loop completely, transferring control to the command that
follows the done statement. The continue command immediately begins the next
execution of the loop, transferring control to the done statement, which continues the
loop.

The usage of break and continue is given below:

while true
do
echo “\nEnter your Department :\c”
read dept
if [ $dept = “mktg” –o $dept = “acct” –o $dept =
“fact” –o $dept = “sales” ]
then
echo “Valid Department”
break
else
echo “\nInvalid Department”
continue
fi
done

SR/UNIX/401/0204/SC/1.1 103
UNIX

Worked out shell scripts

Ø The script given below finds the factorial of a given number accepted from the user.

echo "Enter a number : \c"


read num
result=1
while test $num -gt 0
do
result=`expr $result \* $num`
num=`expr $num - 1`
done
echo "The factorial of the number is : $result"

The output of the above code is given below:

Ø The script given below will accept a string from the user and will check whether the
string is a palindrome or not:

read str
len=`echo $str | wc -c`
len=`expr $len - 1`
ctr=1
while test $len -ge 1
do
str1=`echo $str | cut -c$len`
str2=`echo $str | cut -c$ctr`
if test $str1 != $str2
then

SR/UNIX/401/0204/SC/1.1 104
UNIX

echo "Not a Palindrome"


exit
else
len=`expr $len - 1`
ctr=`expr $ctr + 1`
fi
done
echo "Palindrome"

The output of the above code is given below:

Ø The script given below we will try to simulate the copy (cp) command of UNIX.
Here, the shell would take the file name and directory path as arguments and
check the permissions for the file and the directory. Then the shell will copy the
content of the first file to the directory. If a filename is passed as the second
argument, then the shell will check whether the second file exists, then it would
confirm the over-writing of the file. If the second file does not exist, it will simply
copy the file. The command prototype should be like the following:

if [ $# -ne 2 ]
then
echo " Usage... filecopy file1 file2 "
exit
else
if [ ! -e $1 ]
then
echo " The file $1 does not exist "
exit
elif [ ! -r $1 ]
then

SR/UNIX/401/0204/SC/1.1 105
UNIX

echo " The file $1 does not have a read permission"


exit
fi
if [ -d $2 ]
then
if [ -w $2 ]
then
cp $1 $2
echo " The file $1 has been copied to the
$2 directory"
else
echo " The directory $2 does not have a
write permission"
fi
else
if [ -e $2 ]
then
echo " File $2 exists. Do you want to
overwrite? [y/n]"
read choice
if [ $choice = "y" ]
then
if [ ! -w $2 ]
then
echo " The file $2 does not have
a write permission "
else
cp $1 $2
echo " The file $2 has been
overwritten"
fi
else
exit
fi
else
cp $1 $2
echo " The file has been copied ... "
fi
fi
fi

Ø The example given below uses the shift operator to calculate the sum of three
numbers passed as arguments.

SR/UNIX/401/0204/SC/1.1 106
UNIX

if test $# -lt 3
then
echo "Usage : Shift 3 4 5 6"
exit
fi
j=$#
result=0
while test $j -gt 0
do
result=`expr $result + $1`
shift
j=`expr $j - 1`
done
echo "The result is : $result"

The output is given below:

SR/UNIX/401/0204/SC/1.1 107
UNIX

Introduction to EXCEED

At the end of this chapter, you will be able to:

Use Exceed to connect to server and run X-applications.

SR/UNIX/401/0204/SC/1.1 108
UNIX

About Exceed
Exceed lets you access applications on UNIX workstations from existing Windows
2000/XP, Windows NT, and Windows 98/Me-based personal computers. It lets you run
and display UNIX, Linux, VMS, or X Windows applications (X clients) and integrates
your desktop with X Window
Systems, IBM mainframes, and the Internet. Exceed includes innovative features that
accelerate performance, simplify system administration, optimize personal computing,
and delivers ease of
use. Users are shielded from the complexities of network computing by working within
the familiar Microsoft Windows environment. Exceed is an integrated part of the
Hummingbird Host Access Solutions product family which provide organizations with a
comprehensive Host Access and Network Connectivity solution.

By using Exceed to run remote applications on your local PC, you can:

Ø access powerful applications and information running on networked hosts


Ø establish simultaneous connections to different computers running X clients
Ø use an appropriate window manager to preserve your familiarity with the PC or X
environment

For system administrators, Exceed provides tools to set up, configure and administer PCs
remotely to ensure consistency among systems.

X Window Systems and Exceed


Exceed converts your PC into an Exceed X server. In the X Window environment, the
Exceed X server is also referred to as an X window terminal or display server. Without
Exceed X server software, X applications are accessible only via X terminals, UNIX,
Linux, and VMS workstations.

Exceed works with your network transport software (TCP/IP, DECnet, or IPX/SPX) or
your modem, to access X Windows applications on host computers running the X
Window System. The host can be any operating system that is running the X Window
environment.

Exceed Applications
Exceed includes applications that serve distinct functions. Use the Exceed startup
applications to connect to a host and display UNIX, Linux, VMS and X applications on
your PC. Use the X Client Wizard to guide you through this process, or set up the
connection manually.

SR/UNIX/401/0204/SC/1.1 109
UNIX

Throughout this guide, xterm (a UNIX VT100 terminal emulator) is used as a sample X
client. It provides a terminal emulation window on the host, and a command line where
you can start other X clients.

The applications are listed and briefly described below.

Exceed Exceed X server is a PC X server that displays graphical UNIX,Linux, and X


applications on your PC.

Exceed XDMCP Broadcast This shortcut lets you start the Exceed X server in XDMCP
broadcast mode.

Exceed XDMCP Query This shortcut lets you start the Exceed X server in XDMCP
query mode.

X Client Wizard This application guides you through the process of creating a
connection to a host.

Xconfig Xconfig is a utility for configuring a variety of Exceed settings: input,


communication, video, protocol, security, window modes, performance, X selection,
fonts, troubleshooting, and transport settings.

Xsession Xsession lets you start multiple X clients (Xstart files) and Windows programs
(Wstart files) simultaneously.

Xstart Xstart is an application for automating access to hosts and starting applications.
Use Xstart to create Xstart (.xs) startup files and create shortcut icons to your UNIX,
Linux and X applications. When you click on these icons, they automatically establish a
host connection, log on, and then start an X client, a character-based host application in a
terminal emulator window, or run a host-based script.

Connecting to Hosts
You can connect to a host using a wizard or by creating startup files with Xstart or
Wstart.

Using the X Client Wizard

X Client Wizard guides you through the steps necessary for connecting to a host
application from your PC.

To create a connection using the Client Wizard:

Ø In the Exceed folder, double-click X Client Wizard.


Ø In the welcome screen, click Next.

SR/UNIX/401/0204/SC/1.1 110
UNIX

Ø In the host panel, enter the host name or IP address.


Ø In the same panel, select a host type, then click Next.
Ø In the host connection panel, select a method from the drop-down list. Click Next.
Ø In the application panel, select an X application from the drop-down list. After
you have selected an application, the dialog box immediately displays the
command and default parameters for the application. You can modify the
parameters, but not the command. When you are finished entering this
information, click Next.
Ø In the Login panel, enter the login information you want to display each time this
connection is made. You can leave the Password box blank, but not the User ID
box.
Ø In the Login panel, test the connection. To do this, select Display Host Replies
and click Run. When you are satisfied with the connection, clear Display Host
Replies and click Next.
Ø In the Shortcut panel, you can create a shortcut to the application, and its name
and location. To do this, complete the following information:
§ Type the name of the shortcut and select the option immediately below
this box.
§ Select a location from the drop-down list (showing program groups on the
Windows Start menu) or type a new group name. To create a menu item
on the Exceed X Server Tools menu, select the check box immediately
below this box. When you are satisfied with this information, click Next.

Ø 10 The system creates the specified connection and the shortcut.

SR/UNIX/401/0204/SC/1.1 111

Você também pode gostar