bsit 43 content

12
INFORMATION TECHNOLOGY PROGRAMMES Bachelor of Science in Information Technology - B.Sc. (IT) Master of Science in Information Technology - M.Sc. (IT) In collaboration with KUVEMPU UNIVERSITY B.Sc.(IT) - 4 th Semester BSIT - 43 Unix & Shell Programming BSIT - 44 Software Engineering Directorate of Distrance Education Kuvempu University Shankaraghatta, Shimoga District, Karnataka Universal Education Trust Bangalore

Upload: mukesh-kumar

Post on 28-Nov-2014

259 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: BSIT 43 Content

INFORMATION TECHNOLOGY PROGRAMMESBachelor of Science in Information Technology - B.Sc. (IT)Master of Science in Information Technology - M.Sc. (IT)

Incollaboration

with

KUVEMPU UNIVERSITY

B.Sc.(IT) - 4th Semester

BSIT - 43 Unix & Shell ProgrammingBSIT - 44 Software Engineering

Directorate of Distrance EducationKuvempu University

Shankaraghatta, Shimoga District, Karnataka

Universal Education TrustBangalore

Page 2: BSIT 43 Content

II

Titles in this Volume :BSIT - 43 Unix & Shell ProgrammingBSIT - 44 Software Engineering

Prepared by UNIVERSAL EDUCATION TRUST (UET)Bangalore

First Edition : May 2005

Copyright © by UNIVERSAL EDUCATION TRUST, BangaloreAll rights reserved

No Part of this Book may be reproducedin any form or by any means without the writtenpermission from Universal Education Trust, Bangalore.

All Product names and company names mentionedherein are the property of their respective owners.

NOT FOR SALEFor personal use of Kuvempu UniversityIT - Programme Students only.

Corrections & Suggestionsfor Improvement of Study materialare invited by Universal Education Trust, Bangalore.

E-mail : [email protected]

Printed at :Mayur Print AdsBangalore - 20Ph : 94480 - 42724

Page 3: BSIT 43 Content

III

UNIX AND SHELL PROGRAMMING(BSIT - 43)

: Contributing Authors :

U.M. Mallikarjuna SwamyDCSE, PDCA, B.E., (M.Sc., (Engg.))

and

YasmineM.Sc., PGDCA., M.S. (BITS, Pilani)

Page 4: BSIT 43 Content

IV

Page 5: BSIT 43 Content

V

a

Preface

WHAT IS UNIX?

UNIX is operating system software that manages the hardware and software resources of acomputer. UNIX is one of the most widely used operating systems in industry, government, andeducation. 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 operatingsystem 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 BellLaboratories 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&T’s last releasebefore winding up to its UNIX operations) or on BSD UNIX (Berkeley System Distribution). While manyof the features of UNIX are common to all systems.

From the user’s standpoint, these versions of UNIX are quite similar. Most can trace their ancestry toeither AT&T UNIX or Berkeley UNIX; some are amalgams of both. This book presents features that arefound on almost all UNIX systems, with special emphasis on those that are common to AT&T System Vand 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 havingto become a UNIX expert. It is especially appropriate for students of science, engineering, or businesswho are taking their first computer programming course.

Page 6: BSIT 43 Content

VI

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 backgroundof the Unix operating system, using simple Unix commands, the Unix file system, handling ordinary filesand 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 toestablish communication in UNIX operating system, How to use different filters? and also at the end youwill 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. Thosewho 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 thecomputer. 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 fromthis text, be sure to work through all of the exercises.

Page 7: BSIT 43 Content

VII

a

Contents

PART – A

Chapter 1

INTRODUCTION TO UNIX AND ITS BACKGROUND 1

1.1 The Operating system....................................................................... 11.2 The UNIX operating system.............................................................. 21.3 The origins of UNIX......................................................................... 21.4 How to work with UNIX ?: A Brief session........................................ 31.5 Architecture of UNIX....................................................................... 41.6 UNIX features.................................................................................. 51.7 Conclusion........................................................................................ 6

