Sie sind auf Seite 1von 12

KUVEMPU UNIVERSITY

INFORMATION TECHNOLOGY PROGRAMMES


Bachelor of Science in Information Technology - B.Sc. (IT)
Master of Science in Information Technology - M.Sc. (IT)

B.Sc.(IT) - 4th Semester

BSIT - 43

Unix & Shell Programming

Directorate of Distance Education

Kuvempu University

Shankaraghatta, Shimoga District, Karnataka

In
col l aborati on
w i th

Universal Education Trust


Banga l ore

II

Titles in this Volume :


BSIT - 43 Unix & Shell Programming

Prepared by UNIVERSAL EDUCATION TRUST (UET)


Bangalore

First Edition
: May 2005
Second Edition : December 2010
Third Edition : May 2012

Copyright by UNIVERSAL EDUCATION TRUST, Bangalore


All rights reserved

No Part of this Book may be reproduced


in any form or by any means without the written
permission from Universal Education Trust, Bangalore.

All Product names and company names mentioned


herein are the property of their respective owners.

NOT FOR SALE


For personal use of Kuvempu University
IT - Programme Students only.

Corrections & Suggestions


for Improvement of Study material
are invited by Universal Education Trust, Bangalore.
E-mail : info@uetb.org

Printed at :

Pragathi Print Communications


Bangalore - 20
Ph : 080-23340100

III

UNIX AND SHELL PROGRAMMING


(BSIT - 43)
: Contributing Authors :

Smt. Bhuvaneshwari K V
Lecturer, Department of IS&E
BIET, Davanagere
and

Dr. Shreedhara K S
Professor, Department of CS&E
University BDT College of Engineering
Davanagere

IV

V
a

Pr ef ac e

WHAT IS UNIX?
UNIX is operating system software that manages the hardware and software resources of a
computer. UNIX is one of the most widely used operating systems in industry, government, and
education. It is especially popular in academia: according to AT&T, where UNIX was developed,every
major university in the United States now has at least one computer system running under UNIX.
UNIX is not a user-friendly operating system. Novice user can not directly work on the UNIX
operating system but a smart programmer uses this environment and its tools to turn out programs
faster.
Which Version of UNIX?
UNIX versions can be broadly divided into two schools the System V school from AT&T Bell
Laboratories and the Berkeley school from the University of California, Berkeley. More specifically,
versions tend to be looked at as being either based on SVR4 (System V Release 4 AT&Ts last
release before winding up to its UNIX operations) or on BSD UNIX (Berkeley System Distribution).
While many of the features of UNIX are common to all systems.
From the users standpoint, these versions of UNIX are quite similar. Most can trace their
ancestry to either AT&T UNIX or Berkeley UNIX; some are amalgams of both. This book presents
features that are found on almost all UNIX systems, with special emphasis on those that are
common to AT&T System V and Berkeley System Distribution (BSD) 4.3 UNIX.
Who Should Read This Book?
This book is intended for anyone who wants to acquire a working knowledge of UNIX without

VI
having to become a UNIX expert. It is especially appropriate for students of science, engineering,
or business who are taking their first computer programming course.
What Does This Book Cover?
This book covers the basics of the UNIX operating system. It has mainly two parts: i.e.
PART- A and PART-B.
In PART-A, you will find an overview of the UNIX operating system You will learn about the
background of the Unix operating system, using simple Unix commands, the Unix file system,
handling ordinary files and the attributes of files. You will also learn about the powerful editing
features of the vi editor.
In PART-B, you will find the basics of shell, how to manipulate processes in UNIX system?,
How to establish communication in UNIX operating system, How to use different filters? and also
at the end you will how to write shell scripts in UNIX operating system.
How to Use This Book?
Anyone who is just starting with UNIX should read straight through PART-A and PART-B.
Those who are familiar with basics of UNIX can directly go through PART-B.
Each part of this book begins with a chapter explaining the material without requiring the use of
the computer. You should plan to spend about an hour at the terminal to cover each tutorial.
At the end of each section, you will find some short exercises. To derive the maximum benefit
from this text, be sure to work through all of the exercises.

