100%(1)100% found this document useful (1 vote) 498 views210 pagesWorking With Unix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Working with
UN
™
Ee
is NARESH @
Ni ( technologies
SOFTWARE TRAINING & DEVELOPMENT
An ISO 9001 : 2000 Certified Company
Opp. Satyam Theatre, Ameerpet, Hyderabad - 500 016.
Ph: 23746666, 23784842_UNIX 2 a Page 1 of 208°
Table of Contents © eee
able of Contents élite,
Overview of Operating System
Introduction:
What is an Operating System?
Objectives of Operating Systems
History of Operating Systems
Operating Systems Structuré™
History of UNIX Operating System
Features of UNIX
Operating System and its Types . oy Fah
Architecture of UNIX : See r
Different Flavors of UNIX » ie
Vv vv
UNIX File System Architecture
> Boot Block, Super Block
» Inode Black, Data Block
> exit, date, cal, exit, banner
The Directory Struct
> mkdir, cd, rmdir, rm
Listing Files and Directories:
> Is, Is with options”
> Unlink
Wild card characters or Meta characters
> File substitution
> 1/Oredirection
> Process Execution
> Quoting meta characters. “s
> Positional parameters
Special Parameters ~ “ :
File compression ~ . ‘
> gzip, gunzip, zcat, compress” *
» Uncompress, pack, unpack and Peat ~ z f
File Permissions
> chmod, chown, chgrp, umask
Communication Commands. ~
> write, wall, mail, mail with options “
Networking Commands tos
> telnet, ftp, rlogin, finger, etc.. 2
Backup & Disk Utilities “~ i
v
Naresh i Technologies, Opp Satyain Theatfe, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 2 of 208
> tar
> cpio
> df, du, mount, unmount
Redirection operators
> Redirecting Output
> Redirecting Input
> Standard error
Pipes and Filters 2°
> t,comm., tee, sed, nl
> pg, more, less, head, tail, paste, cut, sort
Exporting Variables
Sed (Stream Editor) & GREP
> grep, grep with options
> fgrep, egrep
Processes and Job control
> ps, kill, Pkill
> Foreground jobs
> Background jobs
>
>
> What is
> Importa
> Different types of shells
> Responsibilities of Shell
> What is a variable?
> System defined variables
> Environment Variables
Operators
> Arithmetic Operators
> Relational Operators
> Logical Operators
> Assignment Operators
Conditional Statements
> If if...else
> If... elif
> Case
Looping,
> While, until , for , break , continue
» More on Loops
> Example Scripts
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 3 of 208
Introduction:
What is an Operating System?
The 1960s definition of an operating system is “the software that controls the
a better. definition. We see an
hardware”. However, today, ae ‘o eer we net :
operating system as the programs that make the hardware useable. In brief, an
operating system is the set of programs that controls a computer. Some examples of
operating systems are UNIX, Mach, MS-DOS, MS-Windows, Windows/NT, Chicago,
OS/2, MacOS, VMS, MVS, and VM- Controlling the computer involves software at
several levels. We will differentiate kernel services, library Sees, and application-
level_services, all of which are part of the operating» To hide details of hardware by: creating abstraction [Link] is software that
hides lower level details and provides a set of higher-level functions. An operating
system transforms the physical world of devices; instructions, memory, and time into
virtual world that is the result of abstractions built by the operating system. There are
several réasons for abstraction. First, the:code needed:to control peripheral devices is
not standardized. Operating systems provide subroutines called device drivers that
perform operations on behalf of programs for example, inptit/ottput operations.
Second, the op
For instance, 0]
have to deal’ yystem transforms the computer
hardware into tiple virtual computers, each belonging to a different program.
Each program that is running is called a process. Each process views the hardware
through the lens of abstractior
through abstraction.
‘ourth, the operating system can enforce security
To allocate resources to processes (Manage resources) an operating system controls
how processes (the active agents) may access resources (passive entities).
> Provide a pleasant and effective user interface the user interacts with the operating
systems through the user interface and usually interested in the “look and feet” of the
operating system. The most important components of the user interface are the
command interpreter, the file system, on-line help, and application integration. The
recent trend has been toward increasingly integrated graphical user interfaces that
encompass the activities of multiple processes on networks of computers. One can
rce manager and extended
machines. Form Resource manager point of view Operating Systems manage the
view Operating Systems from two points of views: Re
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 5 of 208
different parts of the system efficiently and from extended machines point of view
Operating Systems provide a virtual machine to users that is more convenient to usv.
The structurally Operating Systems can be design as a monolithic system, a hierarchy
of layers, a virtual machine system, an exocrine, or usiiig the client-server model. The
basic concepts of Operating Systems are processes, aidmoty. management, 1/O
management, the file systems, and security:
History of Operating Systems:
Historically operating systems havé been tightly related’to the computer architecture,
it is good idea to study the history of operating systems from the architecture of the
computers on which they ran.
time were so pfimitive that programs were often entered one bit at time on rows of
mechanical switches (plug boar¢ ‘amming languages were unknown (not even
assembly languages). Operating systems were uitheard of .
The 1950's - Second Generation
By the early 1950's, the routine had improved somewhat with the introduction of
punch cards. The General Motors Research Laborator
operating systems in early 1950's for their IBM 701. The system of the 50's generally
ies implemented the first
yan one job at a time. These were called single-stream batch processing systems
because programs and data were submitted in groups or batches.
The 1960's - Third Generation
The systems of the 1960's were also batch processing systems, but they were able to
take better advantage of the computer's resources ’by running several jobs at once. So
operating systems designers developed the concept of multiprogramming in which
several jobs are in main memory at once; a processor is switched from job to job as
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 6 of 208
needed to keep several jobs advancing while keeping the peripheral devices in use.
For example, on the system with no multiprogramming, when the current job paused
to wait for other I/O operation to complete, the CPU simply sat idle until the 1/O
finished. The solution for this problem that evolved was, to partition memory into
several pieces, with a different job in each partition. While one job was waiting for
1/O to complete, another job could be using the CPU. Another major feature in third-
generation operating system was the technique called spooling (simultaneous
peripheral operations on line). In spooling, a high-speed device like a disk interposed
ith the program in
between a running program and a low-speed device involved ;
input/output. Instead of writing directly to a printer, for example, outputs are written
to the disk. Programs can run to completion faster, and other programs can be
initiated sooner when the pr jlable, the:outputs may be printed. Note
that spooling tet
like ‘thread being:spun to a spool so that it may be
later be unwound as need resent in this generation was time-
sharing technique, a variant of multiprogramming technique, in which [Link] has
an on-line (i
ixectly connected) terminal, Because the user is present and
interacting with the computer, the computer system must respond quickly to user
requests, otherwise user productivity could suffer. Timesharing systems were
developed to multiprogramming large number of simultaneous interactive users.
Fourth Generation
With the development of LSI (Large Scale Integration) circuits, chips, operating
system entered in the system entered in the personal computer and the workstation
age. Microprocessor technology evolved to the point that it becomes possible to build
desktop computers as powerful as the mainframes of the 1970s, Two operating
systems have dominated the personal computer scene: MS-DOS, written by Microsoft,
Inc. for the IBM PC and other machines using the Intel 8088 CPU and its successors,
pd UNIX, which is dominant on the large personal computers using the Motorola
6899 CPU family.
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 7 of 208
Operating Systems Structure
System Components:
Even though, not all systems have the same structure many modern operating
systems share the same goal of supporting the following:types of system components.
1. Process Management
‘The operating system manages many kinds of activities ranging from user programs
to system programs like printer spooler, name servers, file server étc. Each of these
activities is encapsulated in a process,.A process includes the complete execution
context (code, data, PC, resisters; OS resources in use etc.). It is important to note that
a process is not a program. A process is only ONE instant of a
gram in execution.
There are many processes can be running the same program. ‘The five major activities
Main-Memory Management
Primary-Memory or Main-Memory is a large array of words or bytes. Each word or
byte has its own address. Main-memory provides storage that can be access directly
by the CPU. That is to say for a program to be executed, it must in the main memory.
The major activities of an operating in regard to memory-management are:
> Keep track of which part of memory are currently being used and by whom.
Decide which process is loaded into memory when memory space becomes
available.
Allocate and de-allocate memory space as needed.
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX _ Page 8 of 208
File Management
A file is a collected of related information defined by its creator. [Link] store
files on the disk (secondary storage), which provide long term, storage. Some
examples of storage media are magnetic tape, magneti¢ disk and optical disk. Each of
these media has its own properties like speed, capacity; and data transfer rate and
access methods. File systems normally organized-into directories: to ease their use.
These directories may contain files and other directions. The five main major activities
of an operating system in regard to file management are
1, The creation and deletion of files.
2. The creation and deletion of directions:
manipulating files and directions.
assigned.
Secondary-Storage Management
Generally speaking, systems have several levels of storage, including primary storage,
secondary storage and cache storage. Instructions and’ data must be placed in primary
storage or cache to be referenced by a running program. Because main memory is too
small to accommodate all data and programs, and its data are lost when power is lost,
the computer system must provide secondary storage to back up main memory.
Secondary storage consists of tapes, disks, and other media designed to hold
information that will eventually be accessed in primary storage (primary, secondary,
Cache) is ordinarily divided into bytes or words consisting of a fixed number of bytes.
Each location in storage has an address; the set of all addresses available to a program
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 9 of 208
is called an address space. The three major activities of an operating system in regard
to secondary storage management are:
1. Managing the free space available on the secondary-storage device.
2. Allocation of storage space when new files have to bé written.
3, Scheduling the requests for memory access.
Networking
A distributed system is a collection of processors that do not share memory,
peripheral devices, or a clock. The -protessors communicate with one another through
communication lines called network: ‘The communication-network design must
consider routing and connection strategies, and the: problems of contention and
security. ee
Protection Systert
If a compute! “systen has millage users and allows the concurrent execution of
trolling the access of programs,
Processes, or users to the resou yy computer systems.
Command Interpreter System
A command interpreter is an interface of the operating system with the user. The user
gives commands with are executed by operating system (usually by tur:i‘rg them into
system calls). The main function of a command interpreter is ts get and execute the
next user specified command, Command-Interpreter is usually not part of the kernel,
since multiple command interpreters (shell, in UNIX terminology) may be support by
an operating system, and they do not really need to run in kernel mode. There are two
main advantages to separating the command interpreter from the kernel.
1. If we want to change the way the command interpreter looks, i., I want to change
the interface of command interpreter, I am able to do that if the command interpreter
is separate from the kernel. I cannot change the code of the kernel so [Link] modify
the interface.
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 10 of 208
2. If the command interpreter is a part of the kernel it is possible for a malicious
process to gain access to certain part of the kernel that it showed not have to avoid
this ugly scenario it is advantageous to have the command interpreter separate from
kernel. ;
Operating Systems Services following are the five sérvices provided by operating
systems to the convenience of the users.
Program Execution
The purpose of computer systems. [Link] allow the user to execute programs. So the
operating systems provide an-environment where the [Link] conveniently run
programs. The user does not have to: worry about the memory allocation or
multitasking or’anything. These ings are taken care of by the operating systems.
i ig and: deal locating memory, CPU
jese functions cannot be given to the user-
cannot help the user to run programs
perating systems.
Fach program requires an input and produces output. This involves the use of 1/O.
The operating systems hides the user the details of underlying hardware for the I/O.
All the user sees is that the I/O has been performed without any details. So the
operating systems by providing I/O make it convenient for the users to run
programs. For efficiently and protection users cannot control I/O so this service
cannot be provided by user-level programs.
File System Manipulation
The output of a program may need to be written into new files or input taken from
some files. The operating systems provide this service. The user does not have to
worry about secondary storage management. User gives a command for reading or
writing to a file and sees his task accomplished. Thus operating systems make it easier
for user programs to accomplish their task. This service involves secondary storage
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 11 of 208
management. The speed of I/O that depends on secondary storage management is
critical to the speed of many programs and hence I think it is best relegated to the
operating systems to manage it than giving individual users the control of it. It is not
difficult for the user-level programs to provide these services but for above mentioned
reasons it is best if this service s left with operating system.” f
Communications
There are instances where processes need to’ communicate with each other to
exchange information. It may be between processes running on the same computer or
running on the different computers. By providing this service the operating system
relieves the user of the worry of passing messages between processes. In case where
the messages need to be passed to processes on the other comptiters through a
e user [Link]. be customized to
ich the-message transits and provides the
service interfa
Error Detecti 2
An error is one part of the sys ¢ malfunctioning of the complete system.
To avoid such a situation the operating system constantly monitors the system for
detecting the errors. This relieves the user of thé:worry of errors propagating to
various part of the system and causing malfunctioning. This service cannot allow to
be handled by user- programs because it involves monitoring and in cases altering
area of memory or deal location of memory for a faulty process. Or may be
relinquishing the CPU of a process that goes into an infinite loop. These tasks are too
critical to be handed over to the user programs. A user program if given these
privileges can interfere with the correct (normal) operation of the operating systems.
History of UNIX Operating System:
Since it began to escape from AT&T's Bell Laboratories in the early 1970's, the success
of the UNIX operating system has led to many different versions: recipients of the (at
that time free) UNIX system code all began developing their own differe1 sions in
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 12 of 208
their own, different, ways for use and sale. Universities, research institutes,
government. bodies and Computer Company all began using the powerful UNIX
system to develop many of the technologies which today are part of a UNIX system.
K 2
‘Computer aided design, manufacturing control systems, laboratory si
the Internet it; all began life with and because of UNIX systems. Today, without UNIX
tions, even
systems, the Internet would come to a screeching halt. Most telephone calls could not
be made, electronic commerce would grind to a halt and there would have never been
"Jurassic Park"!
technology wete attaining agemi
computer system suppliets and amon
using UNIX systems.
. And they wanted to continue
Soon all the large vendors, and many smaller ones, were marketing their own,
ed for their own computer
architectures and boasting many different strengths and features. Customers found
that, although UNIX systems were available everywhete, they seldom were able to
interwork or co-exist without significant investment of time and effort to make them
work effectively. The trade mark UNIX was ubiquitous, but it was applied to a
multitude of different, incompatible products.
In the early 1980's, the market for UNIX systems had grown enough to be noticed by
industry analysts and researchers. Now the question was no longer "What is a UNIX
system?" but "Is a UNIX system suitable for business and commerce?"
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX" Page 13 of 208
Throughout the early and mid-1980's, the debate about the strengths and weaknesses
of UNIX systems raged, often fuelled by the utterances of the vendors themselves
who sought to protect their profitable proprietary system sales by talking UNIX
systems down, And, in an effort to further differentiate their competing UNIX system
products, they kept developing and adding features of theif own:
In 1984, another factor brought added attention to UNIX systems. A group of vendors
concerned about the continuing encroachment into their markets and control of
system interfaces by the larger companies; developed the [Link] "open systems."
comprehensive open systems
save on costs; attract a wider
terms. X/Open chose the UNIX
Although UNIX was still owned by AT&T, the company did little commercially with
it until the mid-1980, Then the spotlight of X/Open showed clearly that a single,
standard version of the UNIX system would be in the wider interests of the industry
and its customers. The question now was, "which version?”
Ina move intended to unify the market in 1987, AT&T announced a pact with Sun
Microsystems, the leading proponent of the Berkeley derived strain of UNIX.
However, the rest of the industry viewed the development with considerable concern.
Believing that their own markets were under threat the clubbed together to devs
their own "new" open systems operating system. Their new v organization was called
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 14 of 208
Phy
the Open Software Foundation (OSE), In response to this, the AT&T/Sun faction
formed UNIX International.
The ensuing "UNIX wars" divided the system vendors,between these two camps
clustered around the two dominant UNIX system techriologies: AT&T's System V and
the OSF system called OSF/1. In the meantime,.X/Open Company held the center
ground. It continued the process of standardizing the APIs necessary for an open
operating system specification, -
In addition, it looked at areas of the system beyond the operating system level where
reach would add value for supplier and customer alike, developing or
nnectivity, networking and
Kk were published in successive
time, X/Open had put in place a
supported by testing. Since the
publication of XPG4, X/Open has continued to’ ‘
specifications in line with market requirements. As t
roaden the scope of open systems
benefits of the X/Open brand
became known and understood, many large organizations began using X/Open as the
basis for system design and procurement. By 1993, over $7*billion had been spent on
X/Open branded systems. By the start of 1997 that figure has risen to over $23 billion.
To date, procurements referencing the Single UNIX Specification amount to over $5.2
billion.
In early 1993, AT&T sold it UNIX System Laboratories to Novell which was looking
for a heavyweight operating system to link to its NetWare product range. At the same
time, the company recognized that vesting control of the definition (specification) and
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 15 of 208
trademark with a vendor-neutral organization would further facilitate the value of
UNIX as a foundation of open systems. So the constituent parts of the UNIX System,
previously owned by a single entity are now quite separate
In 1995 SCO bought the UNIX Systems business fro iell,.and UNIX system
oa system
source code and. technology continues to be developed by SCO.
In 1995 X/Open introduced the UNIX-95 brand for computer systems guaranteed to
meet the Single UNIX Specification. The Single UNIX Specification brand program
has now achieved critical mass: vendors whose products have met the demanding
systems by value.
allowing many systems to support the UNIX philosophy of small, often simple tools
that can be combined in many ways to perform often complex tasks. The stability of
the core interfaces preserves existing investment, and is allowing development of a
tich set of software tools. The Open Source movement is building on this stable
foundation and is creating a resurgence of enthusiasm for the UNIX philosophy. In
many ways Open Source can be seen asthe true delivery of Open Systems that will
ensure it continues to go from strength to strength.
*
1969 The Beginning The history of UNIX starts back in 1969, when Ken Thompson,
Dennis Ritchie and others started working on the "little-used PDP-7 in a corner" at
Bell Labs and what was to become UNIX.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842,UNIX Page 16 of 208
1971 First Edition It had a assembler for a PDP-11/20, file system, fork(), roff and ed.
It was used for text processing of patent documents.
1973 Fourth Edition It was rewritten in C. This made*it,portable and changed the
history of OS's. tenet
1975 Sixth Edition UNIX leaves home. Also widely knownas Version 6, this is the first
to be widely available out side-of-Bell-Labs. The first BSD version (I.x) was derived
from V6. :
1982 System II] AT&T's UNIX System Group (USG) release System III, the first public
release outside Bell Laboratories. SunOS 1.0 HP-UX introduced. Ultrix-11
Introduced.
1983 System V Computer Research Group (CRG), UNIX System Group (USG) and a
third group merge to become UNIX System Development Lab. AT&T announces
UNIX System V, the first supported release. Installed base 45,000.
1984 4.2BSD University of California at Berkeley releases 4.2 BSD, includes TCP/IP,
new signals and much more. X/Open formed.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 17 of 208
1984 NIT2 System V Release 2 introduced. At this time there are 100,000 UNIX
installations around the world.
1986 4.3BSD 4.3 BSD released, including internet name’server. SVID introduced. NFS
shipped. AIX announced. Installed base 250,000.
1987 NIT3 System V Release 3 including STREAMS, TLI, RFS. At this time there are
750,000 UNIX installations around the world. IRIX introduced.
1988 POSIX. published. Open Software Foundation (OSF) and UNIX International
(Ul formed. Ultrix 4.2 ships.
1989 AT&T UNIX Software Operation formed in preparation for spin-off of USL.
Motif 1.0 ships.
1989 NIT UNIX System V Release 4 ships, unifying System V, BSD and XENIX.
Installed base 1.2 million.
1990 XPG3 X/Open launches XPG3 Brand. OSF/1 debuts. Plan 9 from Bell Labs ships.
1991 UNIX System Laboratories (USL) becomes a company - majority-owned by
1992 NIT.2 USL releases UNIX System V Release 4.2 (Destiny). October - XPG4 Brand
launched by X/Open. December 22nd Novell announces intent to acquire USL. Solaris
2.0 ships.
1993 4.4 BSD 4.4 BSD the final release from Berkeley. June 16 Novell acquires USL.
- Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 18 of 208
Late 1993 NIT.2MP Novell transfers rights to the "UNIX" trademark and the Single
UNIX Specification to X/Open. COSE initiative delivers "Spec 1170" to X/Open for
fasttrack. In December Novell ships NIT.2MP,, the final USL OEM release of System V
1994 Single UNIX Specification BSD 44-Lite eliminated all code claimed to infringe on
USL/Novell. As the new owner of the UNIX trademark, X/Open introduces the
Single UNIX Specification (formerly Spec 1170), separating the UNIX trademark from
any actual code stream.
1995 UNIX 95 X/Opei introduces the UNIX 95 branding programmed for
implementationis. of the Single UNIX Specification. Novell sells UNIXWare business
igit IXWare 2.0 ships. OpenServer 5.0 debuts.
1997 Single UNIX Specification, Version 2 The Open Group introduces Version 2 of
the Single UNIX Specification, including support for real-time, threads and 64-bit and
ailable on the web. IRIX 6.4, AIX
larger processors. The specification is made freely
4.3 and HP-UX 11 ship.
1998 UNIX 98 The Open Group introduces the UNIX 98 family of brands, including
Base, Workstation and Server. First UNIX 98 registered products shipped by Sun, IBM
and NCR. The Open Source movement starts to take off with announcements from
Netscape and IBM. UNIXWare 7 and IRIX 65 ship.
1999 UNIX at 30 The UNIX system reaches its 30th anniversary. Linux 2.2 kernel
released. The Open Group and the IEEE commence joint development of a revision to
Naresh # Technologies. [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 19 of 208
POSIX and the Single UNIX Specification. First LinuxWorld conferences. Dot com
fever on the stock markets. Tru64 UNIX ships.
2001 Single UNIX Specification, Version 3 Version 3 of the Single UNIX Specification
unites IEEE POSIX, The Open Group and the indtstry ‘efforts. Linux 2.4 kernel
teleased. IT stocks face a hard time at the markets. The value of procurements for the
UNIX brand exceeds $25 billion. AIX SL ships.
2003-- ISO/IEC 9945--2003 The core’ volumes of .Version
the Single UNIX
‘estwood" test suite
Specification are approved as an international standard, The."
2009 UNIX at 40-- IDC on UNIX market -- says UNIX $69 billion in 2008, predicts
UNIX $74 billion in 2013 :
What is Unix, Exactly .
Unix is a computer operating system first developed at Bell Labs (and, to get the legal
language out of the way, a trademark’ of AT&T Bell Laboratories). An "operating
system" is a master program which coordinates other programs! activities and
manages files.
One of the most popular and widespread operating systems in the world, Unix runs
on more brands of computers than probably any other operating system in existence.
This is partly because Unix is "portable', it is written in C, a high-level, machine-
Narésh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 20 of 208
independent language. Programs written on one Unix machine can be easily adapted
to other Unix machines (C is particularly well-integrated with the operating system
itself).
In addition, UNIX is based on a collection of small, ¢ lérstood utilities which
allow you to connect them in many different ways (and in'ways that the authors did
not predict), building procedures and sophisticated tasks:to suit your own needs. This
"Unix philosophy" is often contrasted with monolithic programming environments
(IBM mainframes or the Macintosh *** are sometimes mentioned). in which you can.
only perform tasks the system designers could predict; such systems, while becoming
of difficulty, you may find that you are more than rewarded by the power of UNIX.
So why do you, want to use UNIX? Here are the most commonly given reasons:
* + Unix allows a number of people to work on the same machine at once. It also
allows multitasking *, so each person can work on several things at once.
* 7
* It's a good environment for programmers: UNIX is portable, has excellent
software tools, and has unified concepts and a flexible file system in which everything
is text.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 21 of 208
* Unix is particularly well-suited to networking; electronic mail, network news, file-
transfer programs, remote logins, and terminal-to-terminal communication are very
easy on UNIX systems,
* Unix systems are all over campus, and if you lear UNIX on dre machine, you have
an advantage when you move to any of the others.
* Unix systems are everywhere. (This iS-especially true in the academic world.)
JNTRODUCTION:
and engineering workstations,
es and super computers as well
Multi-user: More than one us hine at a time supported via terminals
(serial or network connection)
Malti-tasking: more than one program can be rut at a time hierarchical directory
structure to support the organization and maintenance obfiles.
Portability: only the kernel (<10%) written in assembler tools for- program
development a wide range of support tools (debuggers, compilers).
Portability: The system is written in high-level language making it easier to read,
understand, change and, therefore move to other machines. The code can be changed
and contplied on a new machine. Customers can then choose from a wide variety of
hardware vendors without being locked in with a particular vendor.
Machine-independence: The System hides the machine architecture from the user,
making it easier to write applications that can run on micros, mini and mainframes
‘Naresti / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 22 of 208
Hierarchical File System: UNIX uses a hierarchal file structure to store information.
This structure has the maximum flexibility in grouping information in a way that
reflects its natural state. It allows for easy maintenance and efficient implementation.
ata time. Amounts
Multiprogramming -Support more'than one program, in memo:
to multiple user processes on the system.
Operating System is a collection of “System Software Programs” which is
coordinating the actions of the computer. That is allocating the resources of the
system, Operating System is a software component. There exist iwo types of Soft
wares. They are
AC Application Software
& System Software
system Software. E.g.: Compilers,
x are two different Operating
Systems. There: exist differ
Operating Systems.
+ Operating System and its Types:
1. Single User and Single tasking OS: In late 1979's DOS was invented by Microsoft
Corporation. It is a “Single User and
Single Tasking OS". That means “At a moment of time only one user is allowed to
work with the computer and he is able to do only one task’. The important feature of
DOS is CUI (Character User Interface).
2: Single User and Multi tasking OS: In late 1983’s another OS, WINDOWS
was developed and released by Microsoft corp. It is a “Single User and Multi
Tasking OS”, That means “At a moment of time “only one user is allowed to work
with the system and he can be able to do more than one work”. The important feature
of WINDOS is GUI (Graphical User Interface).
Naresh # Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 23 of 208
3. Multi user and Multi tasking OS: In late 1960’s UNIX was invented by Dennis
Ritchie e] at Bell Laboratories. It is a “Multi-user and Multi
tasking OS”. That means “At a moment of time More than one user is allowed and ‘he
Hire: of, UNIX is CUI (Character
raphical objects was
is able to do more than one task”. The important feat
User Interface). Later, the software component that supports t
developed by Massachusetts University, which is named as XWindows. While
installing the UNIX os if you can check the Option XWindows, the UNIX will be
installed in GUI Mode.
Types of UNIX: There are many: different versions of UNIX, although they share
e kernel, the shell and the
[Link]; th
to programs and handles the file [Link]-communications in-respanse_to system
calls. As an illustration of the way that the shell and the kernel work together,
suppose a user types rm myfile (which has the effect of removing the file myfile). The
shell searches the filestore for the file containing the program rm, and then requests
the kernel, through system calls, to execute the program rm on myfile. When the
Process rm myfile has finished running, the shell then returns the UNIX prompt % to
the user, indicating that it is waiting for further commands.
¥The shell
The shell acts as an interface between the user and the kernel. When a user logs in, the
login program checks the username ’and password, and then starts another program
Called the shell. The shell is a command line interpreter (CLI). It interprets the
Commands the user types in and arranges for them to be carried out. The commands
‘Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 24 of 208
are themselves programs: when they terminate, the shell gives the user another
prompt (% on our systems).
‘The adept user can customize his/her own shell, and users can use different shells on
the same machine. Staff and students in the school have'the tcsh shell by default. The
tcsh shell has certain features to help the user inputting conimands:
Filename Completion -
By typing part of the name of a commaid, filename or directory and pressing the
[Tab] key, the tcsh shell will:complete the rest of the name automatically. If the shell
finds more than one name bExinning with those letters you [Link], it will beep,
prompting you to tvpe a few more letters before pressing the tab key again.
Architecture of UNDG
‘ther Application ’
Anasten SW
a UNIX
S Seramands sult
S Kemel: Kernel is a part of the UNIX/Linux OS and is loaded in to the memory when
the system is booted. It manages the system resources, allocates time between users
and processes, decides process priorities, and performs all other tasks. Kernel is an
anterface between shell and hardware.
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad, Ph:040 23746666, 23734842UNIX Page 25 of 208
Shell: Shell is an interface between user and kernel. And it is the “Command
Interpreter”, In UNIX/Linux there exist different types of shells. They are In UNIX
“Bourne Shell developed by Stephen Bourne
A kom Shell developed by David Korn
Lanes Developed by Ian Bell
In Linux:
3. Bash Shell is nothing but Bourne again
4. tcsh shell is nothing but turbo shell
Naresh # Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 26 of 208
UNIX File System Structure
One of the features which make UNIX especially attractive is the structure of its file
system. UNIX makes use of an inverted branching-tree file structure. The tree trunk
consists of a single large file, "/" (called root),’ whiéltcontains all the files on the
as ate-called "directories").
Any number of-subdirectories may branch from this main trunk. However, the
system. (In this structure, all files which contain other
hierarchical structure does not end there, for each major branch may also spawn any
number of further branches (sub-subdiréctories). These’ subdirectories may, iri their
turn, send forth yet more levelsiof [Link] so on. All'[Link] names are
separated by */', so you'll find directory names like "/ust/local/ doe/ email-addr".
As an example of the way t
example. Someone: with a usetname of "user" creates a directory to contain her
| such highlevel directories she has in her home
s: "personal," "memos,"
“professional. "These subdirectories contain her basic files: in this case, individual
letters and memos. She might also store programs (which, like most everything else in
UNIX, are really files at heart) in any of her directories. With the branching structure
of her files and directories (much like'folders on a Macintosh, or nested folders in a
real-world filing system), "user" could easily to keep track of a large number of files.
Directories.
The UNIX filesystem is hierarchical (resembling a tree structure). The tree is anchored
at a place called the root, designated by a slash "/". Every item in the UNIX filesystem
tree is either a file, or a directory. A directory is like a file folder. A directory can
contain files, and other directories. A directory contained within another is called the :
child of the other. A directory in the filesystem tree may have many children, but it
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 27 of 208
can only have one parent. A file can hold information, but cannot contain other files,
a! ee
or directories.
bin usr ete kb
< 4 N\ ——_“
staff student
7X
jon. steve
Part of the filesystem tree
indows world. You cannot run a
pattitions are a subset of the root
partition.
/boot: This contains the necessary. stuff to start the machine, including the base
kernel. This partition is optional, but usually present. You will not need to mess
around in here if you have a running system. *
/usr; This is the directory where global executables are stored. It can be read-only, if
you want. Generally speaking, most software is installed here by default.
Vfaev: This is the directory where all of your devices are. There are a few useful
examples for you to know. /dev/hda is the first ide hard drive. /dev/hdb is the
second. /dev/sda Id be the first SCSI drive, and /dev/sg0 is your robotic arm.
/etc: This is where most configuration files are stored. You will spend a lot of time in
here if you are an administrator. Most files require "root" access to change.
/var: Many of the system log files are here, as well as spools (mail, printer...)
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad ‘Ph:040 23746666, 23734842UNIX Page 28 of 208
/bin: This directory is the home of binary executables. These include the common
commands we have already learned like ls, cat, gzip and tar.
Different Flavors of UNIX
» BSD (Berkeley Software Distribution)
> Sun Solaris (Sun Micro Systems)
> Novell Netware (NOVELL)
> IBM SIX (International Business Machines)
> HP-UX (Hewllet Packard) _
> TRU 64 (Hewllet Packard) 7
> LINUX
UNIX File System Architecture
INODE BLOCK
DATA BLOCK
Boot Block: these stores bootable objects that are nécessary for booting the system. It
has completely bootable information.
Super Block: This Stores all the information about the filesystem like
Size and status of the file system
Size of file system logical block
Date and time of the last update
Number blocks allocated
Number blocks unallocated.
File System State
Naresh Technologies, [Link] Theatre, Ameerpet, Hyderabad: Ph:040 23746666, 23734842UNIX Page 29 of 208
UNIX FILE SYSTEM ARCHITECTURE
Boot Block
Super Block
| Inode Block |
|
|
| Data Block
Inode Block: An inode contains all the information about a file except its name, which
is kept in directory. It contains the following oF
The type ofthe file and the mode ofthe file %
The data and ti
The date and
Data Block: Data:blocks are
allocated to the file system.
UNIX Command Syntax:
> All commands have a similar format
commands are generally two to five characters lon;
2 Commands are case sensitive
‘age blocks contains the rest of the space that is
> Options always preceded filenames _
> Options are prefixed using a - symbol
ne man command can be used to display the correct syntax»
> Ifyou make typing mistake press backspace to erase characters.
> To cancel entire the command before you press enter, press the delete key.
> Don’t tum off the computer if you have made mistake press ctil+d.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX. Page 30 of 208
UNIX Command Line Structure:
A command is a program that tells the UNIX system to do something. It has the
form:
Kennan [options] [arguments]
Where an argument indicates on what the command is to perform its action,
usually a file or series of files. An option modifies the command, changing the way |
it performs.
Commands are case sensitive. Command and Command are not the same.
Options are generally preceded by @hyplien (-), and for most commands, more
than one option can be strung together, inthe form:
Command - [option] [option][option]
commands. However, not all UNIX
Options and syntax for a command are listed in the man page for the command.
command
options
Is -al /etc
-
file/directory
Control Keys
Control keys are used to perform special functions on the command line or within an
editor. You type these by holding down the Control key and some other key
simultaneously. This is usually represented as “Key. Control-S would be written as
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX: * i Page 31 of 208 -
AS, With control keys upper and lower case are the same, so “S is the same as “s, This
particular example is a stop signal and tells the terminal to stop accepting input. It
will remain that way until you type a start signal, *Q.
Gontrol-U is normally the "line-kil" signal for your tefininal. When typed it erases the
entire input line.
In the VI editor you can type a control key into your text file by first typing “V
followed by the control character desired, so to type “H into a dociument type *V*H.
$stty - terminal control =
sity reports or sets terminal control options: The ‘ity"\is an abbreviation that, harks
back: to. the qs of teletypewriters; which were associated “with transmission of
early computer terminals.
e stty command is. setting the erase
- datai transmission speed
- Parity checking on data transmission
-hardware flow control
sngiftaie (NE) versus carriage return plus linefeed (CI
~inft@fBréting tab characters
- edited versus raw input
- mapping of upper case to lower case
This command is very system specific, so consult the man pages for the details of the
stty command on your system.
Syntax
stty [options]
indlogies, Opp. Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 32 of 208
(none) report the terminal settings
all (or -a) report on all options
echoe echo ERASE as BS-space-BS
dec set modes suitable for Digital Equipment Corporation operating systems (which
distinguishes between ERASE and BACKSPACE) (Not ‘available ‘on all:systems)
kill set the LINE-KILL character
erase set the ERASE character
intr set the INTERRUPT character -
Examples -
You can display and change your terminal control settings with the stty command. To
display all (-a) of the current line settings:
you automatically each time you login, it can be tnsrted into the login or profile file
that we'll look at later.
Hel;
The UNIX manual, usually called man pages, is available,on-line to explain the usage
of the UNIX system and commands. To use a man page, type the command "man" at
the system prompt followed by the command for which you need information.
Syntax: man [options] command_name
Common Options
-k keyword list command synopsis line for all keyword matches
-M path path to man pages
-a show all matching man pages (NIT)
Examples
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 33 of 208
You can use man to provide a one line synopsis of any commands that contain the
keyword that you want to search on with the "-k" option, e.g. to search on the
keyword password, type:
$man -k password
passwd (5) - password file
passwd (1) - change password information
The number in parentheses indicates the section of the man pages where these
references were found. You can-then access:the man page (by default it will give you
the lower numbered entry, but you cair use a command line,option to specify a
different one) with:
$man passwd
ame and Password, that is as
follows
Login
Passwd — : NareshTech
‘After you had entered correct Login name and password, then it will show
#9 System Administrator Prompt
52 User Working Prompt -
To open an UNIX terminal window, click on the "Terminal" icon from
Applications/Accessories menus
gies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 34 of 208
SS aeowrsot
Bp ceopsces
BE Syste toas
8 vee Sewer
Ce
How to create a new user:
duseradd username
Example: useradd NareshTech
How to create a passwd
#passwd username
Example: passwd NareshTech
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX = Page 35 of 208.
Different UNIX Commands:
© Sdate: It is used to display current svstem date and time.
Syntax: date [options] [+format]
Example: $date
Mon Jan 1 04:30 IST 2006
Common Options:
$date +%d > It displays the current system date [ m™-0 J
$date +%m > It displays the current system month
$date +%y ~ It displays the current system year in short form
$date +%Y > It displays the current system year in full fort
Sdate +%H >t ‘stem time in number of hours
Sdate +%M urrent system time in number of minutes
$date +%S > It displays the current system time in number of seconds
Sdate +a > It displays the current system day in short form
Sdate +%A > It displays the current system day in full form
Sdate +%b - It displays the current system month in short form
$date +%B > It displays the current system month in full form
@scal: It displays previous month, current month and next month calendar.
Fax: $cal [options] [month] [year]
Example: $cal 8 2002
Cal command will print the calendar on current month by default. If you want to
print calendar of August of 2002.
August 2002
SMTu WThES
ae eee
8 91011121314
15 1617 18 19 20 21
‘Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 36 of 208
22 23 24 25 26 27 28
29 3031
$cal year: It will display the specified year calendar.
Ex1:$cal 2000 (it takes year from 1 to 9999)
@ Swho: This command displays a list of the people using the system,
Syntax: $who [options]
Example:
NareshTech _tty01 Jan1 0400. _
Neo tty02 Jan2 0450 2
NareshTech123tty03 Jan 1 06:00
Neo123 Jan 6 04:00
NareshTech ;
Tty01
Jan1 Mon and Date
04:00 STime
4 Swhoami: It will display the name of the current user, terminal number date and time
at which you logged into the system.
Syntax: §Swhoami [options]
Example:
NareshTech ttyO1 Jan 1 04.00
| $clear; It is used to clear the scréen.
) Spwid: It will display current working directory name.
Syntax: $pwd [options]
Example: /home/NareshTech/Neo123
}$su: Stands for Switch User, you want switch, when you are switching user to admin,
we should enter password, if we are switching Admin to user , should not required
password.
Syntax: $su [options] [username/admin name]
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 37 of 208
Example: $su NEO
Enter Password: *****#**
$passwd: It is used to change the password of the current user. After executing this
command it will first ask the old password of the curreittuser and then it will wait for
the new password. Then enter new password. and agaiti new password. After
doing like this the system will set the new password for the current user.
Syntax: $passwd [options]
Example: =
$Enter current Passwd_
$Enter New Passwd
$Confirm New Passwd
word followed:-this command in. the
of ASCII art poster, for example
at UNIX command line or in my
# 4 HeEEHE
4 # 8 # 8 #
4 # # 4 # #
ase BHBREE #
wo eo #
# ee b # #
Sexit: To logout from current user
3¢% Administrator can connect any user without password: But a user wants the
password to go for any other user.
AD - indicates end of data stream; can log a user off. The latter is disabled on many
systems
AC interrupt
logout - leave the system.
exit - leave the shell
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842| re
UNIX Page 38 of 208,
$tty: Tty command will display your terminal.
Syntax: $tty [options ]
Options
-1 will print the synchronous line number. Pe.
-$ will return only the codes: 0 (a terminal), 1 (not a térininal), 2 (invalid options)
(good for scripts)
Changing Run Levels: (#init)
Admin always work in the /root es
J. Hinit0 > to shutdown the system:
Jy #init1 > to bring the user into single user mode
A. tinit 6
How to inter si
GUI to CUL :
Ctrl+alt+F1
Ctrl+alt+F2
Ctrl+alt+F3
Ctrl+alt+ Fa
Ctrl+alt+F5
Ctrl+alt+F6
CUI to GUI
Ctrl+alt+F7
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842Page 39 of 208
The Directory Structure
Fall the files are grouped together in the directory structure. The file-system is
“arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is
traditionally called root (written as_a slash /). In UNIX/Linux there exists some
standard directories; they can vary among the versions of these OS, Here the structure
of the directories is Hierarchical structure. This structure is also called as UNIX File
= system Structure, because directories are also called as Directory Files. The structure
is as follows.
In the diagram below, we see that the home directory of the-undergraduate student
"eeS1vn'" contains two sub-directories (d\
The full path to the file [Link] is "/home/its/ugl/ee51vn/[Link]"
ind pics) and a file called [Link].
Goes J bes j[repon. de }
Directory Related Commands:
@ $mkdir (make directory):
This command is used to create a directory with the specified name.
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842Sa
UNIX Page 40 of 208
Syntax: $mkdir [options] [dimame (s)]
Example: mkdir NEO
‘This command creates a directory with name NEO
@®Change Directory: ~ : .
$cd: Stands for change directory this command is used°td change a'directory from
current directory to the specified directory,
Syntax: $ cd [options] [target-dirname]
Ex$ cd NEO a
Spwd 7
/ust/NareshTech/NEO
‘The above comtmand moves the user from the current directory to the directory NEO
. Represents current director
Examples
cd (also chdir in some shells) change directory
cd changes to user's home directory
cd / changes directory to the system's root
~. goes up one directory level
d ../.. goes up two directory levels
cd /full/path/name/from/root changes directory to absolute path named (note
leading slash)
cd path/from/current/location changes directory to path relative to current locaj
‘leading slash)
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842 "4
AUNIX Page 41 of 208
cd ~username/directory changes directory to the named’ username's indicated
directory. .
@ $rmdir : This command is used to remove the specified directory, when it is empty.
Syntax:Srmdir [options] [dirname]
Ex: $ rmdir NEO
This command deletes the directory NEO
Disk Related = Oh du-
The system Administrator has to regularly monitor the integrity of the file system and
the amount of disk space available, ,
Checking Disk Free Space:
system.
Syntax: df
Example: $df
If we want more detailed information abouttisk usaj
Sd -ivt :
Dfspace makes more sense:
The above command reports the free disk in terms of megabytes and percentage of
total disk space.
$dfspace
dfspace not found
It came because the dfspace command is present in /ete directory.
Example: S/etc/dfspace
Disk Usage- The du command:
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842"UNIX Page 42 of 208
du sounds similar to df but is different in its working. df and dfspace report the disk
space available [Link] file system as a whole whereas du reports the disk space used
by specified files and directories.
Example: $du
Examape: $du /ndev
@ The file Command: file Command;
iFwe want to see all ‘we want to see all the files from current, sent dtertory then we have type the following
command.
$file *
file - file type
This comm
displays several types of files, This program, files, examines the
selected file
-c check the magic file for errors in format
-f file_list file_list contains a list of files to examine.
-h don't follow symbolic links (SVR4 only)
-L follow symbolic links (BSD only) :
-m magic_file use magic_file as the magic file instead of /etc/magic
Files and Users
Ans: A file is nothing but a collection of data. That means it contains some data. In
UNIX/Linux, we are having three types of files. They are
1. Directory Files.
2. Ordinary Files. :
3, Special Files or Device Files.
A file that contains at least one file with in that is called as a Dirstony Fis
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 43 of 208,
A file that is not containing any file with in it is called as an Ordinary File.
aay
A file that contains some files that are related to devices of the system devices,
they are known as Device Files.
Device Files.
QFileRelated commands § Got
Scat (Concatenate) this command is used for three purpidsés as follows)
a) To create a file.
1) To display file data.
¢) To append data at the end of the fle.
Common Options ee
line with a line number
Syntax: cat >
Abe $cat>
Tt) After typing.
your required data.
for entering data, and then enters
TH) To quit from the cat command press , means saving and quit from the
cat or file.
b) Tadisplay the file contents
Kon mple: Scat < NiT
Meraple: Scat NiT NiT1 NiT2 NiT3: >The above command shows the data of the
specified files.
¢) To append data at the end of the file.
Syntax: $ cat >> filename
Ex$ cat >> NiT
By doing this the shell waits for entering data. This data will be added atthe end of
the file managers. After typing your required data press ctrl+d.
Naresh i Technologies, Opp-Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 44 of 208
Stouch - create a file
The touch command can be used to create a new (empty) file or to update the last
access date/time on an existing file. The command is used primarily when a script
requires the pre-existence of a file (for example, to [Link] append information) or
when the script is checking for last date or time a functiOni Was performed.
Syntax
touch [options] [date_time] file
touch [options] [-t time] file s
Common Options
-a change the access time of the file (SVR4 only)
-c don't creat
file if it doesn't already exist
[[CC}YYJMMDPhhmm[.Ss} ©
$touch > to create an empty file.
$ touch NiT NeO file3 > To create multiple emp!
- Syntax: $touch [options] [filename (s)]
Example: $touch Neo Neol Neo2 Neo3
Later
Creating Hidden files:
Which file is creating starting with dot (,) is called hidden file.
Example: Scat > .
The above example creates hidden file with a specified name
E g file
$mv NiT .NiT
‘The aboye command hide the existing file.
Naresh Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 45 of 208
Unhide the files
gmv .NiT NiT
The above command unhide the file.
ist |
pty
or | Removing files:
$rm: To remove the given files
Syntax: $rm [options] [filename (s)]
Example $[Link] > It removes NiT file
Example $rm -i NiT > It asks the conformation before deleting file.
Do u want remove NiT? Y> It removes u—~* ~
2 n> Itwon't removes
\oves all these files
~rrecursively remove a directory, first removing the files and subdirectories beneath it
-£ don't prompt for confirmation (overrides -i)
The shift Command &
The shift command allows you to effectively left shift'your positional parameters. If
you wa the command
shift
whatever was previously stored inside $2 will be assigned to $1, whatever was
previously stored in $3 will be assigned to $2, and so on. The old value of $1 will be
irretrievably lost.
When this command is executed, $# (the number of arguments variable) is also
automatically decremented by one:
Scat tshift Program to test the shift
echo $# $*
Naresh. Technologies, Opp-Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842Sr
UNIX Page 46 of 208
shift
echo $# $*
shift
echo S# $*
shift
echo Sit $*
shift
echo $# $*
shift
echo $# $*
$tshiftabcde =
Sabcde
4bcde
3cde
ft (that is, when $# already equals
xror will vary from one shell to
where prog is the name of the program: that exec the offending shift.
You can shift more than one "place" at once by writing a count immediately after shift,
asin
shift 3 %
This comunand has the same effect as performing three separate shifts:
shift €
shift
shift
The shift command is useful when processing a variable number of arguments
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 47 of 208
Listing Files and Directories:
Is (stands for iisting ): This command is used to display all files names under
current directory.
Options:
$ls -1 > It displays long listing of files. It displays all files aid-directories with
following attributes.
i). Number of blocks allocated
i). Type of the file
File permissions
iv). Number of links
v). Owner of
vi). Group of
viii), File create
ix). File Name,
$Is -x > It displays all files and directories width
$1s-a> As you can see, ls -a lists files that are normally:hidden
Is does not, in fact, cause all the files in your home directory to be listed, but only
those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are
known as hidden files and usually contain important program configuration
information: They are hidden because you should not change them unless you are
very familiar with UNIX. To list all files in your home directory including those
whose names begin with a dot.
Sls -F: > It displays all files, directories, executable file and symbolic files.
NiT\ > Directory file
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad: Ph:040 23746666, 23734842UNIX. Page 48 of 208
NEO* > Executable file
UNIX @ > Linked file etc.,
CHARACTER IF ENTRY IS A
d directory -
- plain file :
b block-type special file
¢ character-type special file
I symbolic link e
s socket 7
Example: cp /home/neo123/ “(Target filename is not mandatory)
Rename a file (Moving files)
$mv(move): This command rename the given filé name with the given new name.
We can also move files physically from one directory to'another directory.
Syntax: §mv [options] [source] [target]
Example: §mv NiT NeO
To move a file from one place to another, use the mv command, This has the effect of
moving rather than copying the file, so you end up with only one file rather than two
Different Types of files
- Ordinary File
d Directory File
b Special block File
¢ Special Character file
L Symbolic File
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 49 of 208,
Different file symbols
Ended with / > Directory, Ended with @> Link file, Ended with * > Exe file
Ended with any special character is ordinary file,
Linux Shows Colors ts /
Sky blue > Link file, Dark blue > Directory, Green > EXE file, Red > Zip file
Black or white > ordinary file YS" ~-davee, tnlee.-
Swe (word count): It is used to count and display the number of lines, words and
characters of the specified file and it display the file name also.
Syntax:-$we [options] [file-name (8)]
Example: $we NiT > It displays the following output
Example: Swe Ni. NeO BiT
10 20
62 98
87 78
159 196 361 Total
Examples:
i), $we -I NiT> It displays 10 lines
ii). $we -w NiT> It displays 20 words
iii). $we ~c NiT> It displays 67 characters
iv). $we -we NiT> It displays 20 words and 67 characters
vi). $we -wl NiT> It displays 20 words 10 lines
Vii). $we -Ic NiT> It displays 10 lines and 67 characters.
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 50 of 208
ks
A link is not a kind of file but instead is second name for a file. When a file had two
links, it is not physically present at two places, but can be referred to by either of the
names. This command links the specified files, in affect if the user made any changes
to any one of these file, then those changes will affect the bothi files.
File links are divided into two types:
1. Hard links
2. Soft links
Hard links: A file can have as mafy as many names'as you want to give it, but the
only thing that is common to all of them they all have the same inode number.
Syntax: $ In [Source file] [target file]
Ex:$ In NiT : !
Here -I optior number, that means they are
10:20 NiT
. 10:20 NeO
The number of links which is normally one for unlinked files, is shown to be two. All
these linked files have equal status, its not that one file contains the original data and
the others don’t, changes made in one file will be affected automatically in the other
file for the simple reason that there is only single copy of the-file in the disk.
‘These links have two limitations .
1. You can’t have two linked files in two file system
2. You can’t link a directory even within the same file system
Soft links: These are links are also known as soft links. These soft links were first
introduced by Berkeley with BSD UNIX, but it is now available in alll flavors of UNIX.
Unlike hard links, a symbolic link points to the file which actually have contents. In-
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX- - _. Page 51 of 208
fact, Windows shortcuts are more like symibolic links, You can create a symbolic link
with the -s option of In, but this time the listing tells you thé different’story:
Syntax: $In -s [source file] [target file]
Example: $In -s NiT NeO
$s -i
25360 -rw-r~1-- 2NiTNeO 81 August 22° 10:25. NiT
2538 «© -Irwxrwxrwx 2NiTNeO 81 August : 22 10:20 NeO
You can identify symbolic links by the-character 1 [Link] the permission field. It
supports directory linking also. - -
Unlink: The name itself indicates that it is used for removing thé links from the files
and the directories. This command removes the links of the files and ‘directories either
it could be a soft
Example: $unlink NeO
Here, it removes the links whichare sdbtitated with the given file NeO
File comparison commands
Scmp: This command compares the given two files, byte by byte and'displays the
differed lines with their filenames. The cmp
mand compares two files, and
(without options) reports the location of the first difference between them. It can deal
with both binary and ASCII file comparisons. It does a byte-by-byte comparison.
Syntax: $cmp [options] filel file2 [skip1] [skip2]
The skip numbers are the number of bytes to skip in each file before starting’ the
comparison.
Common Options
-Lreport on each difference
-s report exit status only, not byte differences Syntax: $ cmp [option] [NiT] [NeO]
Ex $ cmp NiT NeO
Note: If, it doesn’t display any output, that means the files are same.
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 52 of 208
$comm: This command requires two sorted files, and it compares the data of these
files, line by line and display.
Syntax: $comm [options] [NiT] [NeO]
Example: $comm NiT NeO :
$Diff: Display which lines in one file have to be changed’ to
e two files identical.
The diff command ‘compares two files, directories, etc, and reports all differences
between the two. It deals only with ASCII files. Its output format is designed to report
the changes necessary to convert the first file into the second.
Syntax i oe - i
diff [options] file1 file2
Syntax: $diff [option] [NiT] [NeO]
Example: $diff NiT NeO
$Unigq: This command compares the lines with in
the reads the second line, if the second is similar to the first, then it discards the
display of second line, :
gle file. This reads a line and
Syntax: $uniq [options] [+|-n] file [filenew]
‘Common Options
-d one copy of only the repeated lines
~uselect only the lines not repeated
+n ignore the first n characters
-sn same as above (SVR¢4 only)
-n skip the first n fields, including any blanks ( & )
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 53 of 208
-f fields same as above (SVR4 only)
Syntax: unig filename
Ex $uniq NiT
Options:
- out put only the unique lines
c out put only the single copy of each line with a number to its left indicating
the number of times that line is in the input.
-d out put only the single copy of the duplicated lines
Naresh j Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNDC Page 54 of 208
Wild card characters or Meta characters
Regular Expression Metacharacters
Metacharacter Function Example What It Matches
a Beginning-of-line /ove/ — Matche¥'all lines beginning with
anchor love
$ End-of-line anchor _/love$/__ Matches all lines ending with love
Matches one character /l.e/ _ Matchés lines containing an 1,
_ followed by two characters,
followed by an e
Matches zero ormore /*love/ — Matches lines'With zero or more
of the preceding spaces, followed by the pattern
x acters love 5
0 Matches lines containing love or
bey] Matches letters from A through Z
followed by ove
1 Matches any character not in the
2 : range between A and Z
\ Used to escape a /love\./ Matches lines containing love,
metacharacter followed by a literal period;
Normally the period matches one
of any character
In UNIX/Linux some characters in our key board are having special meaning
according to the shell. They are
2 Represent zero or single occurrence of any character.
$ls?> It displays all single character files
$ls 22 > It displays all double character files
Sis N7U > It displays all files which are starting with N and Ending With T but file
‘eneth should be three characters.
* Represent zero or single or more than one Occurrences of any character.
P g
Naresh i [Link], [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 55 of 208
$ls*> It displays all files
$ls N*T > It displays all files which are starting with N and Ending With T
List: [chich2] Represent either chi or ch2.
Sls [abcdef]* > It displays all files which are starting with-specified list _
$ls [!abcdef}* > It displays all files which are not starting With specified list
Range: [ch1-ch2] Represent a single character with in the specified range.
[!ch1-ch2]Represent neither of the characters with in the range.
$ls [a-g] * > It displays all files betweer’specified range”
Printing Statements us
$echo It is used to display the text after the echo. But it'can [Link]
the quoting characters and shell variables.
The single quote character’
There are several reasons that you might need td use quotes in the shell. One of these
is to keep characters otherwise separated by whitespace characters together. Let's look
at an example. Here's a file called phonebook that contains names and phone
numbers:
$ cat phonebook
Alice Chebba 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776 =
Susan Topple 212-555-4932
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX : Page 56 of 208
Tony Iannino 973-555-1295
$
To look up someone in our phonebook file—which has been kept small here for the
sake of example —you use grep: :
$ grep Alice phonebook
Alice Chebba 973-555-2015
$
Look what happens when you look-up Susan:
$ grep Susan phonebook
Susan Goldberg 201
Susan Topple 212-555-4932
$
There are two lines that contain Susan, thus explaining the two lines of output. One
way to overcome this problem would be to further qualify the name. For example,
you could specify the last name as well:
$ grep Susan Goldberg phonebook
grep: can't open Goldberg
Susan Goldberg 201-555-7776
Susan Topple 212-555-4932
$
Recalling that the shell uses one or more whitespace characters to separate the
2° guments on the line, the preceding command line results in grep being passed three
aiguments: Susan, Goldberg, and phonebook
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 57 of 208
grep Susan Goldberg phonebook.
When grep is executed, it takes the first argument as the pattern and the remaining
arguments as the names of the files to search for the-pattern. In this case, grep thinks
it's supposed to look for Susan in the files Goldberg and phonebook. So it tries to open.
the file Goldberg, can't find it, and issues the error message:
grep: can't open Goldberg
next file, phonebook, opens it, searches for the pattern Susan, and
ils down. to-trying to pass whitespace
“done by enclosing the entire
grep ‘Susan Goldberg’ phonebook
When the shell sees the first single quote, it ignores any otherwise special characters
that follow untilitsees the closing quote,
S$ grep ‘Susan Goldberg’ phonebook
Susan. Goldberg 201-555-7776
$
In this case, the shell encountered the first ', and ignored any special characters until it
found the closing ', So the space between Susan and Goldberg, which would have
normally delimited the two arguments, was ignored by the shell. The shell therefore
divided the command line into two arguments, the first Susan Goldberg (which
includes the space character) and the second phonebook. It then executed grep,
Passing it these two arguments.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph;040 23746666, 23734842UNIX Page 58 of 208
grep 'Susan Goldberg’ phonebook,
grep Pl ope arguments | * Susan Goldberg
phonebook
grep then took the first argument, Susan Goldberg, and looked for it in the file
specified by the second argument, phonebook. Note that the shell removes the quotes
from the command line and doesnot pass them to the program...
No matter how many space characters are enclosed between quotes, they are
preserved by the shell.
one two three four
In the first case, the shell removes the extra whitespace characters from the line and
passes echo the four arguments one, two, three, and four.
echo one two three four.
arguments
echo
As we mentioned, all special characters are ignored by the shell if they appear inside
single quotes. That explains the output from the following:
$ file=/users/steve/bin/progi
Naresh ¢ Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 59 of 208
$ echo $file
/users/steve/bin/progl
Secho'Sfile' $ not interpreted
Sfile
$ echo*
addresses intro lotsaspaces names nu numbers phonebook stat
$ echo"
*
$echo'<> |
<> LQ (>
Even the Enter key will be t's enclosed in quotes:
$ echo ‘How are you today,
> John!
How are you today,
John
$
After typing the first line, the shell sees that the quote isn't matched, so it waits for
you to type in the closing quote, As an indication that the shell is waiting for you to
finish typing in a command, it changes your prompt character from $ to >. This is
known as your secondary prompt character and is displayed by the shell whenever
it’s waiting for you to finish typing a command.
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX, Page 60 of 208.
Quotes are also needed when assigning values containing whitespace or special
characters to shell variables:
$ message='I must say, this sure is fun’
$ echo $message
I must say, this sure is fun
$ text="* means all files in the directory! —
$ echo Stext
names nu numbers phonebook stat means all files in the director
ignments made to the variables message and text
» preceding example, you are reminded that the
iable name substitution, meaning that
the files in the current directory before the echo is
this ance, and it's through the use of
The Double Quote
Double quotes work similarly to single quotes, except that they're not as restrictive.
Whereas the single quotes tell the shell to ignore all enclosed characters, double
quotes say to ignore most. In particular, thie following three characters are not ignored
inside double quotes:
Dollar signs
Back quotes
Backslashes
The fact that dollar signs are not ignored means that variable name substitution is
done by the shell inside double quotes.
Sit
$ echo $x
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 61 of 208
addresses intro lotsaspaces names nu numbers phonebook stat
$ echo '$x'
$x
$ echo "$x"
$ Here you see the major differences between no quotes; single quotes, and double
quotes, In the first case, the shell sees the asterisk and substitutes all the filenames
from the current directory. In the second case, the shell leaves the characters enclosed
within the single quotes alone, which results in the display of $x, In the final case, the
double quotes indicate to the shell that variable name sub
performed inside the quotes. So the shell substitutes * for'$x. Because filename
done inside double quotes, * is then passed to echo as the value to
tuted, but don't want the shell to
‘ou must enclose the variable inside double
ce between double quotes and no
> New York, N. Y. 10003"
$ echo Saddress
39 East 12th Street New York, N. Y. 10003
$ echo "address"
39 East 12th Street
New York, N. Y. 10003
$
It makes no difference whether the value assigned to address is enclosed in single
quotes or double quotes. The shell displays the secondary command prompt in either
case to tell you it's waiting for the corresponding closed quote.
After assigning the two-line address to address, the value of the variable is displayed
by echo. Notice that the address is displayed on a single line. The reason is the same
as what caused.
echo one two three four
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 62 of 208
to be displayed as
one two three four
Recalling that the shell removes spaces, tabs, and newlines (that is, whitespace
characters) from the command line and then cuts it up into arguments, in the case of
echo Saddress
the shell simply removes the embedded newline character, treating it as it would a
space or tab: as an argument delimiter. Then it passes the nine arguments to echo to
be displayed. echo never gets.a chance to see that newline; the shell gets to it first.
echo addres: a
echo
When the command
echo "$address"
is used instead, the shell substitutes the value of address as before, except that the
double quotes tell it to leave any embedded whitespace characters alone. So in this
case, the shell passes a single argument to echo—an argument that contains an
embedded newline. echo simply displays its single argument at the terminal. The
newline character is depicted by the characters \n.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 63 of 208
echo "Saddress".
age = = NS 39 Gast 12: Street Now York, N.¥.10008
Double quotes can be used to hide single quotes from the shell, and vice versa:
$ x=" Hello,' he said". =
$ echo $x
'Hello,' he said
The Backslash
Basically, the backslash is eqetvalene to placing single quotes around a single
character, with a few minor exceptions. The backslash quotes the single character that
immediately follows it. The general format is
\c
where c is the character you want to quote. Any special meaning normally attached to
that character is removed. Here is an example:
Secho >
syntax error: ‘newline or ;' unexpected
$ echo \>
>
$
In the first case, the shell sees the > and thinks that you want to redirect echo's output
to a file. So it expects a filename to follow. Because it doesn't, the shell issues the error
message. In the next case, the backslash removes the special meaning of the >, so it is
passed along to echo to be displayed.
$x=*
$ echo \$x
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842F
UNIX Page 640f208 4
4
$x 4
t {
In this case, the shell ignores the $ that follows the backslash, and as a result, variable
substitution is not performed.
Because a backslash removes the special meaning [Link] character that follows, can
you guess what happens if that character is another backslash? Righitvit removes the
special meaning of the backslash:
$ echo \\
\
$
Naturally, you could have also written
Singlé quotes al'sh
$ echo "Slines"
one
two
$lines-one\ _—‘Try it witha \ instead
> two
$ echo "$lines"
onetwo
$ J
‘The shell treats a backslash at the end of the line as a line continuation. It removes
newline character that follows and also does not treat the newline as an argu
delimiter (it's as if it wasn't even typed). This construct is most often used for ty)
long commands over multiple lines.
The Backslash Inside Double Quotes
We noted earlier that the backslash is one of the three characters interpreted 1
shell inside double quotes. This means that you can use the backslash insidé
quotes to remove the meaning of characters that otherwise would be interps
inside double quotes (that is, other backslashes, dollar signs, back quotes, new
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 2373484UNIX “Page 65 of 208
and other double quotes). If the backslash precedes any other character inside double
quotes, the backslash is ignored by the shell and passed on to the program:
$ echo "\$x"
$x
$ echo "\ is the backslash character"
\ is the backslash character
$x=5
$ echo "The value of x is \"$x\"
The value of x is "5"
$
In the first example, the backslash precedes the dollar sign, interpreted by the shell
inside double quotes. So the shell ignores the dollar sign, removes the backslash, and
executes echo, In the second example, the backslash precedes a:space, not interpreted
by the shell inside double quotes. So the shell ignores the backslash and passes it on to
the backslash ‘tised to-enclose double
Now try displaying the line without using any q
$ echo <<< echo $x >>> displays the value of x, wi
syntax error: '<' unexpected
$
The < signals input redirection to the shell; this is the reason for the error message.
Ifyou put the entire message inside single quotes, the value of x won't be substituted
at the end. If you enclose the entire string in double quotes, both occurrences of $x
will be substituted. Here are two different ways to do the quoting properly (realize
that there are usually several different ways to quote a string of characters to get the
results you want):
$ echo "<<< echo \$x >>> displays the value of x, which is $x"
<<< echo $x >>> displays the value of x, which is 1
$ echo '<<< echo $x >>> displays the value of x, which is' $x
<<< echo $x >>> displays the value of x, which is 1
$
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX. Page 66 of 208
In the first case, everything is enclosed in double quotes, and the backslash is used to
prevent the shell from performing variable substitution in the first instance of $x. In
the second case, everything up to the last $x is enclosed in single quotes. If the
variable x might have contained some filename substitution or whitespace characters,
a safer way of writing the echo would have been
echo '<<< echo $x >>> displays the value of x, which
Command Substitution:
Command substitution refers to the shell's capability to insert the standard output of a
command at any point in a command line. There are two ways in the shell to perform
command substitution: by enclosing a shell command with back quotes and with the
$(...) construct. e
The Back Quote
The back, quote is unlike any of the Sm encounteréd pes of quotes. Its
purpose is not to protect characters from the shell but to:tell the shell to execute the
enclosed [Link] to output from the command at that point
[1] Note that using the back quote for command substitution is no longer the
preferred method; however, we cover it here because of the large number of older,
canned shell programs that still use this construc 0, you should know about back
quotes in case you ever need to write shell programs that are portable to older Unix
systems with shells that don't support the newer S(...) construct.
Here is an example:
$ echo The date and time is: “date”
The date and time is: Wed. Aug 28 [Link] EDT 2002
$
When the shell does its initial scan of the command line, it notices the back quote and
expects the name of a command to follow. In this case, the shell finds that the date
command is to be executed. So it executes date and replaces the ‘date’ on the
comunand line with the output [Link] date. After that, it divides the command line
into arguments in the normal manner and then initiates execution of the echo
command.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 67 of 208
$ echo Your current working directory is ‘pwd™
Your current working directory is /users/steve/shell/ché
$
Here the shell executes pwd, inserts its output on the command line, and then
executes the echo. Note that in the following section, back quotes can be used in all
the places where the $(...) construct is used.
‘The $(...) Construct
The POSIX standard shell supports: the newer §(...) construct. for command
substitution. The general format is =
$
uple of reasons. First, complex
juotes can be difficult to read,
This construct is better than back quotes for a:
commands that use combinations of forward and bat
particularly if the typeface you're using doesn't have visually different single quotes
and back quotes; second, S(...) constructs can be easil ested, allowing command.
substitution within command substitution. Although nesting can also be performed
with back quotes, it's a little trickier. You'll see an example of nested command
substitution later in this section.
You are not restricted to executing a single command between the parentheses:
Several commands can be executed if separated by semicolons. Also, pipelines can be
used. Here's a modified version of the nu program that displays the number of
logged-in users:
Scatnu
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 68 of 208
echo There are $(who | we -1) users logged in
$nu Execute it
There are 13 users logged in
$
Because single quotes protect everything, the following output should’be clear:
$ echo'${who | we-I) tells how many users are logged in’
$(who | we-I) tells how many users are logged in
You have
$
(What causes those leading spaces before the’7?) Remember that the shell is
responsible for executing the command enclosed between the parentheses. The only
thing the echo command sees is the output that has been inserted by the shell.
Suppose that you're writing a shell program and want to assign the current date and
time to a variable called now, perhaps to display it later at the top of a report, or log it
into a file. The problem here is that you somehow want [Link] the output from date
and assign it to the variable. Command substitution can be used for this:
$now=$(date) Execute date and store the output in now
Secho$now See what got assigned
Wed Aug 28 [Link] EDT 2002
$
When you write
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 69 of 208
now=$(date)
the shell realizes that the entire output from date is to be assigned to now. Therefore,
you don't need to enclose $(date) inside double quotes,
éputcan be'stored inside a
Even commands that produce more than a single line of
variable:
§ filelist=$(1s)
$ echo Sfilelist a
addresses intro lotsaspaces names nu numbers phonebook stat
$
What happer ting of the files even though
the newlines fr fable (take our word for it). The
newlines got ‘as substituted by the shell in '
around the variable will preserve
intro
lotsaspaces
names
nu
numbers
phonebook
stat
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842i
UNIX Page 70 of 208, 4
$
To store the contents of a file into a variable, you can use cat:
$ namelist=$(cat names)
$ echo "names"
Charlie
Emanuel :
Fred
Lucy
Ralph
Tony
Tony
$
If you want to mail the contents of the file memo to all the people listed in the names
file (who we'll assume here are users on your system), you can do the following:
$ mail $(cat names) < memo
$
Here the shell executes the cat and inserts the output on the command line so it loo!
like this:
mail Charlie Emanuel Fred Lucy Ralph Tony Tony < memo
Then it executes mail, redirecting its standard input from the file memo and passing
the names of seven users who are to receive the mail.
Notice that Tony receives the same mail twice because he's listed twice in the name4
fie. You can remove any duplicate entries from the file by using sort with the
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 71 of 208
option (remove duplicate lines) rather than cat to ensure that each person only
receives mail once:
$ mail $(sort -u names) < memo
§
It's worth noting that the shell does filename substitution after it’ substitutes the
output from commands. Enclosing the commands inside double quotes prevents the
shell from doing the filename substitution on this output if desired.
Command substitution is often used to change the value stored in a shell variable. For
example, if the shell variable name contains someone's name, and you want to convert
every character in that variable to uppercase, you could:use ech
tr's input, perform the translation, and then assign the result back
The next example shows how cut is used to extract the first character from the value
Stored in a variable called filename:
$ filename=/users/steve/memos
$ firstchar=$(echo $filename | cut -cl)
$ echo Sfirstchar
/
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842 _UNIX Page 72 of 208
$
sed is also often used to "edit" the value stored in a variable. Here it is used to extract
the last character from the variable file:
$ fileexec.o
S lastchar-S(echo Sfile | sed 's/.*\(.\)$/\1/")
$ echo $lastchar
The sed comma says to replace all. the characters on the. line with the last one. The
result of the
sted. Suppose that you want to ‘change every
nething else. In a previous example,
aracter from filename, but how do
filename? A two-step process is
one way:
$ filename=/users/steve/ memos
$ firstchar=§(echo $filename | cut -cl)
$ filename=§(echo $filename | tr "$firstchar""*") translate / to “
$ echo $filename
Ausers*steve*memos
$
Ora single, nested command substitution can perform the same operation:
§ filename=/users/steve/ memos
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 73 of 208
$ filename=$(echo $filename | tr "$(echo $filename | cut -cl)""*")
$ echo $filename
Auserssteve* memos
$
Ifyou have trouble understanding this example, compare it to the previous one: Note
how the firstchar variable in the earlier éxample is replaced by the nested command
substitution. =
Syntax: - $ echo “Any normal Text or $-variable name”
\On where n is
\t tab (NIT) ;
\fform-feed (NIT)
\nnew-line (NIT)
\v vertical tab (NIT)
The $# Variable
Whenever you execute a shell program, the special shell variable $# gets set to the
number of arguments that were typed on the command line. As you'll see in the next
chapter, this variable can be tested by the program to determine whether the correct
number of arguments was typed by the user.
The next program called args was written just to get you more familiar with the way
arguments are passed to shell programs. Study the output from each example and
make sure that you understand it:
Scat args Look at the program
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842i
UNIX. 2 Page 74 of 208
echo $# arguments passed 8
echo arg 1 = ‘$1: arg 2 = $2: arg 3 = :$3:
$argsabc Execute it
3 arguments passed
arg 1 =:a: arg 2=:b: arg 3=:c:
$ args ab Try it with two arguments
2 arguments passed ;
arg 1=a:arg2=:b:arg3=: Unassigned args are null
$ args Try it with no arguments
0 arguments passed
S args "abc"
1 arguments passe¢
arg 1=:abc:arg2
$Isx* "See what files start with x
xact
xtra
$ args x* Try file name substitution
2 arguments passed
arg 1 = :xact: arg 2= :xtra: arg 3 =:
$ my_bin=/users/steve/bin
$ args $my_bin And variable substitution
1 arguments passed
Naresh i Technologies. Opp Satyam Theatre, Ameerpet, Hyderabad: Ph:040 23746666, 23734842UNIX. Page 75 of 208
arg 1 = :/users/steve/bin: arg 2= :: arg 3= =
$ args $(cat names) Pass the contents of names
7 arguments passed
ang = :Charlie: arg 2 = ‘Emanuel: arg3 = Fred:
$
As you can see, the shell does its normal ‘command-line’ processing even when it's
executing your shell programs. This means that you can take adv:
Atage of the normal
an indeterminate or variable number of arguments.
You'll see some more practical examples later. Here's a program that illustrates its use:
$ cat args2
echo $# arguments passed
echo they are :$*:
$ args2abe
3 arguments passed
they are :abc:
$ args2 one two
2 arguments passed
they are :one two:
Naresh i Technologies. Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
alUNIX Page 76 of 208
$ args?
0 arguments passed
they are :
$ args2*
8 arguments passed
they are :args args2 names nu phonebook stat xact xtra:
Exit Status
inder the Unix system, it returns an exit
jt-usually indicates whether the
status of zero indicates that a
it failed. Failures can be caused by
error condition detected by the
program. For éxample, the cp command returns a nonzero exit status if the copy fails
for some reason (for example, if it can't create the destination file) or ifthe arguments
aren't correctly specified (for example, wrong number of arguments, or more than two
arguments and the last one isn't a directory). In the case of grep, an exit status of zero
(success) is returned if it finds the specified pattern in at least one of the files; a
nonzero value is returned if it can't find the pattern or if an error occurs (the
arguments aren't correctly specified, or it can't open one of the files).
Ina pipeline, the exit status is that of the last command in the pipe. So in
who | grep fred
Naresh i Technologies. Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 77 of 208
the exit status of the grep is used by the shell as the exit status for the pipeline. In this
case, an exit status of zero means that fred was found in who's output (that is, fred
was logged on at the time that this command was executed).
The $? Variable ae
The shell variable $? is automatically set by the shell to the [Link] of the last
command executed. Naturally, you can use echo to display its value at the terminal.
$ cp phonebook phone2
$ echo $?
$ echo $?
2 Copy "failed"
$ who See who's logged on
root console Jul 8 10:06
wilma tty03 Jul 8 12:36
barney tty04 Jul 8 14:57
betty tty15 Jul 8 15:03
$ who | grep barney
barney tty04 Jul 8 14:57
$ echo $? Print exit status of last command (grep)
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 78 of 208
0 grep "Succeeded"
$ who | grep fred
$ echo $?
1 grep "failed"
$.echo $?
0 Exit status of last echo a
5 .
Note that the numeric result of a "failure" for some commands can vary from one Unix
version to the ess is always signified by azero exit status.
$caton
# Sy
# determine if someone is logged on
#
user="$1"
if who | grep "Suser"
then
echo "Suser is logged on"
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 79 of 208
This fist argument typed on the command line is stored in the shell variable user.
Then the if command executes the pipeline .
who | grep "$user"
and tests the exit status returned by grep. If the exit status is zero, grep found user in
who's output. In that case, the echo command that follows is executed. If the exit
status is nonzero, the specified user is not logged on, and [Link] command is +
skipped. The echo command is indented from the left mar; ‘for aesthetic reasons
root console Jul 8 10:37
barney tty03 Jul 8 12:38
fred tty04 Jul 8 13:40
joanne tty07 Jul 8 09:35
tony tty19 Jul 08:30
lulu tty23 Jul 8 09:55
$ on tony We know he's on
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX. Page 80 of 208
tony tty19 Jul8 08:30 Where did this come from?
tony is logged on
$ onsteve We know he's not on
$Sonann Try this one =
joanne tty07 Jul8 09:35
" annis logged on
$
specified user is logged on, the corresponding line from: who's output is also
displayed. This:may not be such a 1 bad thing, pt the program requirements called for
| nothin,
who | grep "Su
function of writing any matching lines to standard
output, even though we're really not interested in that. We can dispose of grep's
output by redirecting it to the system's "garbage can," /dev/null. This is a special file
on the system that anyone can read from (and get [Link] end of file) or write
to. When you write to it, the bits go to that great bit bucket in the sky!
who | grep "Suser" > /dev/null
The second problem with on appears when the program is executed with the
argument ann, Even though ann is not logged on, grep matches the characters ann for
the user joanne, What you need here is a more restrictive pattern specification, which
you leamed how to do in Chapter 4, "Tools of the Trade," where we talked about
regular expressions, Because who lists each username in column one of each output
Naresh i Technologies. [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 81 of 208
line, we can anchor the pattern to match the beginning of the line’ by preceding the
pattern with the character *:
who | grep "$user" > /dev/null
But that's not enough. grep still matches a line like
bobby tty07 Jul 8 09:35
if you ask it to search for the pattern bob. What you need to do is also anchor the
pattern on the right. Realizing that who ends each username with one or more spaces, -
the pattern
“A$user "
uuser="$1"
if who | grep "Suser " > /dev/null
then
echo "Suser is logged on’
fi
$ who Who's on now?
root console Jul 8 10:37
Naresh Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 82 of 208
barney tty03 Jul 8 12:38
fred tty04 Jul 13:40
joanne tty07 Jul8 09:35
tony tty19 Jul 8 08:30
lulu tty23. Jul 8 09:55
$ on lulu
lulu is logged on
Sonann Try this again
command prompt will be returned, In the next section, you'll see how to test whether
the correct number of arguments has-been supplied to a program and, if not, take
some action.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX, Page 83 of 208
File compression
There are three commands to zip the files
1. gzip
2. compress
3. pack
Sgzip
On gzip the original file is replaced by another which:has. the same name with .gz
extension. _
Syntax: $gzip [options] [filename (s)]
Example: $gzip NiT
Example: $zcat [Link]
Scompress
On gzip the original file is replaced by another which has the same name with .z
extension. i.
Syntax: $compress [options] [filename (s)]
Example: Scompress NiT
Suncompress:
On uncompress, the zip file is replaced by normal file.
Syntax: Suncompress [options] [filename (6)]
Example: $uncompressp NiT.z
$zcat:
Naresh / Technologies, [Link] Theatre, Ameefpet, Hyderabad Ph:040 23746666, 93734842UNI Ss. Page 84 of 208
I display the contents of zip formatted file,»
Syntax: Szcat [options] [filename(s)]
Example: $zcat NiT.z
Spack .
On grip the original file is replaced by another which-has the-same-name with .gz
extension.
Syntax: $pack [options] [filename (s)]
Example: $pack NiT
Sunpackp:
On gunzip, the zip file is replaced by normal file.
[options] [filename (s)]
Syntax: $pcat
Example: $pc
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 85 of 208
File Permissions
UNIX is unusual in that, under default protection, anyone on the system may read
any file (except for mail files), and whether or not it is in their home directory, though
only the actual owner of the file may alter it. Directories'are-alsoy-as a rule, open:
others may list the files in or connect to (though not alter) most directories.
(Don't forget that the best way to ensure the security of your files is to change your
password regularly, using the ‘passwd’ command.)
Access privileges in Unix are divided into three kinds: "read,"
almost always yourself), people in one
somewhat cryptic fashion. ("Is -a" also lists this f
usually preferences files.) " .
In UNIX each and every file may have 3 permissions. They are read [i], write [w], and
execute [x]. We can modify or change these permissions for a directory or file. Here
changing means adding or removing a particular or a set of permissions.
The column of ten letters or -'s on the far left are the privileges information. A "d" in
the far left indicates a directory. Files would have a "-" there instead. The next nine
characters are the read (r), write (w), and execute (x) privileges for owner or "user"
(first three), group (next three), and everyone else or "others" (last three):
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph:040 23746666, 23734842UNIX | Page 86 of 208
Read
means that a file can be looked at and copied or, for a directory, the contents can be
listed. (Usually, you use ‘more filename' to view a text file one screen at a time; this
does not work for compiled files.)
Write
Means that a file can be edited, overwritten, deleted, moved, etc. For a directory,
this means that files can be placed there, the directory can be moved, and so on.
Execute
Means that the file can be run (like a program). For directories, “execute” permission
means that the directory can be opened. (Standard UNIX commands; like 'Is', are just
1 aplicated pograms, like 'ftp',)
ileges, which means the owner
rone else-on the system can only execute (run)
You own your own home directory, and, by default, this is set so that you can read,
write, and execute it and everyone else can read and: execute it. The default for files
inside your home directory is for you to read and write them and everyone else to
read them. If you want, you can use chmod to change the protection on your home
directory so that “others” cannot read or execute your home directory, but this is not
encouried. It is better to change the protection on individual files. If you do change
the protection on your home directory, you should maintain "execute" privilege for
"others". This will not allow people to see the contents of your directory, but it will
allow some utilities like finger to work properly.
So these permissions can be modified using two methods. They are
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 87 of 208
a) Symbolic mode
b) Absolute mode
a) In Symbolic mode we can represent file permissions and modifying symbols and
types of users (users will be discusses later) are as follows...
Permissions users symbols
read or owner u adding -
ee group g removing :
execute x others °
all a. :
‘Types of users: In the above, owner/user [u], group [g] and
different types of users.
$ chmod utx, g-w, o+rwx NiT 5
This command will provide execute permission to owner, remove write from group
and provide read, write and executé permissions on theefile Nil.
In Absolute mode °
In Absolute mode the representation is a combination of 3 digit code. In this code we
should use the digits in the range 0-7, and 1 represents the presence and 0 represents
the absence of a particular ‘permission. The permissions can be changed by using one
command.
'§ chmod 427 NiT
This command provides read for owner, write for group, all permissions to the file
NiT. That is how, see below.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX Page 88 of 208
[Link] for owner 2is for group Zis for others
100 10 11> (inary form of 427)
rwx rwx rwWx
T-- -wWe Tw Xx (result)
Permission Weight *
Read 4
Write 2
Execute 1 .
Read and Write 6
Write and Execute 3
Read and Execute 5
Read write and Execute
ssion, group is having only write
The way to change the privileges setting on a fil
mode) command. There's two ways to do this, and
both of them in great detail.
directory is the "chmod" (change
online manual pages talk about
In brief, the first way is numeric and the second is symbolic. In the numeric system,
read privileges are given the value 4, write [Link] given 2, and execute
privileges are given 1, These numbers are added together for the owner (user), your
group, and others. So that, for example, "chmod 644 myfile" gives you read and write
privileges for "myfile” (4+2) and your group and others only read privileges (4). To
make the file executable, you can type "chmod 755 myfile"; so that now you can read,
write and execute it (4+2+1) and everyone else can read and execute it (4#1).
‘The other way to use chmod is with the abbreviations "u" for user (you), "g" for group,
and "o" for others; read, write, and execute are r, w, and x, just like in the "Is -1" listing.
Thus, to add write privileges for members of your group to myfile, type "chmod g+w
uvfile"; to remove read privileges for others, type "chmod o-r myfile".
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 89 of 208
So if you want othersto be‘able to read and copy or use a file but not alter it, give
them read privileges (and execute, if it's an executable file). To allow others to list a
directory, give them read privileges (and to connect to the directory, execute
privileges).
You own your own home directory, and, by default; this is set so-that'you can read,
write, and execute it and everyone else can read and exécute it The default for files
inside your home directory is for you to read and write them and everyone else to
read them. You might want to change the privileges of your home directory for
others, or you might want to just protect certain files in your home directory.
To reset the level of privacy by changing the privileges on a file or directory, you'll
need the ‘chmod’ (change mode):command, which you can use in two completely
different ways. The easier way is using the abbreviations "u" for user (you), "g" for
group, and "o" for others; read, write, and execute are "r", "w"yand"x'. Thus, to add
write privileges'for members of your group to myfile, type “chmod: g+w myfile"; to
ns on a file -by-file basis. The
ly when you create any file or
determine permissions for all uxture files with asingle c command.
1. Whenever file is created UNIX assumes that the permissions for this file should be
666.
2. Whenever directory is created UNIX assumes.
should be 777.
Example:
$umask 242
This would see to it here onwards any new file that you create would have the
the permissions for this file
permissions 424 (666-242)
That means: for a file
The user has Read
The group has write
The others has Read permissions
For Directory
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 90 of 208
Any directory that you create would have the permissions 535 (777-242)
The user has Read and Execute
The group has Write and Execute
The others has Read and Execute permissions
#chown - change ownership
Ownership of a file can be changed with the chown command. On most versions of
UNIX this can only be done by the super-user, ie: a normal user can't give away
ownership of their files. chown is used as below, where # represents the shell prompt
for the super-user:
Syntax
Examples |
# chown [new_owner] [file name]
Creating New Group
#groupadd: Itcreates a new group under existing uset.
Syntax:
#groupadd [options] {new group] [filename]
Example
groupadd NiT NeO
#chgrp - change group
Anyone can change the group of files they own, to another group they belong to, with
the chgrp command.
Syntax
chgrp [options] group file
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX Page 91 of 208 i
Common Options
-R recursively descend through the directory structure
-f force, and don't report any errors
Examples
#chgrp [new_group] {filename}
The test Command
A built-in shell command called test is most often used for testing one or more
conditions in an if command. Its generalformat is
test expression
where expression represents the condition you're testing. test evaluates expression,
and if the result is true, it [Link] exit-status of zero; o; otherwise, the result is false,
test "$name" =
The = operator is used to test whether two valties are identical. In this case, we're
testing to see whether the contents of the shell variable name are identical to the
characters julio. If it is, test returns an exit status of Zero; nonzero otherwise.
Note that test must see all operands ($name and julio):and operators (=) as separate
arguments, meaning that they must be delimited by ‘one or more whitespace
characters.
Getting back to the if command, to echo the message "Would you like to play a
game?" if name contains the characters julio, you would write your if command like
if test “Sname" = julio
then
echo "Would you like to play a game?"
fi
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNE Page 92 of 208
(Why is it better to play it safe and enclose the message that is displayed by echo
inside quotes?) When the if command gets executed, the command that follows the if
is executed, and its exit status is tested. The test command is passed the three
arguments $name (with its value substituted, of course), =, and julio. test then tests to
see whether the first argument is identical to the third argument and returns a zero
exit status if it is and a nonzero exit status ifitisnot. ©...
The exit status returned by test is then tested. If it's zero, the commarids between then
and fi are executed; in this case, the single echo command is executed. If the exit status
isnonzero, the echo command is skipped.
It's good programming practice to enclose sliell variables that are arguments to test
inside a pair of double quotes (to allow variable substitution)..This ensures that test
sees the argument in the case. where ‘its value is null. Fo!
following example:
following:
test=julio _
When test executed, it saw only two arguments.
By placing double quotes around the variable, you that test sees the argument
because quotes act as a "placeholder" when the argument is null.
§ test "Sname' = julio
$ echo $? Print the exit status
1
$
String Operators
Operator Returns TRUE (exit status of 0) if
string: = string: is identical to string.
string
string: = string: is not identical to string.
‘Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
iiiUNIX Page 93 of 208
String Operators
Operator Returns TRUE (exit status of 0) if
string
string string is not null.
-nstring string is not null (and string must be seen by test):
-zstring —_string is null (and string must be seen by test).
You've seen how the = operator is used. The != operator is similar, only it tests two
strings for inequality. That is, the exit status from test is zero if the two strings are not
equal, and nonzero if they are. *
Let's look at three similar examples.
$ day="monday'
e the value of day is equal to the
$ %
Here we assigned the characters monday—including the space character that
immediately followed—to day. Therefore, when the previous test was made, test
Teturned false because the characters "monday " were not identical to the characters
"monday".
If you wanted these two values to be considered equal, omitting the double quotes
would have caused the shell to "eat up" the trailing space character, and test would
have never seen it:
$ day="monday "
$ test $day = monday
$ echo $?
0
8 True
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 94 of 208
Although this'seems to violate our rule about always quoting shell variables that are
arguments to test, it's okay to omit the quotes if you're sure that the variable is not
null (and not composed entirely of whitespace characters).
You can test to see whether a shell variable has a null value with the third operator
listed in Table 8.1: eo
test "$day"
This returns true if day is not null and false if it is. Quotes are not necessary here
because test doesn't care whether it sees an argument in this case. Nevertheless, you
are better off using them here as well because if the variable consists entirely of
whitespace characters, the shell will get rid of the argument if not enclosed in quotes.
$blanks="_"
$ test $blanks Is it not null?
$ echo $2
1 _ False—it's null
§ test "Sblanks"
hy arguments because the shell ate up the four
‘ot ofié argument consisting of four space
In case we seem to be belaboring the point about blanks and quotes, realize that this is
a sticky area that is a frequent source of shell programming errors. It's good to really
understand the principles here to save yourself a lotof f programming headaches in the
future.
There is another way to test whether'a string is null, and that's with either of'the last
two operators listed previously in Table 8.1. The -n operator returns an exit status of
zeto if the argument that follows is not null. Think of this operator as testing for
nonzero length.
The -z operator tests the argument that follows to see whether it is null and returns an
exit status of zero if it is. Think of this operator as testing to see whether the following
argument has zero length.
So the command
test -n "$day"
returns an exit status of 0 if day contains at least one character. The command
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX ‘ Page 95 of 208
test -z"Sdataflag"
returns an exit status of 0 if dataflag doesn't contain any characters.
Be forewarned that both of the preceding operators expect an argument to follow;
therefore, get into the habit of enclosing that argument inside double quotes.
$nullvar= »
$ nonnullvar=abc
$test-n"Snullvar" Does nullvar have nonzero length?
$ echo $2 _
1 No
$test-n"$nonnullvar" And what about nonnullvar?
$ echo $?
0 Yes
$ test-z "Snullvar" Does nullvar have zero length?
$ echo $?
‘or example, if the shell variable
ens if you try to test it for zero
length:
$ echo Ssymbol
$ test -z "Ssymbol"
sh: test: argument expected
$
The = operator has higher precedence than the -z operator, so test expects an
argument to follow. To avoid this sort of problem, you can write your command as
test X"$symbol" = X
which will be true if symbol is null, and false if it's not. The X in front of symbol
prevents test from interpreting the characters stored in symbol as an operator.
An Alternative Format for test
The test command is used so often by shell programmers that an alternative format of
the command is recognized. This format improves the readability of the command,
especially when used in if commands.
Naresh i Technologies, [Link] Theatre. Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 96 of 208
You'll recall that the general format of the test command is
test expression
This can also be expressed in the alternative format as
[expression ]
The [ is actually the name of the command (who said anything about command
names having to be alphanumeric characters?). It still initiates execution of the same
test command, only in this format, test expects to see a closing.] at the end of the
expression. Naturally, spaces must appear after the [ and before the J.
You can rewrite the test command shown in a previous example with this alternative
format as shown:
$[-z"Snonnullvar" |
$ echo $?
1
Naresh Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 97 of 208
Communication Commands
The following commands are known as “communication commands”. They are write,
mesg, mail and etc. Here we are having two types of communications.
They are: :
1, On-line communication or two-way communication
2. Off-line communication or single-way communication
1. On-line communication:
On-line communication can be performed by using write and commands.
write: The write command lets you have a two-way communication with any person
who is currently logged in. write uses login name of-the recipient as argument, and
text of the message from the standard input,
If Stanley is logged in, then he will see the message, and he has the option of replying
and if he wants to reply then he has to invoke the write command,
Swrite Raju
another half an hour After the above if any person wants to close his communicati
he has to press .
Note: when a user is using the above command, he has to remember one thing, 1
the terminal no. also, that is as follows
| Swrite Stanley tty03
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
|
there exists more than one person with same user name then the user must includes |UNIX Page 98 of 208,
Smesg: This command decides the willingness of a person to receive or not receive the
messages send by other persons. This can be applied in two-way and single-way
communication.
a) $mesg n: By executing the above command means:the person is not ready to
receive messages. :
b) $mesg y: This means, the person is ready to receive the messages.
2) Off-line communication
mail: Command is used for this type of communication, At this itis not necessary that
the receiver must be logged in once you received the mail you, can do the following.
View it on your terminal save it in a mail box save it in a file Delete it reply to it
forward it to others. §
$mail Stanley
Hai we are going. to Thailand to celebrate the launching of our company’s new
branch, are you ready to come .or press to terminate mail.
The sent message doesn't directly appear on ‘Stanley's terminal, if he is running
another program at this moment. When he is free then system display the message
like iz
you have anew mail
To view the mail messages you have to execute the mail command without any
options.
$mail: Then mail list the all messages with the names of respective sender names and
prompts you with the symbol & Here Stanley has to enter his choice, whether he may
press enter key or a number to view that particular message and so on.
mail command options:
& to view next message.
& to view previous message.
&N to the Nth numbered message.
&aN deletes Nth message
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 99 of 208
&uN undeletes the Nth message.
&s filename saves the current message in the specified file,
&muser-name forwards the message to the specified user.
aN replies to the sender of message N.
&q quits from mail. .
&S Displays the last message
&= It prints current message number
&. Displays the current message
&u Undelete last deleted message
&u 123 Undelete specified messages
&v ©. permits editing of current message with vi
&E
&l
wall: Itis used for the
Swall
Good Morning tou All
ctr+d
it is used to send mails
Smail:
$mail
Subject:
Type the mailUNIX Page 100 of 208
$mail
Subject:
Type the mail
ctrl+d
& p> Itis used to print out
& d> Itis used to delete the current opened mail
& 45 Itis used to delete the 5% mail
& 15> Itis used to delete from mail 1-5 mails
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 101 of 208
Networking Commands:
TELNET and FTP are Application Level Internet protocols. The TELNET and FTP
protocol specifications have been implemented by many different sources, including
ay other public
The National Center for Supercomputer Applications (NCSA), and
domain and shareware sources. ee
The programs implementing the TELNET protocol are usually called telnet, but not
always. Some notable exceptions are n3270, WinQVT, and QWS3270, which are also
TELNET protocol implementations. TELNET is used for remote login to other
computers on the Internet. -
The programs implementing the FIP protocol are usually ftp, but there are
t too. A program called Fetch, distributed by Dartmouth CollExe,
r Junod, and: Ftptool, written by a Mike
we often included in TELNET
buted by NCSA. FTP is used for
transferring files between computers on the Internet.
login is a remote login service that was at one tithe exclusive to Berkeley 43 BSD
UNIX. Essentially, it offers the same functionality as
remote computer information about the user's login environment. Machines can be
et, except that it passes to the
P
configured to allow connections from trusted hosts without prompting for the users’
passwords. A more secure version of this protocol is the Secure SHell, SSH
From a UNIX prompt, these programs are invoked by typing the command (program
name) and the (Internet) name of the remote machine to which to connect. You can
also specify various options, as allowed, for these commands.
Syntax :
telnet [options] [ xemote_host [ port_number } ]
£n3270 [options] [ remote_host [ port_number ] ]
Naresh j Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 102 of 208,
ftp [options] [ remote_host ]
Common Options
ftp telnet Action
-d set debugging mode on
-d same as above (SVR4 only)
-i tum off interactive prompting
FTP > File Transfer Protocol
1). FIP > > It is used to copy data from oné server to another
2) FIP > Is > It displays server side files
3) FTP > Ms > It displays client side files
4) FTP> pwd Silt
isplays server password
used to download multiple files
upload file
4). FIP > mput NiT NeO file3 > To upload multiple files
5). FIP >? It display all FTP commands
6) FIP > bye > It quit from FTP
7). FTP > get file * > to download all files
$finger - get information about users
finger displays the .plan file of a specific user, or reports who is logged into a specific
machine. The user must allow general read permission on the -plan file.
Syntax: finger [options] [user[@hostname]]
Common Options
force long output format
-m match username only, not first or last names
$ force short output format
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 103 of 208
Example: $finger
Ei find files
The find command will recursively search the indicated directory tree to find files
matching a type or pattern you specify. find can then [Link] files or execute arbitrary
commands based on the results. ;
Syntax: $find directory [search options] [actions]
Common Options
For the time search options the notation in days; n is:
+n more than n days a
nexactly n days
-nless than n days
-newer filename true if newer than filename
-type type of file, where type can be:
bblock special file
¢ character special file
d directory
Lsymbolic link
p named pipe (FIFO)
fregular file
Example: $find -NiT NeO_file
The first argument is the name of the directory in which the search starts. In the case
of current directory (represented by the.). The second part of the command specifies
the file or files to search for.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 104 of 208
Running find in the background:
We can run find command to search file in the background also. To run a command in
the background, you end it with an ampersand (&).
Example: $find /-name NiT _file-print >found_NeO&
The advantage of the running command in the backgibiinid is that you can go on to
rnin other commands without waiting for the background job to finish.
‘Terminal Type
Most systems are set up so the user is by default prompted for a terminal type, which
should be set to match the terminal in use before proceeding. Most computers work if
you choose "vt100". Users connecting using a Sun workstation may want to use "sun;
'xterms" or "xterm",
(On some systems, e.g. MAGNUS, it's also necessary to type "unsetenv TERMCAP")
-or- :
TERM=; export TERM - if using the Bourne shell (see Chapter 4.)
where is the terminal type, such as vt100,that you would like set.
whe
jort program locations
whéreis reports the filenames of source, binary, and manual page files associated with
command(s).
Syntax: $whereis [options] command(s)
Common Options
-b report binary files only
-m report manual sections only
5 >port source files only
$ihich - report the command found
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 105 of 208
$which will report the name of the file that is be executed when the command is
invoked. This will be the full path name or the alias that's found first in your path.
Syntax $which command(s) ;
Example: $ which mail
$script - record your screen /O
Sscript creates a script of your session input and output. Using the script command,
you can capture all the data transmission from and to vour terminal screen until you
exit the script program. This can be useful during the programming-and-debugging
process, to document the combination of things you have tried, or to get a printed
copy of it all for later perusal.
[file] <. . > exit
-a append the outputto file
Typescript is the name of the default file used by:sctipt.
You must re 1 to type exit to end session and close your typescript
file.
Naresh # Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX, Page 106 of 208
Backup Utiliti
Every file, every database, every byte of information stores on the system, If some
hardware failure and any system crash occurred then all the data which is present on
the system will be lost. To overcome this by the following backup utilities.
1. $tar: Stands for tape archieve, this command is uséd to create archieve onto the
tapes and the floppies. This command will create art archieve in the media
The common options are:-
Copy
-x extract
-t table of contents
Syntax: Star [options] _
Example: $tar -cv!
Example: Star -xvf,
Scpio: Stands for copy input output. This utility copies files to and from backup
device. It uses standard input to take the list of file names. The [Link] can't
use without the help of pipes and redirection filters.
Syntax: command | cpio [options]
Example: $ls | cpio -oc > /dev
-oc --> Run in copy outmode.
History
The C shell, the KORN shell and some other more advanced shells, retain information
about the former commands you've executed in the shell. How history is done will
depend on the shell used. Here we'll describe the C shell history features. You can use
the history and savehist variables to set the number of previously executed
commands to keep track of in this shell and how many to retain between logins,
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad: Ph:040 23746666, 23734842UNIX Page 107 of 208
respectively. You could put a line such as the following in .cshre to save the last 100
commands in this shell and the last 50 through the next login.
set history=100 savehist=50
‘The shell keeps track of the history list and saves it in “/-history between logins.
You can use the built-in history command to recall previous commands, eg. to print
the last 10: -
$history 10
You can repeat the last command by typing!!:
$i!
You can repeat any numbered command by prefacing the nu
scape, or quote it first, with a \
before the character, i.e. \!. The history substitution characters are summarized in the
following table.
C Shell History Substitution _
Command |
__tepeat command number n_
“repeat command n fro
‘repeat command that
‘repeat command with str anywhere on the line
lect the first argument that had strinit _
epeat the last command, generally used with a modifier
‘select the nth argument from the last command (n-0 is the
command name)Page 108 of 208
ie
select the last argument from the last command |
‘select all arguments to the previous command |
select the nth through last arguments from the previous
command
t s/stri/str2 substitute srl with str2 in its first occurrence in the ath
1 _.... command, ending with a g substitute globally
Identity
The system identifies you by-the user and group numbers (userid and groupid,
respectively) assigned to you by your system administrator. ‘Yow don't normally need
to know your-userid or groupid as the system translates username « userid, and
‘oupid automatically. You probably already know your username; it's
the name you logon with; The group name is not as obvious, and indeed, you may
yup is the one associated with your
up by your system administrator.
Similarly, there is'a group database file where the system administrator can assign:
you rights to additional groups on the system.
In the examples below % is your shell prompt; you don't type this in.
You can determine your userid and the list of groups:you belong to with the id
groups commands. On some systems id displays your user and primary grou]
information, e.g.:
$id
uid=1101 (frank) gid=10(staff)
on other systems it also displays information for any additional groups you belong t
Sid i
uid=1101 (frank) gid=10(staff) groups=10(staff),5(operator),14(sysadmin),110(uts)
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842. /UNK Page 109 of 208
‘The groups command displays the group information for all the groups you belong
to, e.
Sgroups
staff sysadmin [Link]
The ulimit Command:
It stands for user limit, though most files in UNIX occupy tens of blocks, in some odd
case a program my go away add create files which occupy huge amount of disk space.
Some times things might take such.a bad turn that the file might occupy several
megabytes of disk space and ultimately harm for the file system. To avoid creation of
such files UNIX uses a variable called ulimit.
2097152
This implies
Sulimit 1
Hence onwards no file can be created whose size is bigger than 512 bytes. Thus this
change will be effectively only for the current session and the system will return to its
default value when you log out.
An ordinary user can only reduce the ulimit value andis never permitted to increase
it. A super user is an exception to the rule and can increase or decrease this value.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23740666, 23734842UNIX Page 110 of 208
One of the most important contributions UNIX has made to Operating Systems is the
provision of many utilities for doing common tasks or obtaining desired information.
Another is the standard way in which data is [Link] transmitted in UNIX
systems, This allows data to be transmitted to a file, the téihinal screen, or a program,
or froma file, the keyboard, or a program; always in a uniform manner.
The standardized handling of data supports two important features of UNIX utilities:
I/O redirection and piping. With output redirection, the output of a command is
redirected to a file rather than to the terminal screen. With input redirection, the input
to a command is given via a file rather than the keyboard. Other tricks are possible
with input and output redirection as well, as you will see. With piping, the output of a
execution is from right to left. The output of extreme right command is becomes input
to its left side command. i
2. If there exists > or | then the command execution-is from left to right. Here the
output of left command is an input to its right command.
File Descriptors
There are 3 standard file descriptors:
> stdin 0 Standard input to the program
> stdout 1 Standard output from the program
» stderr 2 Standard error output from the program
Stream Device Value
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 111 of 208
Standard Input Keyboard 0
Standard Output Terminal Screen 1
Standard Error Terminal Screen 2
Normally input is from the keyboard or a file. Output, both stdout and stderr,
normally go to the terminal, but vou can redirect one or both of these’to one or more
files. You can also specify additional file descriptors, designating them by a number 3
through 9, and redirect I/O through them.
File Redire ,
Output redirection takes the output of a command and [Link] a named file.
Input redirection reads the file as input to the command, ‘Thé following table
summarizes the'redirection options.
File Redirection.
Symbol
>
"same as above, but overrides noclobber option on csh and,
creates the file if it doesn't already exist.
uriarilaa
oe“ Tread from standard input until "String" is encountered as the
[< fiie3
The above command concatenates NiT then NeO and redirects (sends) the output to
file3. If file3 doesn't already exist it is created. If it does exist it will either be truncated
to zero length before the new contents are inserted, or the command will be rejected, if
the noclobber option of the csh is set. (See the csh in Chapter 4). The original files, NiT
and NeO, remain intact as separate entities.
Naresh i Technologies. [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
nt re a saeUNIX Page 112 of 208
, Pipes and Filters
synhol 7
Pipes: If an operator is useful to redirect the output of a command to a file or to any
command is known as a pipe. There exist 2 pipe operators.
1. > Ex:$ who > userlist
In this example, who command has to display the list of users, but here the output of
who command is stored in the file userlist. In this commiand some disadvantages are
there. -
2. | (pipe) Ex: $ who | sort > sortwho this command display the number of
logged in users. Here the output of who command (standard output) is given to
we -1 command (stai i .
By default it will display 10 lines from the BOF
Syntax: $ head [options] file
Common Options
-n number of lines to display, counting from the top of the file
-tumber same as above
Ex: $ head NiT Display first 10 lines from the file NiT.
Ex:$ head -3 NiT Display first 3 lines from NiT.
Ex$head-I5NiT Display 1 to 15 lines from the BOF of the file NiT.
$tail: It will display the output from the EOF. By default it will display the last 10
lines.
Syntax: $ tail [options] file
Common Options
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 113 of 208
-number number of lines to display, counting from the bottom of the file
Ex: $tail NiT Display last 10 lines from the file NiT.
Ex: Stail 5 NiT Display last 5 lines of NiT.
Ex: Stail -25 NiT Display last 25 lines of NiT.
Ex: Stail +20 NiT Displays all line from 21 to all Tin
more, less, and pg - page through a file
UNIX provides three commands which offer more flexibility in-viewing liles. l'hese
are the above. » -
more, less, and pg let you page through-the contents of a [Link] screen at a time.
These may not all be available on. your UNIX system. They. you to back up
rds, etc.
Example: $pg -c NiT —~
Options
more less pg Action
--c-c clear display before displaying
-iignore case
-w default don't exit at end of input, but prompt and wait
-lines -lines # of lines/screen
+/pattern + / pattern + /pattern search for the pattern
1). It sets the number of lines to be displayed per page.
2). Ability to move either forwards or backwards in a file just at the touch of a keys
like f and b.
Naresh i Technologies, [Link] Theatre, Ameérpet, Hyderabad Ph:040 23746666/23734842UNIX Page 114 of 208
3). Skip pages while viewing the file page by page.
4). Search the file for a pattern in forward or backward direction. ‘
5). If you hit the key spacebar it displays page wise and enter line by line.
6). To quit pg or more or less, hit the keys q or Q or BREAK or DELETE
Taking printout
UNIX permits sharing of one printer amongst all users or several printers amongst
several users. The lp command is used to send the user's print job the print queue.
Syntax: $1p foptions] [filename (s)]} —
Examples: =
Sip NiT
Sip NiT NeO
SIp -w NiT
. -q level > Sets a priority level for the print job
Once''the print request has been made using thé Ip command we can watch its
progress using the Ipstat command which shows the status of our print job. There are
various options that can be used with the Ipstat command. Out of these most
comprehensive information is obtained using the -t option.
Sipstat -t
Scheduler is running
System default destination: Ip
Device for Ip: dev/Ip0, Ip is accepting requests since Wednesday August 6 [Link]
2009
Ip-I3NiT 198 Apr2312:10
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 115 of 208
Ip-1root 188 Mar03 12.28 i :
Ip-M4NiT 291 Feb23 18:11 }
Ip-15NiT 98 — Apr23 16:13
Scancel: Some times submitted the job for printing we decide to cancel it, we can do so
using cancel command... —
$cancel [options] [jobid]
Example: cancel Ip -13 . .
Where Ip-13 is the request id-of our print job. The cancel command responds with,
request, “Ip-13,190 canceled a
Cut It is also filter. This command displays the specified positional characters. It must
be associated with -c option and we can use, and - operators to specify position and
$cat > NiT
StdNo _StdName Course Grade
1001 RamKumar Net D
1002 AjitPanday UNIX. - 67 D
1003, Kavleen PHP 89 B
1004 Subin JAVA 92 A
1005 Raju Oracle 76 D
1006 Priya QA 98 A
Ex $cut -c2, 5 NiT
It will display second and fifth characters trom each line of the file NiT.
Ex: $cut -c2-5 NiT
It will display second, third and fifth characters from the file NiT
Naresh i Technologies, Opp-Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 116 of 208
Ex: $cut -£14 NeO
Itdisplays 1 and 4 fields from NeO file
Ex $cut -f1-4 NeO
Itdisplays all fields between 1 to 4 from NeO file
Sort: This command is tised to sort the file contents accotding to-alphabetical order or
- numerical order and display them. The sort command is used to order the lines of a
"file. Various options can be used to choose the order as well as the field on which a file
is sorted. Without any options, the’sort compares entire lines in the file and outputs
them in ASCH order (numbers first, upper case létters, ‘then [Link] letters).
Syntax: $sort [options] [+pos1 [-pos2']] file
Common Options
-b ignore leading blanks (<
hen determining starting and ending
-n numeric sort :
-m Merge files that have been already sorted
-0 outfile output file
-r reverse the sort
-t char use char as the field separator character
-w unique; omit multiple copies of the:same line (after the sort)
Ex$ sort Raju
Ex: $ sort NiT NeO NiT1
Ex: $ sort -r Raju
Ex: §$sort -oresult NiT NeO NiT1
Ex $sort -u -o result NiT NeO NiT1
Ex $sort -m NiT NeO
Naresh # Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 117 of 208
Ex: §sort-n KiT
Ex §sort -nr KiT
This command can reverse sort the contents and display.
Paste: This command display the combined data of thi
iespecified two files. The paste
command allows two files to be combined side-by-side: The default delimiter between
the columns in a paste is a tab, but options allow other delimiters to be used.
Syntax: $paste [options] filel file2
Common Options :
-d list list of delimiting characters ~
-s concatenate lines
Itmay be necessary to quote delimiters with special meaning to the shell.
A hyphen (-) in place of a file name is used to indicate that field should come from
standard input.
Ex: $ paste NiT NeO
Debugging
Debugging is the process of removing bugs. In Unix Operating System type the
following statement: 0
$set -vx
Here, v ensures that each line in the shell script is displayed before it gets executed,
and x ensures that the command along with the argument values that it may have is
also displayed before execution.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842,
ualUNIX Page 118 of 208
note that by setting the -v option the line from the script which is about to get
executed is displayed, where the lines which are preceded by the '+" sign, have come’
courtesy the -x option
We can unset the debugging options that may have beert'set ty the fSllewine:
$set +x!
Built-in Integer Arithmetic
The POSIX standard shell provides a mechanism for performing integer arithmetic on
shell variables called arithmetic expansion. Note that some older shells do not support
this feature.
The format for arithmetic expansion is
$((expression
where expressi
Valid shell var values (leading and trailing
whitespace is a the C programming language
and are listed
the command line, For example,
echo $((i#1))
adds one to the value in the shell variable i
variable i doesn't have to be preceded by a dolla
that the only valid items that can appear in arithmetic expansions are operators,
numbers, and variables. If the variable is not defin contains a NULL string, its
value is assumed to be zero. So if we have not assigned any value yet to the variable a,
we can still use it in an integer expression: *e
prints the result. Notice that the
|: That's because the shell knows
$ echo Sa Variable a not set
s
$ echo $((a=a+1)) Equivalent toa=0+1
1
S echo $a
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad . Ph:040 23746666, 23734842UNIX . Page 119 of 208
1 Now a contains 1
Note that assignment is a valid operator, and the. value of the assignment is
substituted in the second echo command in the pre example::Parentheses may
be used freely inside expressions to force grouping, asin” =
echo $((i= (i+ 10) *}))
If you want to perform an assignment without echo or some other command, you can
move the assignment before'the arithmetic exparision. So to multiply the variable i by
5 and assign the result back to [Link]
i=$((i*5))
That concludes our introduction to writing commands and using variables.
i
$tr: This tr reads input and either deletes’ target characters or translates each target
character into a specified replacement character. The output is a translated version of
the input. The tr command translates characters from stdin to stdout.
Syntax: tr [options] string! [string2]
With no options the characters in string1 are translated into the characters in string2,
character by character in the string arrays. The first character in string! is translated
into the first character in string2, etc.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 120 of 208
A range of characters in a string is specified with a hyphen between the upper and
lower characters of the range, e.g. to specify all lower case alphabetic characters use
faz]
Repeated characters in string2 can be represented with,
'fen}' notation, where
be as large as needed
character x is repeated n times. If n is 0 or absent it is asstimed to:
to match string1.
Characters can include \octal (BSD and SVR4) and \chatacter (SVR4 only) notation.
Here "octal" is replaced by the one, two, or three octal integer sequences encoding the
ASCII character and "character" can be one of: ~
b back space
£ form feed
nnew line
x carriage re
ttab
in the string field where class can
take on character classification values, including:
alpha alphabetic characters
lower lower case alphabetic characters
upper upper case alphabetic characters
Common Options
-c complement the character set in string!
-d delete the characters in string]
-s squeeze a string of repeated characters in string] to a single character
a) Ex$tr a T < NiT This command display the translated version of the
file NiT. That is each instance of a replaces with T.
) Ex:$ tr ‘abe’ ‘xyz’ < NiT This command replaces all instances of a with x, b
with y, and ¢ with z respectively and then display the resultant data.
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
sUNK ; : Page 121 of 208
¢) Ex: $ tr -d ‘abe’ < NiT This command deletes all instances of a, b and c in the
file NiT and display the result. ‘
$nk:.If any of the file you want to see with the line numbers then we can use In
command to accomplish’ this task. Sdnte files are very"large files if we want to see
particular line in that file then it will become very diffictlf for us to see'that lane,
Syntax: $nl [options] [filename] :
Example: $nl NiT
It displays all lines with line numbers in the file NiT file.
$tee: tee utility sends what it reads from its input in two ions: one to a file
named as an argument and to the standard output(monitor),.tee sends standard in to
specified files and also to standard out. It's often used in commiand pipelines,
Syntax: tee [options] [file[s] :
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 122 of 208
Sed (Stream Editor) & GREP
Stream editor for filtering and transforming text from standard input to standard
output. Sed is a streamlined, noninteractive editor. It allows you to perform the same
kind of editing tasks used in the vi and ex editors. Iistead of working interactively
with the editor, the sed program lets you type your edititig. commands at the
command line, name the file, and then see the output of the editing command on the
screen.
Options: es
Name ‘ Purpose
q ‘Quits affer the specified line
P Prints the specified line
n
d
a
i
“ ,
Ba
/pattern/ Search for the specified pi
“ Represents Beginning of the lines
s/strl/str2/ Substitutes str2 at str1 “s
Here are some examples for sed command
1.$ sed ‘Sq’ NiT Display first 5 lines.
2.$ sed ‘2p’ NiT
Display all lines, but 24 line will be displayed twice.
3. $ sed -n'2p’ NiT
Display only 2 line.
4. $ sed ‘3d’ NiT
Naresh i Technologies. [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842P_uNIX Page 123 of 208
Display all lines except 3+ line.
5.$sed -n-e 2p’ -e9p’ NiT
Display 2» and 3 lines.
6.$ sed -n’/Raju/p’ NiT -
Display all lines that contain the pattern Raju. 2
7.$ sed -n‘/Raju/w NiT1’ NiT
This command searches the specified [Link] lines and save those lines’
into the file NiT1.
8,$ sed -n‘/*Raju/p! NiT
Itwill display the lines that are beginning with the pattern Raj
9.$ sed‘
GREP Family:
Sgrep (Globally search for Regular Expression) is one of the most popular and useful
UNIX filters. It scans a file for the occurrence of a pattern, and can display the selected
pattern, the line numbers in which they were found, or the filenames where the
pattern occurs. grep can also select lines not containing the pattern.
Syntax: $ grep options pattern filename(s)
1. searching fora single pattern
Ex: $ grep sales NiT: This command displays all lines which contain the pattern sales
from the file NiT.
a) If the pattern was not found, then it will display nothing.
2. If you want to search more than one pattern, then include the patterns in single
quotes. Ex: $ grep ‘Net’ NiT
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 124 of 208
Options: -c counting occurrences
-n displaying line numbers
-v deleting lines
-I displaying file names
-i ignoring case sensitivity
-n displays line numbers of text .
$grep command can also allowed the wild card characters, but pattern: must be
included with in double quotes. Below see the Examples.
1. Sgrep hello NiT> It finds hello string in Nit File.
it specified all files
gs which are starting with, capital or
srs of Unix string where existed
Unix string found.
are matching call cell etc..
9. $grep “R..u” NiT > It should match R andi with this two character should be
there.
10.$grep “\” NiT ~ It will search only the word Unix
Anchors: :
A> Start of the line
$> End of the line .
11.$grep “*U” NiT > It prints all lines which, are starting with ‘U’
12.$grep “AUnix” NiT > It prints all lines, which are starting with Unix string
13.$grep “x$” NiT > It prints all lines which are ending with ‘x’
14.$grep “*\” NiT > It prints all lines which are starting which UNIX
string.
Naresh # Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 125 of 208
15.$grep “[0-9]$” NiT > It prints all lines which are ending with digits
16.$grep ”[abc]” NiT > It prints all lines which are starting with a b and c
17.$grep ”*[Sabc]” NiT > It prints all lines which are not starting with a b and ¢
18.$grep “A$” NiT.> It displays all blank lines fron NiT file
19.$grep -c “4$” NiT > It displays all blank lines riumbers
20.$grep “A...$” NiT > It displays the line only having three characters
$fgrep command: :
The fgrep command is similar to grep, but with three main differences; you can use it
to search for several targets at once, it doesn’t allow you to use regular expressions to
search for patterns, and is faster than grep
Example: $fgrep.“Shell
ava and QTP from NeO and NiT files
a ul member of the grep command family. You
can use it like fgrep to search for mi iple targets Like, grep, it allows you to use
ides, more powerful set regular
regular expressions to specify targets, but it pro
expressions than grep.
The egrep command accepts all of the basic regular expressions recognized by grep.
Example:
Segrep “Unix|Java| QTP” NiT
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 126 of 208
Exporting Variables
By default, any variable is a available only in the shell in which it is defined.
Example:
$a=30
$sh
$echo
$a$exit
$echo $a _
30 : e
After defining a, we invoked a sub shell. This is achieved by saying sh at the prompt.
Sexport a
$sh
$echo $a
30
Example:
$export abe
a=100 b=200 c=300
is same as
a=100 b=200 c=300
$export abc
Naresh f Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX i Page 127 of 208.
UNIX is we know is a multi-user, multi -tasking operating system. If we want to see
which processes are running at any instant type the following
Common Options
-a-eall processes, all users
-e environment/everything
-§ process group leaders as well
-I-Llong format am
“uu user user oriented report” © =
-X-@ even processes not executed from terminals
memory: This number is called
\d run upto a maximum of 32767.
When the maximum numbe counting all over again from 0
onwards.
If we want to find out which processes are running for the other users who have
logged in execute the ps command with the -a optioi: -a standing for processes of all
the users.
Sps-a
IE we want to see what a particular user is doing just say the following
$ps -u userl where u stands for user and user! for his login name.
Another useful option available with ps is -t. It lets you find out the processes that
have been launched from a particular terminal.
$ ps-t tty3d
Additional information that UNIX stores about each running processes. This
information can be obtained by using the option -f stands for full listing.
Naresh i Technologies. Opp. Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666,23734842UNIX Page 128 of 208
$ps-f
Still More Processes:
So far we have encountered only the processes associated with individual users. But
there is several more running in memory which are necessary for the system to work.
$ps-e here -e stands for every process running at that instant.
Background Processes: .
Most system processes run in the background, while the-users execute their processes
in the foreground. To run a process in the background, UNIX provides the ampersand
(&) symbol. While executing a command, if the symbol is placed at the end of the
command then the command will be:executed in the backgroi When you run a
. This is PID of the
process in the background 4 number is displayed on the
2). The output of a background process should always'be redirected to a file.
3). With too many processes running in the background the overall system
performance is likely to dExrade.
4), If you logout while some of your processes are running in the background all these
processes would be abandoned halfway through.
Snohup:
The nohup command:
If we are to ensure that the processes that we have executed should not die even when
we log out, the nohup command is the answer. Using this command we can submit
‘Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 129 of 208
the time consuming commands in the background, log out and leave the terminal and
come next day to see our output ready. nohup stands for no hang up.
$nohup sort [Link]>[Link]
17695
now we can safely log out without our processes getting té on logging out
Note: If we do not redirect the output of our background process the command acts
intelligently and stores output in the file '[Link]'.
$nohup sort [Link] -
16779 a
sending output to [Link]
6173 terminated how the kill command works
at such times we can employ signal number 9, the ‘suré:kill signal to forcibly terminate a
process as follow. :
$kill -9 6173
Scheduling of processes:
cron stands for chronograph. During booting UNIX executes this file and displays the
message ‘cron n started’ on the host terminal. Once UNIX launches this process there
onwards cron is activated once every minute. When cron wakes up it ehecks whether
any scheduled job is available for it to execute.
Job scheduling
Sat
Naresh i Technologies, [Link] Theatre, Ameerpet; Hyderabad Ph:040 23746666, 23734842UNIX Page 130 of 208
$batch
The at command:
This command is capable of executing UNIX commands at a future date and time.
$ at 17:00
echo "It's 5 PM ! Backup your files and logout"
ctri+d
then it gives job-id and the date and time when it will be executed.
job id always terminates with .2 indicating that this job was submitted using the at command.
There are two options available with the at command which permit to view the list of
this queue. The
jobs submitted using at and to remove any unwanted jobs fi
options are -1 for listing jobs and -r for removing jobs.. While removing a submitted
$at 9:15 am Mar 24
$at now+10minutes
Sat now+1day
‘The Batch Command:
Instead of we specifying that our commands be executed/at a precise moment in time
sometimes we may let the system decide the best time for executing our commands.
The way to achieve this is through a command called batch. When we submit our jobs
using this command, UNIX executes our job when it is relatively free and the system
load is light.
$batch sort [Link]>[Link]
ctrl+d -
Once again note that the ‘.b' extension given to our job-id signifies that has been
submitted using the batch command.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 131 of 208
The Null Command ;
This seems about as good a time as any to talk about the shell’s builtin null
command. The format ofthis command is simply
and the purpose of it is—you guessed it—to do nothing, So:what-good is it? Well, in
most cases it's used to satisfy the requirement that a command appear; particularly in
if commands. Suppose that you want to make sure that the value stored in the
variable system exists in the file /users/steve/mail/systems, and if it doesn't, you
want to issue an error message and exit from the program. So’you start by writing
something like
if grep "$system" /users/steve/mail/systems > /dev/null
n :
but you don't know what to write after the then becatise you want to test for the
echo "Ssystem is not a valid system"
exit]
fi
So if the system is valid, nothing is done. If it's not validyithe error message is issued
and the program exited.
Remember this simple command when these types of situations arise.
The $$ Variable and Temporary Files
If two or more people on your system use the rolo program at the same time, a
potential problem may occur. Look at the rem program and see whether you can spot
it. The problem occurs with the temporary file /tmp/phonebook that is uséd to create
a new version of the phone book file.
grep -v "fname" phonebook > /tmp/ phonebook
Naresh i Technologies, Opp-Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842_UNIX Page 132 of 208
my /tmp/phonebook phonebook
If more than one person uses rolo to remove an entry at the same time, there's a
‘chance that the phone book file can get messed up because the same temporary file
will be used by all rolo users.{1] Naturally, the chances of this happening (that is, the
preceding two commands being executed at the same't aore:than one user) are
rather small, but, nevertheless there still is that ol ‘Anyway, it brings up an
important point when dealing with temporary files in general,
{1} Actually, it depends on the users' default file creation ‘mask (known as umask). If
one person has created /tmp/phonebook and it's not-writable by anyone else, the
next person who comes along and tries to create it will'get an error message from the
shell. The net result is that the first
$ echo $$
4668
$ ps
PID TTY TIME COMMAND
4668 co 0:09 sh
6470 co 0:03 ps
os
As you can see, $$ is equal to the process id number of your login shell. Because each
process on the Unix system is given a unique process id number, using the value of $$
{in the name of a file minimizes the possibility of another process using the same file.
So you can replace the two lines from rem with these
grep -v "$name" phonebook > /tmp/phonebook$$
Naresh i Technologies, Qpp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX : “Page 133 of 208
mv /tmp/phonebook$$ phonebook
to circumvent any potential problems. Each person running rolo will run it as a
different process, so the temporary file used in each case will be different.
The && and | | Constructs
The shell has two special constructs that enable you to execute a cotimand based on
whether the preceding command succeeds or fails. In case you think this sounds
similar to the if command, well it is. It's sort of a shorthand form of the if.
If you write : —=
commandl && command2 8 ye *
anywhere where the shell expects'to see'a:command, comman
if it returns aréexit status of zero, command2 will be executed
be executed, and
For example, i
sort bigdata >
then the mv
equivalent to
mv /tmp/sortout bigdata
The command.
[-z"SEDITOR" ] && EDITOR=/bin/ed
tests the value of the variable EDITOR. If it's null, /bin/ edhis assigned to it.
The | | construct works similarly, except that the second command gets executed only
if the exit status of the first is nonzero. So if you write
grep "Sname" phonebook | | echo "Couldn't find $ndme"
the echo command will get executed only if the grep fails (that is, if it can't find $name
in phonebook, or if it can't open the file phonebook). In this case, the equivalent if
command would look like
if grep "$name" phonebook
then
‘Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
aesUNIX Page 134 of 208
else
echo "Couldn't find $name"
fi
You can write a pipeline on either the left- or right-hand,[Link] these constructs. On
the left, the exit status tested is that of the last command in the pipeline;thus
who | grep."*$name "> /dev/null | | echo "Sname's not logged on
causes execution of the echo if the grep fails.
The && and | | can also be combined on the same command line:
who | grep **$name "> /dev/ null &&e echo *Sname's not logged on" \
| | echo "$name is logged on"
(Recall that when \is used at the end of the line, it signals
‘shell.) The [Link] gets executed if the grep succeeds; the seco
These constructs are also often used in if commands:
ecuted. The exit status from this
sendmail program is executed. If
executed, and this is used as the
won't be executed.
valisys returns a nonzero exit status,
exit status that is tested by the if. In that case, sen
The use of the && operator in the preceding [Link] like a "logical AND"; both
programs must return an exit status of zero for the sendmail program to be executed.
In fact, you could have even written the preceding if as
validsys "Ssys" && timeok && sendmail "Suser@$sys" < $message
When the | | is used in an if, the effect is like a "logical OR":
ifendofmonth | | specialrequest
then
sendreports
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 135 of 208
fi
If endofmonth returns a zero exit status, sendreports is executed; otherwise,
specialrequest is executed and if its exit status is zero, sendreports is executed. The net
effect is that sendreports is executed if endofmonth § or, specialrequest return an exit
status of zero. ane
The printf Command
Although echo is adequate for displaying simple messages, sometimes you'll want to
print formatted output: for example, lining up columns of data. Unix systems provide
the printf command, Those of you familiar with the C programming language will
notice many similarities,
The general format of the printf command is
it often contains whitespace.)
by a percent sign (%) are written
eded by a percent sign are called
conversion specifications and tell print how the responding argument should be
displayed. So, for each percent sign in the format string there should be a
corresponding argument, except for the special conversion specification %%, which
causes a single percent sign to be displayed.
Here's a simple example of printf:
$ printf "This is a number: %d\n" 10
This is a number: 10
$
printf doesn't add a newline character to its output like echo; however, printf
understands the same escape characters that echo does , so adding \n-to the end of
the format string causes the prompt to appear on the next line.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 136 of 208
Although this is a simple case that could easily be handled by echo, it helps to
illustrate how the conversion specification (%d) is interpreted by printf: When the
format string is scanned by printf, it outputs each character in the string without
modification until it sees the percent sign; then it reads the d and recognizes that the
%d should be replaced by the next argument, which must be an integer number. After
that argument (10) is sent to standard output, printf sees..the-\n-and outputs a
newline. 2
The different conversion specification characters.
printf Conversion Specification Characters
Character Use for Printing
d Integers f
°
x
x 5
4
ol -
s feral strings
b trings containing backslash escape characters
% Percent signs
The first five conversion specification characters aré all used for displaying, integers.
%ed displays signed integers, and %u displays unsigned integers; %u can also be used
to display the positive representation of a negative nutnber (note that the result is
machine dependent). By default, integers displayed as octal-or hexadecimal numbers
do not have a leading 0 or Ox, but we'll show you how to enable this later in this
section.
“is RCE seit
Strings are printed using %s or %b. %s is used to print strings literally, without any
processing of backslash escape characters; %b is used to force interpretation of the
backslash escape characters in the string argument.
Here are a few printf examples:
§ printf "The octal value for %d is %o\n" 20 20
‘Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 137 of 208,
The octal value for 20 is 24
$ printf "The hexadecimal value for %d is %x\n" 30 30
The hexadecimal value for 30 is le 7
$ printf “The unsigned value for %d is %u\n" -1000 -1000
The unsigned value for -1000 is 4294966296
$ printf "This string contains a backslash escape: %s\n""test\nstring”
This string contains a backslash escape: test\nstring
d escape: %b\n""test\nstring"
$ printf "This string contains an interp:
In the last printf, %c is used to display a single,character. If the corresponding
argument is longer than one character, only the first is dis
$ printf "Just the first character: %c\n" abc
a
$
The general format of a conversion specification is
%{flags][width][ precision|type
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX, Page 138 of 208
The type is the conversion specification character, you can see, only the percent sign
and type are required; the other parameters are called modifiers and are optional.
Valid flags are -, +, #, and the space character. - left justifies the value being printed;
this will make more sense when we discuss the width modifier. + causes printf to
precede integers with a + or - sign (by default, only negative integers are printed with
a sign). # causes printf to precede octal integers with 0 and-hexadecimal integers with
Ox or OX for %#x or %#X, respectively. The space character catases printf to precede
positive integers with a space and negative integers with a -. ‘a
$ printf "%+d\n%+d\n%+d\n" 10-10 20
$ printf "%#o %#x\n" 100 200
0144 Oxc8
$
As you can see, using + or space as the flag lines up columns of positive and negative
numbers nicely.
The width modifier is a positive number that specifies the minimum field width for
printing an argument. The argument is right justified within this field unless the - flag
is used:
$ printf "%20s%20s\n" string] string2
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842_UNX Page 139 of 208
string] string2
Be.
$ printf "%-20s%-20s\n" string! string?
string] string? '
$ printf "%5d%5d%5d\n" 110100
1 10 100
$ printf "%5d%5d%5d\n" -1 -10-100 _
-1 -10-100
$ printf "%-5d%-5d%-5d\n" 1.10 100
size for the field; if the width of an
The .precision modifier is a positive number that specifies a minimum number of
digits to be displayed for %d, %u, %0, %x, and %X. This results in zero padding on
the left of the value:
$ printf "%.5d %.4X\n" 10 27
00010 001B
$
For strings, the .precision modifier specifies the maximum number of characters to be
printed [Link] string; if the string is longer than precision characters, it is truncated
on the right:
$ printf "%.5s\n" abcdefg
abcde
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
amacitiilUNIX Page 140 of 208
$
A width can be combined with precision to specify both a field width and zero
padding (for numbers) or truncation (for strings):
$ printf ":%#10.5x:%5.4x:%5.4d\n" 1 10 100
: 0x00001: 000a: 0100
$ printf ":%9.5s:\n" abcdefg
: - abcde:
§ printf ".%-9.5s:\n" abedefg
rabcde
$
Finally, if a * ith or precision; the argument
preceding the and will be [Link] the width or
precision, respectively th, two integer arguments must
precede the value being p'
$ printf "%*s%**3\n" 12 "test
test one te
$ printf "%12s%10.2s\n" "test one" "test two"
test one te
5
As you can see, the two printfs in this example produce the same results. In the first
printf, 12 is used as the width for the first string, 10 as the width for the second string,
and 2 as the precision for the second string. In the second printf, these numbers are
specified as part of the conversion specification.
The various conversion specification modifiers.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX x Page 141 of 208
printf Conversion Specification Modifiers
Modifier Meaning,
flags
- Left justify value. Le
+ Précede integer with + or-.
(space) - Precede positive integer with space character.
# Precede octal integer with 0, hexadecimal integer with 0x or OX.
width Minimum width of field; * [Link] next argument as width.
precision Minimuny number of digits-to display for integers; maximum number of
characters to display for strings; * means use‘next argument as precision.
numbers from a file:
cat $* |
while read number] number2
do
printf "%12d w124\n" $numberl $number2
done
$ cat data
1234 7960
593 -595,
395304
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 142 of 208.
3234999
394-493,
$ align data
1234-7960
593-595
395-304
3234999
394
s
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 143 of 208
Editing Files
If you spend any time on a Unix system, you'll want to become comfortable with a
‘text editor. You may want to edit your configuration files, make quick changes to a
web page, edit a script you've created, or simply jot yourself some notes: all these
activities are easily done with a command line text editor
In the beginning, Unix had only one editor, the line-by-line editor "ed". A medieval
version of Unix contained a new program called "ex"; its most notable feature was
allowing people to work with a full screen of text by giving the command "vi". The
new display editor proved so popular that AT&T's Unix System V included vi as a
separate program. -
Since then, vi has remained as a standard command line editor, but two others have
emerged: emacs, a full-featured text-editor, and the easy [Link] pico. This document
provides a brief introduction to each and references for how to learn more.
EDITORS
led
Ed: editor
an make any changes at line level only. We
icter position. So this is having some limited features
by default it is i
for the Unix operating system. It was one of the
system and has been standard in Unix-based sy:
written by Ken Thompson and contains one of the first implementations of regular
can’t move the cursor to a
only. When we open this e command mode. ed is a line editor
end-user programs hosted on the
ever since. ed was originally
expressions. Prior to that implementation, the concept of regular expressions was only
formalized in a mathematical paper, which Ken Thompson had read. ed was
influenced by an earlier editor known as QED from University of California at
Berkeley, Ken Thompson's alma mater. ed went on to influence ex.
Exeditor
ex, short for Extended, is a line editor for Unix systems. The original ex was an
advanced version of the standard UNIX editor ed, included in the Berkeley Software
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 144 of 208
Distribution. ex is similar to ed, with the exception that some switches and options are
modified so that they are more user-friendly.
pmeraiee i hoie
Switches
ex recognizes the following switches:
- (obsolete) suppresses user-interactive feedback
-s (XPG4 only) suppresses user-interactive feedback
-Lsets lisp editor option
-r recover specified files after a system crash
-R sets read-only -
-t tag Edit the file containing the specified tag
-v invoke viswalimode (vi)
-w set window
-x set encryption mode.
-C encryption option —
file specifies file tobe edited
vi editor
This editor is known as visual editor. In some of the Linux versions it can also be
called as vim (visual Improved) editor. The other names of this are screen editor or
character editor. This editor is most user-friendly editor than all others. In this editor
we are having 3 modes. They are
Command mode,
Insert mode
Ex command mode
Here Command mode is the default mode when we open the vi editor. In this mode
we can execute vi commands. Append or Insert mode is used to append or insert the
data. Escape mode is used to come to the command mode from the insert or append
mode, for this press Esc key.
Command mode commands
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX . Page 145 of 208
1. Entering in to vi.
a) vi without opening a file.
b) vifilename Opening a file using vi.
2. Basic cursor movement commands.
InUNIXh j k 1 _— | —
Moving the Cursor
the symbol “ before a letter means that the key should be held down while the
letter key is pressed. =
* jor
for down-arrow] move cursor down‘one line
* k [or up-arrow], move cursor up one line
[or right-arrow] move cursoright one character
* 0 (zero) move cursor to start of current line (the one with the cursor)
* § move cursor to end of current line
w move cursor to beginning of next word
b move cursor back to beginning of preceding word:
:0 or 1G move cursor to first line in file
:n or nG move cursor to linen
:$ or G move cursor to last lirie in file
(Respectively) 2h
O(zero) go to beginning of the line.
$ go to end of the line.
vi filename edit filename starting at line 1
vi -1 filename recover filename that was being edited when system crashed.
Screen Manipulation
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX Page 146 of 208
The following commands allow the vi editor screen (or window) to move up or down
several lines and to be refreshed.
Af move forward one screen
Ab move backward one screen
Ad move down (forward) one half screert
Au move up (back) one half screen’
A] redraws the screen
Ar redraws the screen, removing deleted:lines
Adding and Deleting Text ~
Unlike PC editors, you cannot replace o or delete text by hight fing it with the mouse.
The following commands allow you to insert and add text. Each of these commands
puts the vi editor into insert mode; thus, the hit
I insert text at beginning of current line, until hit
* a append text after cursor, until hit
A append text to end of current line, until hit
* o open and put text in a new line below current line, until hit
* O open and put text in a new line above current line, until hit
Changing Text
The following commands allow you to modify text.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842_UINKK Bage 147 af 308
* ¢ replace single character under curser (ne needed) :
R replace characters, starting with current curser position, until hit
ew change the current word with new text,
starting with the character under cursor, until hit
eNw change N words beginning with character under euiser, until hit
e€ change (zeplaee) the entire eurrentline, stepping when is hit
Nee er eNe change (replace) the next N ines, starting with thes quent line,
Stopping’ when is hit
Nx delete Neatacens ata nig with cha
dw delete the single word beginning with charaeter under euESeF
dNw delete N words beginning with | havacter tinder €UESBE)
eg, dow deletes § words :
BD delete the remainder of the line, starting wien curser pasitien
* dd delete entire current line
Ndd or dNd delete N lines, beginning with the eurrentling;
e.g, Sdd deletes 5 lines
Cult Pasting Ws
The fellewing commands allaw yeu te copy and paste text:
vy copy (yank, cut) the current line into the buffer =
Nyy of ¥Ny_ copy (Vank, cut) the next N lines, ineluding the current ling, inte the
buffer
P put (paste) the line(s) in the buffer inte the text after the eurtent line
fav ing Bi
Naresh / Techaslegies; Opp-Satyam Theatre; Ameerpet, Hyderabad Bh:040 23746666, 23734843UNIX Page 148 of 208
These commands permit you to input and output files other than the named file with
which you are currently working.
xx filename read file named filename and insert after current line
(the line with cursor)
:w write current contents to file named in otiginal vicall
:w newfile write current contents to a new file named newfile
:12,35w smallfile write the contents of the lines numbered 12 through 35 to
anew file named smallfile -
:w! prevfile write current contents over a pre-existing file named prevfile
To Exit vi 4
* :x quit vi, writing out modified file to file named in original invocation
8. Control commands. ©
iw Save changes:in same file
twq Save and quit.
iw filename Save changes in given file
iq quit without saving.
num Go to line num
Naresh i Technologies, Opp Satyam Theatre. Ameerpet, Hyderabad Ph:040 23746666, 23734842,UNIX Page 149 of 208
Shell Scripting
Shell Script file means, a file, which contains a set of commands with in it, If any file
contains commands, then that file can be an executable file. Shell Scripts can be useful
to execute the set of commands at a single moment of-time, we will get our required
(er
outputs and those can also be saved under a file. iting the commands
separately will consume much more time. Using shell scripts we cai\'reduce this time
at a greater extent. Shell Scripts can also take arguments is known as Command Line
Arguments.
Uses of shells
1. Customizing your work environments.
. Automating your daily tasks.
. Automating repetitive tasks
. Executing important system pracedure like shutting down the system
Qepen
. performing same operating on many files.
‘The Kemel and the Utilities
The UNIX system is itself logically divided into two pieces
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842TOWLE BRON OHO a RAR arian ORR, HEARS 40 SAR OMNBDE, | IRAN,
. ; sevverd APR aL
"SAADR 20 ian JNNOB aD tHERBoNd & “AUAISIS & HO Hod FRNA)
isi pe8 40
weadord WORRIRNES [RENE & dn HIS OF (HRISAR M10 x? Aa) weasts
‘Mennepien DOOIO ye pe) aWOS Bn AB ROC NERO 0 A Se IpRS
weadold & BA HOARE ae FAG IBOURIOS ABA HOA “OBB 48H OLA WH SH aiesseut
-Boj oi oF us A ae aque ONO) A FIP J HO a
PORE ap Uy WRIRS HOA HO ‘naBoy B 198 Prous Rak (Aressa2ae fk SUA JO
aadnes & Aay soINg aA seeid PUR) PREHAD) at We WHA Red Se WORs Se OSU 185 BH
“ono EAPO PAA © PRON BEG XING 8 OAS SITE
b “de ss Hp 40 PUREE UO FN
S38) OY WAM Sao TENA ShuBAd Jo aataRbas amaId ap a] OMA SAE 4385 UT
AHBISAS ANA ©) LH Boy HOA
RAWAM WOH RIONG 405 LOHAN ARAN 800) ‘TRUS BEE,
‘NOHO Si SONENE PUY AOA are pa
weer ap speej waists INA oi sane
46 08 “PAIR oq PURLARES Sr TYBROIG
_ 8 RUE SIP ai Uo Sapisar WHE : % Waysis
mina aH RPM MOB REL] 3 OWE OIE
NRHONG AWM ate PUR YSIp sAaindNES SHR aL,
EMO FNYS sa iA AUR paleo PUB Uo PLAT st seIndiReD Alp OLN ayy HEY
iowa SfoinduioD auf UE SopIsek PUL WAISAS KING otf JO HUaly OU SF faUIBY etfsand then just waits for someane to type in something. As soom as someone types im
some characters followed by Enter, the getty in disappears; but before it goes
away, it starts up a program called login to mish the process of logging in B 0
gives login the characters you typed im att the iimal—characters that presumably
Nanesh i Teclmallngies, Opp Satyam Theatre, Anmeenpet, Wiydisabadl FiO) Z5PR66S, ZSTSAGAD
naanUNIX _ Page 152 of 208
login started on sue's terminal.
—}
fogin: sue
Password:
When login begitis execution, it displays the string Password: at the terminal and then
waits for you to type your password. After you | typed it, login then proceeds to
verify your login name and password against the: corresponding entry in the file
/etc/passwd. This file contains one line for each’ user of the system. That line
specifies, among other things, the login name, home directory, and program to start
up when that user logs in.[1] The last bit of information:(the program to start up) is 4
stored after the last colon of each line. If nothing follows the last colon, the standard
shell /usr/bin/sh is assumed by default. The following three lines show typical lines
from /etc/ passwd for three users of the system: sue, pat, and bob:
After login checks the password you typed in against the one stored in /etc/shadow, 4
it then checks for the name of a program to execute. In most cases, this will be j
/usr/bin/sh, /usr/bin/ksh, or /bin/bash. In other cases, it may be a special custom-
designed program. The main point here is that you can set up a login account to
automatically run any program whatsoever whenever someone logs in to it. The shell
just happens to be the program most often selected.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 153 of 208
So login initiates execution of the standard shell on sue's terminal after validating her
password,
Jogin executes
. login: sue
Password:
According to the other entries from /etc/passwd'shown previously, pat gets the
program ksh stored in /ust/bin (this is the Korn shell), and bob gets the program
data_entry.
The init program starts up other programs similar to getty for networked connections.
For example, sshd, telnetd, and rlogind are started to service logins via ssh, telnet, and
rlogin, respectively. Instead of being tied directly to a specific, physical terminal or
modem line, these programs connect users! shells to pseudo ttys. These are devices
that emulate terminals over network connections. You can see this whether you're
logged in to your system over a network or on an X Windows screen:
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842i
i Techaningies, Opp Suyam Mhaatne, Amannpet, Wiydksald! Pk OAD ZI PAGK, ZITRBID
NaseathPage 155 of 208
They ae different types of shells:
4, The Bowsne Shell
2, TheC shell
3. The Kom shell
4. Bash, Bourne Again Shells
5. Tesh, The T € Shell
‘The original UNIX shell is known as sh, short for Shell or the Bourne Shell, named for
@ Steven Boume, the creator of sh. As shell go, sh remains faily primitive, but it was
© quite advanced fox the 1970s, Bowme Shell has been considered a standard part of
UNDX for decades. The shell prompt is $, Execution command sh.
Designed by Bill joy at the University of Califomia at Berl € shell was so
named because much of its syntax parallels that of the C programming language. The
shell 135 % and execution command Csh.
command and press the Enter key B2), y you by
then proceeds to cary: out your request Gtep you ask it to execute a particular
plogaam, the shell searches the disk wntil it fi named program, When found,
‘the shell asks the kemel to initiate the piogiam tion and then the shell "goes to
Skeep'' until the progiam has finished Gtep 3), The kemel copies the specified program
Me Memory and begins its execution. This copied progam is called a process; tn Unis
Way, the distinction is made between a progiam thatis kept ina file on the disk anda
process that is im memory doing things.
Neves i Technlenies, Opp Snaxen Theatie, Amacrpes, Hyderabad Rin OM 237A, 2572,dard output, it will’appear at your terminal unless
id. Similarly, if the program reads input from
in i unless redirected from a file or
ontrol afice again returns to the shell, which
Responsibilities of the SHELL
The shell is ultimately responsible for making s
prompt get properly executed. The following are the responsibilities of a shell.
Program Execution
.. Variable and file Substitution *
. 1/O redirection
. Pipeline hookup
. Environment Control
. Interpreted Programming Language
SeReENE
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 157 of 208
The shell’s responsi ies
program
execution
variable and
fwename
substitution
YO
redirection
environment
control
pipeline
hookup
Program Execution
The shell is responsible for the execution of all programs that you request from your
terminal. Each time you type in a line to the shell, the shell analyzes the line and then
determines what to do. As far as the shell is concerned, each line follows the same
basic format:
Program-name arguments
The line that is typed to the shell is known more formally as the command line. The
shell scans this command line and determines the name of the program to be executed
and what arguments to pass to the program. The shell uses special characters to
determine where the program name starts and ends, and where each argument starts
and ends. These characters are collectively called whitespace characters, and are the
space character, the horizontal tab character, and the end-of-line character, known
Naresh i Technologies, [Link] Theatre, Ameerpét, Hyderabad Ph:040 23746666, 23734842UNIX Page 158 of 208.
more formally as the newline character. Multiple occurrences of whitespace characters
are simply ignored by the shell. When you type the command.
mv tmp/mazewars games
‘The shell scans the command line and takes [Link] the start of the line te the
first whitespace character as the name of the program ‘o-executer mv. The set of
characters up to the next whitespace character isthe first. argument te mv:
‘tmp/mazewars. The set of characters tip to the next whitespace charatter (known as a
word to the shell)—in this case, the newline=is the second argument to mv: games.
After analyzing the command line, the shell then proceeds to execute the mv
command, giving it the two arguments tmp/mazewaré and games.
you assign values to variables.
the command line, preceded by a
to the variable at that point. The
and line. In faet; the shell seans
ion characters *, ?, of [...] before
id its arguments. Suppase that
shell also perforins
the command line looking f
determining the name of the program to exec
your current directory contains the files as shown
Now let's use filename substitution for the echo command: 4
§ echo * List all Ales
[Link] progl shortcut sweeney ‘
§
How many arguments do you think were passed to the echo program, ene or fou?
Because we said that the shell is the ene that performs the filename substitution, the’
answer is four, When the shell analyzes the line 7
echo *
Naresh / Technologies, [Link] Theatre, Amesrpet, Hyderabad Ph:040 23746666, 23734842Page 159 of 208
Echo [Link] prog! shortcut sweeney
hen the shell determines the arguments to be passed: to the command. So echo never
es the asterisk. As far as it's concerned, four arguments were typed on the command
. Execution of echo.
arguments
7 +
sweeney
/O Redirection:
It is the shell's responsibility to take care of inputand output redirection on the
command line. It scans-the command line for the occurrence of the special redirection
characters <, > oF >>.
When you type:the command
echo Remember to tape Law and:Order > reminder
the shell recognizes the special output redirection character > and takes the next word.
on the command line as the name: of the file that:the output is to be redirected to. In
this case, the file is reminder. If reminder already exists and you have write access to
it, the previous contents are lost (if you don't have write access to it, the shell gives
you an error message).
Before the shell starts execution of the desired program, it redirects the standard
output of the program to the indicated file. As far as the program is concerned, it
never knows that its output is being redirected. It just goes about its merry way
writing to standard output (which is normally your terminal, you'll recall), unaware
that the shell has redirected it to a file.
Let's take another look at two nearly identical commands:
$we -Lusers
S users
$ we -1< users
5
$
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 160 of 208
In the first case, the shell analyzes the command line and determiries that the name of
the program to execute is we and it is to be passed two arguments: “1 and user
Execution of we -lusers.
arguments [a |
we ee
users
When we begins execution, it sees that it was passed two. arguments. The first
argument, -L tells it to countthe number of lines. The second argument specifies the
name of the’file whose lines: are to be counted:“So we opens the file users, counts its
lines, and then prints the count together with the filename at thi ‘inal.
Operation of we in the second’ case is slightly different. The shell’ spots the input
and line. The word that follows on the
¢ ‘put is to be redirected from. Having "gobbled.
up" the < users from’ the command line, the shell-then starts execution of the wc
program, redirecting its standard input from the file users and passing it the single |
argument-l = :
% e looking for redirection characters, it also
looks for the pipe character |. For each such chatacter that it finds, it, connects the
standard output from the command preceding the'| to the standard input of. the one.
following the |. It then initiates execution of both programs.
So when you type
who | we-I an .
the shell finds the pipe symbol separating the commands who and we. It connects the,
standard output of the former command to the standard input of the latter, and then,
initiates execution of both commands. When the who command executes, it makes a
list of who's logged in and writes the results to standard output, unaware that this is,
not going to the terminal but to another command instead. {
When the we command executes, it recognizes that no filename was specified and,
counts the lines on standard input, unaware that standard input is not coming from:
the terminal but from the output of the who command. 2
Environment Control .
The shell provides certain commands that let you customize your environment. Yow
environment includes your home directory, the characters that the shell displays '
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 161 of 208
prompt you to type in a command, and a list of the directories to be searched
whenever you request that a program be executed.
Interpreted Programming Language
The shell has its own built-in programming language, This language is interpreted,
meaning that the shell analyzes each statement in the’[Link] line at a time and
then executes it. This differs from programming languages sucli‘as,C and FORTRAN,
in which the programming statements are typically spmpiled into a machine-
executable form before they ate executed.
Programs developed in interpreted programming langnages are typically easier to
debug and modify than compiled. ones. However, they usually take much longer to
execute than their compiled equivalents.
find in most other
m-making statements,
ited. Modern'shells based on the IEEE
ys, data:typing, and built-in
The shell programming language provides features
Variable value can change during
Variables are two types:
1. System defined variables
2. User defined variables
System defined variables
Environmental variables are used to provide information to the programs you use.
You can have both global environment and local shell variables. Global environment
variables are set by your login’ shell and new programs and shells inherit the
environment of their parent shell. Local shell variables are used only by that shell and
are not passed on to other processes. A child process cannot pass a variable back to its
parent process.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040.23746666, 23734842UNIK | - Page 162 of 208
The’ current environment variables are displayed with the "env" or “printenv"
commands. Some common ones are:
. DISPLAY The graphical display to use,e.g. nyssa:0.0
. EDITOR The path to your default editor, e.g. /ust/bit
. GROUP Your login group; eg. staff
: HOME Path to Your home directory, e.g. /home/ frank
. HOST The hostname of your system, e.g, nyssa
+ °°" IRS Internal field separators, usually any white space (defaults to tab, space
and )
+ LOGNAME The name you login with, e.g, frank
+. PATHS, Paths |
be searched for.
° USER Your username, eg, frank
Many environment variables will be set automatically when you login. You can
modify them or define others with entries in your startup files or at anytime within
the shell. Some variables you might want to change are PATH and DISPLAY. The
PATH variable specifies the directories to be automatically searched for the command
you specify. Examples of this are in the shell startup scripts below.
You set a global environment variable with a command similar to the following for
theC shell: ,
Ssetenv NAME value
and for Bourne shell:
$ NAME=value; export NAME
User defined variables
Naresh Technologies; [Link] Theatre; Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 163 of 208
These are defined by user and are used most extensively in shell programming
Creating user defined variables
1. The first character of a variable name should be alphabet or underscore
. No commas or blanks are allowed within a variable name
2
3. variables names should be of any reasonable length
4, variable names are case sensitive
7
. Itshouldn’t be reserved word.
Shell keywords
Echo
Trap
Case
Wait
Done Umask
Export return
For
The readonly Command
The readonly command is used to spec
subsequently changed. For example,
‘ify variables whose values cannot be
readonly PATH HOME
makes the PATH and HOME variables read-only. Subsequently attempting to assign a
value to these variables causes the shell to issue an error message:
Naresh i Technologies, Opp Satyam Theatre. Ameerpet, Hyderabad Ph:040 23746666. 23734842UNIX Page 164 of 208,
§ PATH=/bin:/ust/bi
$ readonly PATH
$ PATH=$PATH:/users/steve/bin
sh: PATH: is read-only
$
Here you see that after the variable PATH was made read-only, the shell printed an
error message when an attempt was made to assign a value to it.
To get a list of your read-only variables, type readonly -p without
2] By default, Bash produces output of the form declare -r va
compliant output, you must i
set command with the -o po:
$ readonly -p
readonly PATE
$
unset removes both exported
You should be aware of the fact | that the read- only variable attribute is not passed
down to subshells. Also, after a variable has been-made read-only in a shell, there is
No way to "undo" it. i
The unset Command
Sometimes you may want to remove the definition of a variable from your
environment. To do so, you type unset followed by the names of the variables:
$ x=100
$ echo $x
100
$ unset x Remove x from the environment
$ echo $x
Naresh / Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
ihIX Page 165 of 208
Bs
‘The eval Command
| This section describes another of the more unusual commands in the shell: eval. Its
format is as follows:
eval command-line
F where command-line is a normal command line that you would type at the terminal.
When you put eval in front of it, however, the net effect is that the shell scans the
command line twice before executing it,[1] For the simple case, this really has no
effect:
[1] Actually, what happens is that eval simply executes:the command passed to it as
arguments; so the shell processes the command line when passitig the arguments to
eval, and [Link] again when eval executes the comman e net result is that the
command line is scanned twice by the shell.
$ eval echo hello
But consider the follo aple without the use of eval:
$ pipe="|"
$1s Spipe we 1
|: No such file or directory
we: No such file or directory
-I: No such file or directory
$
Those errors come from ls. The shell takes care of pipés‘and I/O redirection before
variable substitution, so it never recognizes the pipe symbol inside pipe. The result is
that the three arguments |, wc, and -1 are passed to Is as arguments.
Putting eval in front of the command sequence gives the desired results:
$ eval Is $pipe we -1
16
$
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 166 of 208
The first time the shell scans the command line, it substitutes | as the value of pipe.
Then eval causes it to rescan the line, at which point the | is recognized by the shell as
the pipe symbol.
The eval command is frequently used in shell programs that build up command lines
inside one or more variables. If the variables contain any characters:that must be seen
by the shell directly on the command line (that is, not'as the result of substitution),
eval can be useful. Command terminator (;, |, &), I/O redirection (<, >), and quote
characters are among the characters that must appear directly on the command line to
have any special meaning to the shell.
For the next example, consider writing a program last whose sole purpose is to
display the last argument passed to-it- You needed to get at [Link] argument in the
mycp program in Chapter 10, "Reading and Printing Data. 2 you did so by
shifting all [Link] until the last one was left. You can’ v also tse eval to get at it
as shown:
$ cat last
eval echo \$$1
$ last one two
four
$ last *
zo0_report
$
The first time the shell scans
echo \$$#
the backslash tells it to ignore the $ that immediately follows. After that, it encounters
the special parameter $#, so it substitutes: its value on, the command line. The
command now looks like this:
echo $4
(the backslash is removed by the shell after the first scan). When the shell rescans this
line, it substitutes the value of $4 and then executes echo.
This same technique could be used if you had a variable called arg that contained a
digit, for example, and you wanted to display the positional parameter referenced by
arg. You could simply write
eval echo \$6arg
Naresh # Technologies, Opp-Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842aa Page 167 of 208
The only problem is that just the first nine positional parameters can be accessed this
Way; to access positional parameters 10 and greater, you must use the ${n} construct:
eval echo \${$arg)
pea how the eval command can be used to effectively create “pointers" to variables:
x=100 —
$ ptrx=x
$ eval echo \$$ptrx Dereference ptrx
100 a
S$ eval $ptrx=50 Store 50 in var that ptrx points to as t
$ echo $x See what happened
50
$ Es
The wait Command :
If you submit.a command line to the background for execution that command line
shell independent of your current shell (the job: is said to run
‘may have sent a large sort into
finish because you need to use
wait process-id
where process-id is the process id number of the process you want to wait for. If
omitted, the shell waits for all child processes to complete execution. Execution of
your current shell will be suspended until the process-or proresses finish execution.
You can try the wait command at your terminal:
$ sort big-data > sorted_data & Send it to. the background
[2] 3423 Job number & process id from the shell
$ date Do some other work
Wed Oct 2 [Link] EDT 2002
$ wait 3423 Now wait for the sort to finish :
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 168 of 208
$ When sort finishes, prompt is returned
The $! Variable
If you have only one process running in the background, then wait with no argument
suffices. However, if you're running more than one commtand [Link]-background a
You want to wait on a particular one, you can take advantage of the fact that the she
Stores the process id of the last command executed in the background inside the
special variable $!, So the command -
wait $!
waits for the last process sent to: the background to complete execution. As
mentioned, if you send several commands to the background, you can save the value
of this variable for later use with wait: ’
prog] &
pidi=$!
prog? &
pid2=$!
wait Spidl # wait for prog! to finish
wait $pid2 # wait for prog? to finish
We are having two types of shell scripts. They are
1) Non-Interactive Shell Scripts.
2) Interactive Shell Scripts.
1) Non-Interactive Shell Scripts:-
1 we are not interacting with the shell script while it is executing then it is known as
Non-Interactive shell scripts.
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 169 of 208
Examples:
# This is a sample script, to display message
echo "Welcome to the world of UNIX Shell Scripting"
echo "good morning"
# Shell Script @1
# Finding the biggest of two niumbers.
a=123 =
b=167 a
if [$a-gt $b]
then
echo “ a is
# Shell Script @2._
# Finding the biggest of two numbers
a=123 :
bo234
if [$a-gt $b]
then
echo “ ais big”
else
echo” bis big”
# Shell Script @3
# Script for display the list of files, the current working user's list and present working
directory.
Is-x
who
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666,23734842UNIX Page 170 of 208,
pwd
# Shell script @4
# Script for count number of users
Echo “There are ‘who | we -I users —
2) Interactive Shell Scripts:-
If we are interacting with the shell script while it is executing then it is known as
Interactive shell scripts.
$read:- This command is usedto'take a’[Link] the [Link] an end user and
save that value in a variable.
# This is a sample seript ro re;
echo -n "enter a value :"
read x
echo "given value = $x"
# samplel script to get two values and calculate sum .
echo -n "enter first value :"
read x
echo -n “enter second value :"
read y
(@=x+y))
echo "result = $2"
# sample script to get input through command line arguments,
# or also called positional parameters
echo "script name = $0"
echo "[Link] args = $#°
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 171 of 208
echo "first arg = $1"
echo "second arg = $2"
echo "all args = $@"
# input through files, store the data in a file separated by spaces
# this can read one line ,
# first field assigned to x and rest of the line assigned to y
echo -n "enter file name :" -
read f a
read xy < $f
echo "x = $x
# example to calculate’sqrt
#/bin/bash
echo-n "enter a num :"
read x
r= ./sqrt $x
echo "square root = $1"
# example on reading strings and calculating length
echo -n “enter a string :"
read x
echo "given string = $x"
echo “length = ${#x}"
# reading passwords, display is disabled
echo -n "enter a password :"
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 172 of 208
stty -echo -icanon
read x
stty echo icanon
echo
echo "given password = $x"
echo-n "enter user name :"
read x -
who | grep $x > /dev/null && echo "user logged in®
who | grep $x > /dev/null | | echo "user not logged in"
Arithmetic Opérators
+ Addit
- Subtract
* Multiplication
/ Division
%o Mod © S
‘a Power
Relational Operators
On Strings
i two strings are equal
two strings are not equal
/dev/null
then
echo "login name $n is present"
else
echo "login name $n not present"
fi
# example on using a menu and if
echo "add sub mul"
echo -n “enter your choice :*
Page 178 of 208
Naresh 7 Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 179 of 208
read op
op=echo "Sop" | tr [upper] [Jower:]
if[ "Sop" = "add" -o "Sop" = "sub" -o "Sop" = "mul" ]
then
echo -n “enter two values :"
read xy
fi
if[ Sop = "add" ]
then
echo "res = $((x+y))"
elif [ Sop = *suib"
elif [Sop = "mul" ]
then
echo "res = $((x*y))"
else :
echo “invalid option"
fi
#Script 10
echo -n "enter first:"
read x
echo -n "enter second :"
read y
if["Sx" \> "Sy"]
then
echo 'Sy"
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX Page 180 of 208
echo "$x"
else
echo "$x"
echo "Sy"
fi
Naresh Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 181 of 208
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 182 of 208,
LOOP:
All programming languages provide mechanisms that enable you to execute
repeatedly a group of statements. The shell is no exception, as it provides two such
language statements: the for and the while. This [Link] then for, the more
widely used of the two. The general format of the for statement'in the shell is:
> for statement
for variablename in list of values
do command
command *
done: variable is any shell variable that you choose. It is‘listed without a leading
dollar sign, The: number of ite list determines the number of times the
commands enclosed between the do and done will be executed. Each time the loop is
assigned to variable.
executed, the nex‘ ie in list
# example on using for
for((i=Li<-5;i++))
do
echo "hello"
done
# calculate sum of squares
s=0
((s= s+ i4i)
done
cho "sum of square = $s"
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabgd Ph:040 23746666, 23734842UNIX Page 183 of 208
# displaying multiplication table
echo -n "enter a value :"
read x
for((i1i<=10;++))
do
((p=x*i)
echo "$x x $i= $p" a
done
# example on stim of n values using for
echo -n "enter how many val
read x
for((e1i<=
do
echo -n "enter va
readn
(6 =s* n))
done
echo "sum = $s"
# example on nested for
for((i-1ji<=5;i++))
do
for(§-4j>=i-)
do
echo-n""
done
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842echo -n
done
echo
done
# example on nested for
for((i=1;i<=5;i++))
do
for((j=1js=ij+#))
do
echo-n"*"
done
echo
done
# example to display file and dir
foriin*
do
if [-£"Si"]
then
echo "file -> $i"
elif [-d"Si"]
then
echo "dir -> $i"
done
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842
Page 184 of 208Page 185 of 208
#script for wait 5 sec.
echo -n “enter aname (5 seconds) :"
old=stty -g°
stty -icanon min 0 time 50
read x
stty "Gold"
if [-2"Sx"]
then
echo "timed out"
else
echo "given name:
# display the list of values
for val in 1020304050
do :
echo “$val”
done ;
The above example will display 10, 20, 30, 40, and 50. —
The while Statement
The while is another looping statement provided by the shell. It enables you to
repeatedly execute a set of commands while a specified condition is true. The format
of the while is:
while { condition ]
do
command
command.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX
done
# sample on using while
cl
while [ $c -le 10]
do
echo “hello” -
(c++)
done
# reading input till eof
# calculating [Link] lines & [Link] chars in the given input
_ a
while read I
do
(c+)
((n= n+ $f4#l} + 1)
done
echo “[Link] lines = $c"
echo "[Link] chars
# reading froma file
# calculating how many times a word 'unix' repeated
echo -n “enter file :"
read £
c=0
while read 1
Page 186 of 208
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 187 of 208
do
foriin $l
do
if[ "Si" = "unix" ]
then
(c++)
fi
done -
done < $f
echo "[Link] times unix = $c"
# script can be executed in tl
u=$1
if[ $#-eq1]
then
while [1]
do
w= who | cut-d""-f1°
foriin $w
do
if [ "Su" = "$i" ]
then
echo
echo "Su logged in"
exit
fi
done
sleep 30
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 188 of 208
done
else
echo "usage : s85 username"
fi
#user logged in or not
echo -n “enter user name :"
read x
if who | grep $x > /dev/null
then
echo "user logged in"
else
echo “user not logged in"
fi
#generating password
x="abcdefghijkdmn op qrstuvwxyzA BCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
cl %
while [ $c -le 8 ]
do
((p = $RANDOM % 62))
w=${x:$p:1}
pw=echo "S{pw}$w"™
let "c++"
done
echo "password = ${pw
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 189 of 208,
In the above syntax, until the condition is true the commands with in the do and done
will be repeatedly executed.
Here we are introducing one new command, ie.
More on Loops
Breaking Out of a Loop
Sometimes you may want to make an immediate exit from a loop. To just exit from
the loop (and not from the program), you can use the break command, whose format
is simply
break e
When the break is executed, control is sent immediately out‘of the loop, where
execution then continues as normal with the command that follows the done.
‘but to return an exit status of zero, The
a nonzero exit status. If you write
while true
do
done
the while loop will theoretically be executed forever because true always returns a
zero exit status. By the way, the : command also does nothing but return a zero exit
status, so an “infinite” Joop can also be set up with —
while :
do
done
Because false always returns a nonzero exit status, the loop
‘until false
do
done
will theoretically execute forever.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 190 of 208
The break command is often used to exit from these sorts of infinite loops, usually
when some error condition or the end of processing is detected:
while true
do
cmd=8(getcmd)
if ["$cemd" = quit ]
then.
break
else
processcmd "$cmd"
to execute the getcmd and processcmd programs
point, thé break command will be executed, thus
If the break command is used in the form
breakn
the n innermost loops are immediately exited, so in
for file
do
while [ “Scount’ -It 10]
do
if [-n"Serror"}
then
break 2
fi
done
done
both the while and the for loops will be exited if error is nonnull.
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 191 of 208
Skipping the Remaining Commands in a Loop
The continue command is similar to break, only it doesn't cause the loop to be exited,
merely the remaining commands in the loop to be skipped. Execution of the loop then
continues as normal. Like the break, an optional number can follow the continue, so
continue n
causes the commands in the innermost n loops to be skipped; but execution of the
loops then continues as normal.
for file
do
if [!-e "Stile" ]
then
echo "$file not found!"
continue.
fi
#
# Process the
# s
fil
done
Each value of file is checked to make sure that the file exists. If it doesn't, a message is
printed, and further processing of the file is skipped. Execution of the loop then
continues with the next value in the list. Note that the preceding example is
equivalent to writing
for file
do
if [!-e "Stile" ]
then
echo "$file not found!"
else
#
# Process the file
#
fi
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX Page 192 of 208
done
Executing a Loop in the Background
An entire loop can be sent to the background for execution simply-by placing an
ampersand after the done:
$ for file in memo[1-4]
>do
> run $file
> done & Send it to the background
[1] 9932
: 7
request id is laser1-85 (standard input)
request id is laser1-87 (standard input)
request id is laser1-88 (standard input)
request id is laser1-92 (standard input)
data a from standard input. Output
redirected from the loop to.a fil applies to all commands in the loop that write to
standard output:
$foriin1234
>do
> echo $i :
> done > loopout Redirect loop's output to loopout
$ cat loopout ‘
1
2
3
4
$
You can override redirection of the entire loop's input or output by explicitly
redirecting the input and/or output of commands inside the loop. To force input or
output of a command to come from or go to the terminal, use the fact that /dev/tty
always refers to your terminal. In the following loop, the echo command's output is
explicitly redirected to the terminal to override the global output redirection applied
to the loop:
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX, Page 193 of 208
echo "Processing file $file" > /dev/tty
done > output
echo's output is redirected to the terminal while the rest go¢s to the file output.
Naturally, you can also redirect the standard error output from a loop, simply by
tacking on a 2> file after the done:
while [ "Sendofdata" -ne TRUE ]
do
done 2> errors
Here output from all commands in he loop writing, to "slandatd error will be
redirected to the file erro
entire output from a loop can
ted manner. Here's a highly
from a for command piped into we:
$forim1234 —
>do
> echo $i
> done | we -1
4
$
Typing a Loop on One Line
If you find yourself frequently executing loops directly at the terminal, you'll want to
use the following shorthand notation to type the entire loop on a single line: Put a
semicolon after the last item in the list and one after each command in the loop. Don't
put a semicolon after the do.
Following these rules, the loop
foriin1234
do
echo $i
done
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph;040 23746666, 23734842UNIX“! Page 194 of 208
becomes
for iin 123 4; do echo $i; done
‘And you can type it in directly this way:
$ for iin 123 4; do echo $i; done
_
2
3
4
$
The same rules apply to while and until loops.
if commands can also be [Link] the same line using a similar format:
$ if [1-1]; then echo yes; fi
yes
$ if [1 =2]; theitecho yes; else echo no; fi
repeatedly execute a set of commands while a specified condition is false. The format
of the until is:
until [ condition ]
do
command
command
done
# example on using until
cl
until { $c -gt 10 ]
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842IX. Page 195 of 208
echo "hello"
(c+)
* Done
expt (expression)
This command is used to perform a single arithmetic calculation and display the
result. Although the POSIX standard shell supports built-in integer arithmetic
operations, older shells don't. It's likely that you may see command substitution with
a Unix program called expr, which evaluates an expression. given to it on the
command line:
$expr1+2
t be a separate argument, thus
The usual arithmetic operators are recognized by expr: + for addition, - for
subtraction, / for division, * for multiplication, and % for modulus (remainder),
$ expr 10 +20 / 2
20
$
Multiplication, division, and modulus have higher precedence than addition and
subtraction. Thus, in the preceding example the division was performed before the
addition.
$ expr 17*6
expr: syntax error
$
What happened here? The answer: The shell saw the * and substituted the names of
all the files in your directory! It has to be quoted to keep it from the shell:
Naresh i Technologies, [Link] Theatre, Amee‘pet, Hyderabad Ph:040 23746666, 23734842,UNIX Page 196 of 208
$ expr "17*6"
17*6
$
That's not the way to do it. Remember that expr mustsee each operator and operand
as a separate argument; the preceding example sends the-whole-expression in as a
single argument.
Sexpr17\*6
102
$
Naturally, one or more of the arguments to expr can be the value stored inside a shell
variable because the shell takes care of the substitution first anya
$izl
$ expr $i+1
on shell variables. Do the same
ymmand substitution mechanism to
$i-Sexpr$it+1) Add 1toi
$ echo $i
2
$
In legacy shell programs, you're more likely to see expr used with back quotes:
Si-expr$i+1 Add 1toi
$ echo $i
3
$
Note that like the shell's built-in integer arithmetic, expr only evaluates integer
arithmetic expressions. You can use awk or bc if you need to do floating point
- calculations. Also note that expr has other operators. One of the most frequently used
ones is the : operator, which is used to match characters in the first operand against a
regular expression given as the second operand. By default, it returns the number of
characters matched.
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 197 of 208
The expr command
expr "Sfile":"*"
returns the number of characters stored in the variable file, because the regular
expression .* matches all the characters in the string. |
Examples: $ expr 1 +2 $ expr 234 - 197 \
3 37 : .
S expr 5 \*50 $ expr 200 / 50 sgh
250 4
$ expr 200 % 50
0
$count=10
$ expr $count + 10
20
The case--in-esac_ Statement:
The shell case statement is useful when you want to compare’avalue against a whole
‘ou know that this can be done with an if - elif statement chain, but
@ an easier to-write and to read. The general
pattern n) command
command
esac:
Operation of the case is as follows: value is successively compared against pattern1,
pattern2, ....... pattern n. As’soon as a match is found, the commands listed after the
matching pattern are executed, until a double semicolon is reached. At that point, the
Naresh i Technologies, [Link], Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX . Page 198 of 208
case statement is terminated. If value does not match any of the specified patterns,
then no action is taken, and the special pattern * commands will be executed.
Here one sample example:
# Shell Script @5
#1/bin/bash
# example on case
echo -n "enter a single digit : "
read x
case $x in
0) echo "zero";
1) echo "one";
2) echo "two";
3) echo “three
4) echo "four"
*) echo "enter
esac
#!/bin/ bash
# finding given char is alpha or digit or other
echo -n "enter a char :"
read x
case $xin
{[alpha:]}) echo "an alphabet",;
{0-9]) echo "digit";
*) echo “other char";
Esac
#1/bin/ bash
4 the following is a sample program
# to use , $52 vall operator val2
Naresh / Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 199 of 208
# example s52 10 + 20
2-$1
y783
case $2in
+) echo "sum = $((x+y))";
-) echo "diff = $((x-y))"s;
\*) echo "prod = $((x*y))";
*) echo "use +/-/*"; -
esac.
# example on using menu and case
echo "add sub nul" :
echo -n "enter y
read op
op=echo "Sop"
if[ "Sop" =
then
echo -n "enter |
read xy
fi
case Sop in
add) echo "sum= $((x+y))";
sub) —_ echo "res = $((x-y))";
mul) echo "res = $((x*y))";;
*) echo "invalid selection"
esac
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX Page 200 of 208
Example Scripts
1). Write a program to copy a file
vicopysh
echo Enter source filename and target filename
read src trg
if cp Sstc $trg
then
echo file copied successfully -
else
echo file fail to copy
fi
[Link]
echo “Enter anynumbe
readn
iff expr $n%2
then
“eq,
echo “$n is even number”
else
echo “$n is odd number”
fi
3). Write a program given string is empty or not
[Link]
echo “Enter a string”
read str
iff-z$str]
chen
echo “String is NiTty”
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad. Ph:040 23746666, 23734842UNIX Page 201 of 208
else
echo “String is not NiTty”
fi
4). Write a program to display all sub-directories in thé current directory.
[Link] er
For lin *
do
iff-d $i] ea
then : ae
echo $i
fi
done
5). Write a pro
vinumbersh
echo “Enter th
1
while[$i-le 10]
do
echo $i
expr $it 1’
done
6). Write a program to delete given file
viramash
echo “Enter any file which is existed”
read fname
ifm $fname
then
echo “File deleted successfully”
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842,UNIX ae Page 202 of 208
else
echo “File not deleted”
fi
wq
7. Count files in directory (not hidden one's)
#1/bin/ksh
echo * | we-w
[Link] change uppercase filenames to lowercase
#/bin/sh
if [$# -eq 0]; then
for f in $* ; do
goecho $f | tr
[A-Z]" "fa-z]
echo mv -i $f $g
mv -i$f$g
done
[Link] ten day old files
#/bin/ksh
echo "This script will put the names of ten day old files in /tmp in /tmp/[Link]
file"
find /tmp -size 0 -atime +10 -exec Is -1 {} \; > /tmp/[Link]
find /tmp -size 0 -atime +10 -exec rm -f {} \;
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 203 of 208
10. swap two files (if they exist):
if test -f $1
then
if test -£$2
then
mv $1 [Link]
my $2 $1
mv [Link] $2
else
echo "$2 is not a file. Quitting"
for ain4837134
do
echo -n $a" "
if test $a -ge 5
then
echo "big"
else
echo "small"
fi
done
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 204 of 208
12. Display the steps of success
echo "Climb the steps of success"
for ((i=1;i<=5; i++ ))
do
for ((j=1:j
j++))
do é
echo-n" |"
done
echo "_"
done
13. Write script to print given number in reverse order, for eg. If no is 123 it must
print as 321.
if[$#-ne1]
then
echo "Usage: $0 number"
echo" Iwill find reverse of given number"
echo" For eg. $0 123, Iwill print 321"
exit
fi
n=$1
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 205 of 208
tev=0
sd=0
while [ $n -gt 0]
do
sd="expr $n % 10°
rev='expr $rev \*10 + Ssd°
n=expr $n / 10°
done .
echo "Reverse number is $rev"
f all digit, For eg. If no is 123 it's sum
then
echo "Usage :
echo" I will find sum of all digit for given number"
echo" For eg. $0 123, I will print 6 as sum of all-digit (1+2+3)"
exit1
n=$1
sum=0
sd=0
while [$n-gt 0]
do
sd= expr $n % 10°
sum="expr $sum + $sd°
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842.UNIX Page 206 of 208
n=expr$n / 10°
done
echo "Sum of digit for numner is $Ssum"
15. Display Good Morning, Good Afternoon, Good Evéning ,-according to system
time.
temph="date | cut -c12-13° -
dat=date +"%A %d in %B of %Y (%6r)™
if [ $temph -It 12]
then
fi
mess="Good Afternoon $LOGNAME"
fi
if [Stemph -gt 16 -a $temph -le 18 ]
then
mess="Good Evening $LOGNAME"
fi
16. Display the following pattern
1
22
Naresh i Technologies, Opp Satyam Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842UNIX Page 207 of 208
55555
echo "Can you see the following:"
for (( i=1; i<=5; i++ ))
do
for ((j=1; j<=i; j++ )) : af
do
echo -n "$i"
done
echo"
done
12345
echo "Can you see the following:"
for ((i=1; i<=5; i++ ))
do
for ((j=1; j<=i; j++ ))
do
echo -n "5"
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842136UNIX Page 208 of 208
done
echo"
done
18. Displays the stars pattern
echo "Stars"
for ((i*1; i<=5; i++ ))
do -
for ((j=1;j<=i; j++ ))
do
echo-n"*"
do
for ((j-1; j<=i: j++ ))
do
echo -n"*"
Naresh i Technologies, [Link] Theatre, Ameerpet, Hyderabad Ph:040 23746666, 23734842