Exercises......................................................................................... 6

Chapter 2

UNDERSTANDING THE UNIX COMMANDS 7

2.1 Looking for a UNIX commands......................................................... 72.2 Internal and external commands......................................................... 72.3 Structure of UNIX Commands........................................................... 82.4 Flexibility of command usage............................................................. 82.5 Using manual pages on-line................................................................ 92.6 When things Go wrong...................................................................... 102.7 Conclusion........................................................................................ 10

Exercises......................................................................................... 10

Page 8: BSIT 43 Content

VIII

Chapter 3

GENERAL-PURPOSE UTILITIES 11

3.1 Displaying calendar: cal..................................................................... 113.2 To display system date and time: date................................................ 133.3 Displaying message: echo................................................................. 133.4 An alternative to echo: printf ............................................................. 143.5 The calculator: bc............................................................................. 143.6 Recording your session: script........................................................... 153.7 Changing your password: passwd...................................................... 163.8 Who are the users logged-in?: who.................................................... 173.9 Know your machine’s name: uname.................................................. 183.10 Knowing your terminal: tty ................................................................. 193.11 Displaying and setting terminal character: stty.................................... 193.12 Lock your terminal: lock................................................................... 203.13 Check your spellings: spell and ispell................................................... 213.14 Taming the cursor: tput ..................................................................... 233.15 Conclusion........................................................................................ 23

Exercises......................................................................................... 23

Chapter 4

THE FILE SYSTEM 25

4.1 Concept of a UNIX file..................................................................... 254.2 What’s in a file name?....................................................................... 264.3 The parent-child relationship.............................................................. 264.4 Checking your present working directory: pwd................................... 274.5 Changing the current directory: cd...................................................... 274.6 Creating directories: mkdir ................................................................ 284.7 Removing directories: rmdir .............................................................. 284.8 Absolute pathnames.......................................................................... 284.9 Relative pathnames........................................................................... 294.10 Listing directory contents: ls.............................................................. 304.11 The Unix file system......................................................................... 334.12 Conclusion........................................................................................ 34

Exercises......................................................................................... 34

Chapter 5

MANAGING ORDINARY FILES 35

5.1 Displaying and creating files: cat........................................................ 35

Page 9: BSIT 43 Content

IX

5.2 Copying the files: cp.......................................................................... 355.3 Deleting the files: rm ......................................................................... 365.4 Renaming the files: mv...................................................................... 375.5 Paging output: more......................................................................... 375.6 Printing a file: lp................................................................................ 385.7 Knowing file types: file...................................................................... 395.8 Counting lines, words, and characters: wc........................................... 395.9 Displaying a data in octal: od............................................................. 405.10 Splitting a file into multiple files: split................................................. 415.11 Comparing two files: cmp.................................................................. 425.12 Finding what is common: comm......................................................... 425.13 Converting one file to other: diff ......................................................... 435.14 Conclusion........................................................................................ 44

Exercises......................................................................................... 44

Chapter 6

BASIC FILE ATTRIBUTES 45

6.1 Listing file attributes: ls -l.................................................................. 45Listing directory attributes: The –d option.........................................

6.2 File permissions................................................................................. 466.3 Changing file permissions: chmod...................................................... 476.4 File system and inodes....................................................................... 506.5 Hard links......................................................................................... 506.6 Symbolic links................................................................................... 526.7 Default file and directory permissions: umask.................................... 526.8 Locating files: find ............................................................................ 546.9 Conclusion........................................................................................ 55

Exercises......................................................................................... 55

Chapter 7

THE VI EDITOR 57

7.1 The Basics of vi................................................................................ 577.2 The modes of vi................................................................................ 577.3 Navigation in command mode............................................................ 597.4 Adding and replacing text.................................................................. 607.5 Saving text........................................................................................ 607.6 Deleting text..................................................................................... 617.7 Pattern search.................................................................................. 617.8 Pattern search and Replace............................................................... 62