VII
a

Contents

PART A
Chapter 1
INTRODUCTION TO UNIX AND ITS BACKGROUND

1.1
1.2
1.3
1.4
1.5
1.6
1.7

1
2
2
3
4
5
5
6

The Operating system.......................................................................


The UNIX operating system..............................................................
The origins of UNIX.........................................................................
How to work with UNIX ?: A Brief session........................................
Architecture of UNIX.......................................................................
UNIX features..................................................................................
Conclusion........................................................................................
Exercises.........................................................................................

Chapter 2
UNDERSTANDING THE UNIX COMMANDS

2.1
2.2
2.3
2.4
2.5
2.6
2.7

7
8
8
8
9
10
11
11

Looking for a UNIX commands.........................................................


Internal and external commands.........................................................
Structure of UNIX Commands...........................................................
Flexibility of command usage.............................................................
Using manual pages on-line................................................................
When things Go wrong......................................................................
Conclusion........................................................................................
Exercises.........................................................................................

VIII
Chapter 3
GENERAL-PURPOSE UTILITIES

12

3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
3.10
3.11
3.12
3.13
3.14
3.15

12
14
15
15
16
17
18
19
20
21
21
23
23
25
26
26

Displaying calendar: cal.....................................................................


To display system date and time: date................................................
Displaying message: echo.................................................................
An alternative to echo: printf.............................................................
The calculator: bc.............................................................................
Recording your session: script...........................................................
Changing your password: passwd......................................................
Who are the users logged-in?: who....................................................
Know your machines name: uname..................................................
Knowing your terminal: tty.................................................................
Displaying and setting terminal character: stty....................................
Lock your terminal: lock...................................................................
Check your spellings: spell and ispell...................................................
Taming the cursor: tput.....................................................................
Conclusion........................................................................................
Exercises.........................................................................................

Chapter 4
THE FILE SYSTEM

27

4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
4.11
4.12

27
28
28
29
30
30
31
31
32
32
36
37
37

Concept of a UNIX file.....................................................................


Whats in a file name?.......................................................................
The parent-child relationship..............................................................
Checking your present working directory: pwd...................................
Changing the current directory: cd......................................................
Creating directories: mkdir................................................................
Removing directories: rmdir..............................................................
Absolute pathnames..........................................................................
Relative pathnames...........................................................................
Listing directory contents: ls..............................................................
The Unix file system.........................................................................
Conclusion........................................................................................
Exercises.........................................................................................

Chapter 5
MANAGING ORDINARY FILES

38

5.1

38

Displaying and creating files: cat........................................................

IX
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14

Copying the files: cp..........................................................................


Deleting the files: rm.........................................................................
Renaming the files: mv......................................................................
Paging output: more.........................................................................
Printing a file: lp................................................................................
Knowing file types: file......................................................................
Counting lines, words, and characters: wc...........................................
Displaying a data in octal: od.............................................................
Splitting a file into multiple files: split.................................................
Comparing two files: cmp..................................................................
Finding what is common: comm.........................................................
Converting one file to other: diff.........................................................
Conclusion........................................................................................
Exercises.........................................................................................

38
39
40
40
42
43
43
44
45
46
47
47
48
48

Chapter 6
BASIC FILE ATTRIBUTES

49

6.1
6.2
6.3
6.4
6.5

49
50
51
54
54
54
56
57
58
60
60

6.6
6.7
6.8

Listing file attributes: ls -l..................................................................


File permissions.................................................................................
Changing file permissions: chmod......................................................
File system and inodes.......................................................................
Hard links and Symbolic links.............................................................
6.5.1. Hard links................................................................................
6.5.2. Symbolic links..........................................................................
Default file and directory permissions: umask....................................
Locating files: find............................................................................
Conclusion........................................................................................
Exercises.........................................................................................

Chapter 7
THE VI EDITOR

61

7.1
7.2
7.3
7.4
7.5
7.6
7.7

61
61
62
63
64
65
65

The Basics of vi................................................................................


