Escolar Documentos
Profissional Documentos
Cultura Documentos
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
Globsyn
UNIX
Globsyn
UNIX
Module Objectives
SR/UNIX/401/0204/SC/1.1 1
UNIX
Introduction to UNIX
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!
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
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.
SR/UNIX/401/0204/SC/1.1 4
UNIX
Unix Shell
SR/UNIX/401/0204/SC/1.1 5
UNIX
Unix Architecture
Ø 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.
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.
Ø 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.
Ø 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.
SR/UNIX/401/0204/SC/1.1 7
UNIX
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
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:
$date –u +%A
$date –u +%B
SR/UNIX/401/0204/SC/1.1 8
UNIX
$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:
SR/UNIX/401/0204/SC/1.1 9
UNIX
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.
$ 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 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).
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
-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
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.
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.
$ who
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.
Ø 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 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.
SR/UNIX/401/0204/SC/1.1 16
UNIX
Science
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
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
$ cd /
$ pwd
/
Creating Directories
$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
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.
With the above command, three directories called temp1, temp2 and temp3 will be
created under the current directory.
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:
$rm –r temp1
The directory temp1 along with all its subdirectories and files will be removed.
SR/UNIX/401/0204/SC/1.1 19
UNIX
$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
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
SR/UNIX/401/0204/SC/1.1 20
UNIX
1---------2-----3-----------4------------5-------------6--------------7
Ø 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
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
Ø ls –x : This option sorts the files alphabetically and prints the output row wise.
$ls -x
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.
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:
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:
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:
So file1 under temp1 directory will be copied to temp2 directory with the name
copiedfile.
Ø 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.
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:
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
SR/UNIX/401/0204/SC/1.1 27
UNIX
mv <Source> <Destination>
The above command will move the file from the source directory to the destination
directory.
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.
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:
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.
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
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 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
file1: This is A.
file2: Thes is B.
file1 file2
SR/UNIX/401/0204/SC/1.1 31
UNIX
My File My File
The first one. The second one.
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.
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
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.
$ head –n5 file1 -> displays the first 5 lines of file file1
$ head –c10 file1 -> displays the first 10 characters of file file1
$ tail –n5 file1 -> displays the last 5 lines of the file file1
$ tail –c10 file1 -> displays the last 10 characters of the file file1
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:
The above command requires the knowledge of two things, namely to whom the
permission will be given and what are those permissions.
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.
Ø Text Method
SR/UNIX/401/0204/SC/1.1 34
UNIX
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:
SR/UNIX/401/0204/SC/1.1 35
UNIX
A very interesting result is obtained if the owner issues the following command:
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.
when a particular user needs to be transferred to another group, the chgrp command is
used.
Search a file
Sometimes it becomes essential to locate the existence of a particular file. The find
command helps to perform the task.
SR/UNIX/401/0204/SC/1.1 36
UNIX
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.
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 –ctime n : This option will return a value of true if the file specified has been
created or modified n days ago.
SR/UNIX/401/0204/SC/1.1 37
UNIX
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.
Ø $ 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.
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
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
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):
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:
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
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
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
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.
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.
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
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.
Ø $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:
Ø $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.
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
$ 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.
$ ls | more
$ls –l | pg
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.
$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
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 example given below starts sorting from the second field.
$sort +1 file1
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.
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 –n: In UNIX, sorting can be done on numeric fields also. The command below
will perform the task.
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.
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.
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:
The following example searches a specified expression in a particular file and will
display the lines containing the expression:
SR/UNIX/401/0204/SC/1.1 51
UNIX
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
The above command will display all the directory and special files under the current
directory.
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.
SR/UNIX/401/0204/SC/1.1 53
UNIX
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.
The command given above will display the lines containing the word hello with the line
numbers. The output is given in the next page:
The command given above will display the count of lines containing the text Hello in the
file called 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 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 “ ”
$ls | tr –s “ “
The parameter used with -s, must be the same as that of the
column separator of the source data
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
SR/UNIX/401/0204/SC/1.1 56
UNIX
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 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:
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
The above command will extract the character from third position of each line till the end
of the line.
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
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
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
Logged in as user1
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:
$grep –iv clerk file1 | sort +2n | cut –d” “ –f1-2 | head -1
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
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.
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
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
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:
If the file you specified does not exist, then it will tell you that it is a new file, like this:
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 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.
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.
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.
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
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)
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.
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:
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
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.)
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:
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:
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.
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`
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
SR/UNIX/401/0204/SC/1.1 72
UNIX
A shell script, when executed works as a command itself. The shell script also accepts
arguments like any other UNIX 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.
$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.
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.
Ø Shell or user-defined variables: These types of variables can be created and then
used by the user.
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
$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”
$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.
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:
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.
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.
SR/UNIX/401/0204/SC/1.1 75
UNIX
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
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
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.
Ø The following command will display the text and sound the system bell.
Ø 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”
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
$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.
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
Environment Variables
Besides user-defined variables, a shell also has some special variables called environment
variables. Some of the environmental variables are given below:
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
$echo $HOME
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.
$echo $PATH
This variable contains the system prompt, the $ symbol. Setting the value of this variable
to the desired prompt can change the system prompt.
$PS1=”Hello Prompt”
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.
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
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
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.
$cat .profile
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
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.
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.
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.
SR/UNIX/401/0204/SC/1.1 84
UNIX
Now, to execute the shell passing arguments, issue the following command:
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.
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.
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.
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
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
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.
Ø 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.
SR/UNIX/401/0204/SC/1.1 88
UNIX
The same above program can be written with the help of elif statement.
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:
if test -n $str2
then
echo "Length of str2 is greater than zero"
fi
SR/UNIX/401/0204/SC/1.1 90
UNIX
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
Ø The script given below finds the highest of three numbers entered by the user using
numeric operators:
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
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.
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
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
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.
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.
Ø The script given below displays a menu and depending on the option selected, it will
execute the respective command.
SR/UNIX/401/0204/SC/1.1 95
UNIX
Ø 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.
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.
SR/UNIX/401/0204/SC/1.1 97
UNIX
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 set of instruction written within the do…done block is executed till the condition
specified with the while statement return true exit status.
Ø 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 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
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.
$ 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 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.
until [ condition ]
do
execute commands
done
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
SR/UNIX/401/0204/SC/1.1 101
UNIX
In the above usage of for loop, the loop body is executed, as many times as there are
items in the list.
for var in a b c d
do
echo " The value of var is : $var"
done
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
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.
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
Ø The script given below finds the factorial of a given number accepted from the user.
Ø 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
Ø 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
Ø 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"
SR/UNIX/401/0204/SC/1.1 107
UNIX
Introduction to EXCEED
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:
For system administrators, Exceed provides tools to set up, configure and administer PCs
remotely to ensure consistency among systems.
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.
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.
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.
X Client Wizard guides you through the steps necessary for connecting to a host
application from your PC.
SR/UNIX/401/0204/SC/1.1 110
UNIX
SR/UNIX/401/0204/SC/1.1 111