Page 10: BSIT 43 Content

X

7.9 Miscellaneous operators in command mode........................................ 627.10 Conclusion........................................................................................ 63

Exercises.......................................................................................... 63

PART – B

Chapter 8

THE SHELL 64

8.1 Common shells................................................................................. 648.2 The shell’s interpretive cycles............................................................ 648.3 Pattern matching – the wild-cards...................................................... 658.4 Escaping and quoting......................................................................... 678.5 Redirection: The three standard files.................................................. 678.6 /dev/null and /dev/tty: two special files................................................ 718.7 pipes................................................................................................ 718.8 Creating a tee: tee............................................................................ 728.9 Command substitution....................................................................... 738.10 Shell variables................................................................................... 738.11 Conclusion........................................................................................ 75

Exercises......................................................................................... 75

Chapter 9

THE PROCESS 77

9.1 Basics of process.............................................................................. 779.2 Process status: ps............................................................................. 799.3 System process................................................................................. 809.4 Mechanism of process creation.......................................................... 819.5 Running jobs in background................................................................ 829.6 Jobs execution with low priority: nice................................................. 839.7 Killing process with signals................................................................ 849.8 Execute later: at and batch............................................................... 849.9 Running jobs periodically: cron........................................................... 859.10 Timing processes: time...................................................................... 879.11 Conclusion........................................................................................ 87

Exercises......................................................................................... 87

Chapter 10

COMMUNICATION AND ELECTRONIC MAIL 89

Page 11: BSIT 43 Content

XI

10.1 Write and talk.................................................................................... 8910.2 Refusing and accepting messages...................................................... 9010.3 E-mail basics.................................................................................... 9010.4 The universal mailer: mailx............................................................... 9110.5 A full-screen mail program: pine....................................................... 9310.6 Conclusion........................................................................................ 96

Exercises......................................................................................... 96

Chapter 11

SIMPLE FILTERS 98

11.1 The sample database......................................................................... 9811.2 Paginating files: pr ............................................................................ 9911.3 Displaying the beginning of file: head................................................. 9911.4 Displaying the end of file: tail ............................................................ 10011.5 Slitting a file vertically: cut................................................................. 10011.6 Pasting files: paste............................................................................ 10211.7 Ordering a file: sort.......................................................................... 10311.8 Locating repeated and non repeated lines: uniq................................... 10611.9 Translating characters: tr ................................................................... 10711.10 Searching for a pattern: grep............................................................. 10811.11 Conclusion........................................................................................ 110

Exercises.......................................................................................... 110

Chapter 12

SHELL PROGRAMMING 111

12.1 A simple shell script.......................................................................... 11112.2 Subshells.......................................................................................... 11212.3 The shell as a programming language................................................. 11312.4 Variables.......................................................................................... 11312.5 Input using the read statement........................................................... 11512.6 The set command............................................................................. 11512.7 Arithmetic operations using the expr utility......................................... 11612.8 Control structures.............................................................................. 11712.9 The if statement and test command.................................................... 11812.10The elif and else statements............................................................... 11912.11 The case statement........................................................................... 120

Page 12: BSIT 43 Content

XII

12.12 for loops........................................................................................... 12112.13 while loops....................................................................................... 12112.14 until loops......................................................................................... 12212.15 Conclusion........................................................................................ 123

Exercises......................................................................................... 123

Chapter 13

DEVELOPING SHELL SCRIPTS 124

13.1 Creating executable file..................................................................... 12413.2 Assigning Labels to wc command...................................................... 12513.3 Deleting files safely........................................................................... 12713.4 A Daily Reminder System: tickle........................................................ 12913.5 Displaying arguments multiple times................................................... 13013.6 Exercises.......................................................................................... 132