The modes of vi................................................................................
Inserting and Replacing Text..............................................................
Navigation in command mode............................................................
Saving text........................................................................................
Deleting text.....................................................................................
Pattern search..................................................................................

X
7.8 Pattern search and Replace...............................................................
7.9 Miscellaneous operators in command mode........................................
7.10 Conclusion........................................................................................
Exercises..........................................................................................

66
66
67
67

Chapter 8
THE SHELL

68

8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
8.9
8.10
8.11

68
68
69
70
71
74
75
76
77
77
79
79

The shells interpretive cycles............................................................


Common shells.................................................................................
Pattern matching the wild-cards......................................................
Escaping and quoting.........................................................................
Redirection: The three standard files..................................................
/dev/null and /dev/tty: two special files................................................
pipes................................................................................................
Creating a tee: tee............................................................................
Command substitution.......................................................................
Shell variables...................................................................................
Conclusion........................................................................................
Exercises.........................................................................................

Chapter 9
THE PROCESS

80

9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10
9.11

80
82
83
84
85
86
87
88
89
90
91
91

Basics of process..............................................................................
Process status: ps.............................................................................
System process.................................................................................
Mechanism of process creation..........................................................
Running jobs in background................................................................
Jobs execution with low priority: nice.................................................
Killing process with signals................................................................
Execute later: at and batch...............................................................
Running jobs periodically: cron...........................................................
Timing processes: time......................................................................
Conclusion........................................................................................
Exercises.........................................................................................

Chapter 10
COMMUNICATION AND ELECTRONIC MAIL

92

10.1 Write and talk....................................................................................

92

XI
10.2
10.3
10.4
10.5
10.6
10.7

Refusing and accepting messages......................................................


E-mail basics....................................................................................
The universal mailer: mailx...............................................................
A full-screen mail program: pine.......................................................
Details of Users: finger.....................................................................
Conclusion........................................................................................
Exercises.........................................................................................

93
93
94
96
99
99
100

Chapter 11
SIMPLE FILTERS

101

11.1
11.2
11.3
11.4
11.5

101
102
102
103
103
104
104
105
107
107
109
110
111
112
113
113

11.6
11.7
11.8
11.9
11.10
11.11

The sample database.........................................................................


Paginating files: pr............................................................................
Displaying the beginning of file: head.................................................
Displaying the end of file: tail............................................................
Slitting a file vertically: cut.................................................................
11.5.1. Cutting Columns (-c)...........................................................
11.5.2. Cutting Fields (-f)................................................................
Pasting files: paste............................................................................
Ordering a file: sort..........................................................................
11.7.1. Sort Options..........................................................................
Locating repeated and non repeated lines: uniq...................................
Translating characters: tr...................................................................
Searching for a pattern: grep.............................................................
11.10.1 grep options........................................................................
Conclusion........................................................................................
Exercises..........................................................................................

Chapter 12
SHELL PROGRAMMING

114

12.1
12.2
12.3
12.4
12.5
12.6
12.7

114
115
116
116
118
119
120

A simple shell script..........................................................................


Subshells..........................................................................................
The shell as a programming language.................................................
Variables..........................................................................................
Input using the read statement...........................................................
The set command.............................................................................
Arithmetic operations using the expr utility.........................................

XII
12.8
12.9
12.10
12.11
12.12
12.13
12.14
12.15

Control structures..............................................................................
The if statement and test command....................................................
The elif and else statements...............................................................
The case statement...........................................................................
for loops...........................................................................................
while loops.......................................................................................
until loops.........................................................................................
Conclusion........................................................................................
Exercises.........................................................................................

121
122
122
123
124
125
125
126
126

Chapter 13
DEVELOPING SHELL SCRIPTS

127

13.1
13.2
13.3
13.4
13.5
13.6

127
128
130
131
132
134

Creating executable file.....................................................................


Assigning Labels to wc (Word Count) command: mywc.......................
Deleting files safely:del......................................................................
A Daily Reminder System: tickle.......................................................
Displaying arguments multiple times:echo.sh......................................
Exercises..........................................................................................

Das könnte Ihnen auch gefallen