bsit 43 content raghu

12

Click here to load reader

Upload: warrior432

Post on 11-Apr-2015

62 views

Category:

Documents


1 download

DESCRIPTION

content of bscit 4th sem unix book

TRANSCRIPT

Page 1: BSIT 43 Content Raghu

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

Incol laboration

wi th

KUVEMPU UNIVERSITY

Directorate of Distance EducationKuvempu University

Shankaraghatta, Shimoga District, Karnataka

Universal Education TrustBangalore

B.Sc.(IT) - 4th Semester

BSIT - 43 Unix & Shell Programming

Page 2: BSIT 43 Content Raghu

II

Titles in this Volume :BSIT - 43 Unix & Shell Programming

Prepared by UNIVERSAL EDUCATION TRUST (UET)Bangalore

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

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 :Pragathi Print CommunicationsBangalore - 20Ph : 080-23340100

Page 3: BSIT 43 Content Raghu

III

UNIX AND SHELL PROGRAMMING(BSIT - 43)

: Contributing Authors :

Smt. Bhuvaneshwari K VLecturer, Department of IS&E

BIET, Davanagere

and

Dr. Shreedhara K SProfessor, Department of CS&E

University BDT College of EngineeringDavanagere

Page 4: BSIT 43 Content Raghu

IV

Page 5: BSIT 43 Content Raghu

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,everymajor 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 UNIXoperating system but a smart programmer uses this environment and its tools to turn out programsfaster.

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 lastrelease 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 user’s standpoint, these versions of UNIX are quite similar. Most can trace theirancestry to either AT&T UNIX or Berkeley UNIX; some are amalgams of both. This book presentsfeatures that are found on almost all UNIX systems, with special emphasis on those that arecommon 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

Page 6: BSIT 43 Content Raghu

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 thebackground 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 editingfeatures 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 alsoat 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 ofthe 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 benefitfrom this text, be sure to work through all of the exercises.

Page 7: BSIT 43 Content Raghu

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........................................................................................ 5

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

Chapter 2

UNDERSTANDING THE UNIX COMMANDS 7

2.1 Looking for a UNIX commands......................................................... 72.2 Internal and external commands......................................................... 82.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........................................................................................ 11

Exercises......................................................................................... 11

Page 8: BSIT 43 Content Raghu

VIII

Chapter 3

GENERAL-PURPOSE UTILITIES 12

3.1 Displaying calendar: cal..................................................................... 123.2 To display system date and time: date................................................ 143.3 Displaying message: echo................................................................. 153.4 An alternative to echo: printf............................................................. 153.5 The calculator: bc............................................................................. 163.6 Recording your session: script........................................................... 173.7 Changing your password: passwd...................................................... 183.8 Who are the users logged-in?: who.................................................... 193.9 Know your machine’s name: uname.................................................. 203.10 Knowing your terminal: tty................................................................. 213.11 Displaying and setting terminal character: stty.................................... 213.12 Lock your terminal: lock................................................................... 233.13 Check your spellings: spell and ispell................................................... 233.14 Taming the cursor: tput..................................................................... 253.15 Conclusion........................................................................................ 26

Exercises......................................................................................... 26

Chapter 4

THE FILE SYSTEM 27

4.1 Concept of a UNIX file..................................................................... 274.2 What’s in a file name?....................................................................... 284.3 The parent-child relationship.............................................................. 284.4 Checking your present working directory: pwd................................... 294.5 Changing the current directory: cd...................................................... 304.6 Creating directories: mkdir................................................................ 304.7 Removing directories: rmdir.............................................................. 314.8 Absolute pathnames.......................................................................... 314.9 Relative pathnames........................................................................... 324.10 Listing directory contents: ls.............................................................. 324.11 The Unix file system......................................................................... 364.12 Conclusion........................................................................................ 37

Exercises......................................................................................... 37

Chapter 5

MANAGING ORDINARY FILES 38

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

Page 9: BSIT 43 Content Raghu

IX

5.2 Copying the files: cp.......................................................................... 385.3 Deleting the files: rm......................................................................... 395.4 Renaming the files: mv...................................................................... 405.5 Paging output: more......................................................................... 405.6 Printing a file: lp................................................................................ 425.7 Knowing file types: file...................................................................... 435.8 Counting lines, words, and characters: wc........................................... 435.9 Displaying a data in octal: od............................................................. 445.10 Splitting a file into multiple files: split................................................. 455.11 Comparing two files: cmp.................................................................. 465.12 Finding what is common: comm......................................................... 475.13 Converting one file to other: diff......................................................... 475.14 Conclusion........................................................................................ 48

Exercises......................................................................................... 48

Chapter 6

BASIC FILE ATTRIBUTES 49

6.1 Listing file attributes: ls -l.................................................................. 496.2 File permissions................................................................................. 506.3 Changing file permissions: chmod...................................................... 516.4 File system and inodes....................................................................... 546.5 Hard links and Symbolic links............................................................. 54

6.5.1. Hard links................................................................................ 546.5.2. Symbolic links.......................................................................... 56

6.6 Default file and directory permissions: umask.................................... 576.7 Locating files: find............................................................................ 586.8 Conclusion........................................................................................ 60

Exercises......................................................................................... 60

Chapter 7

THE VI EDITOR 61

7.1 The Basics of vi................................................................................ 617.2 The modes of vi................................................................................ 617.3 Inserting and Replacing Text.............................................................. 627.4 Navigation in command mode............................................................ 637.5 Saving text........................................................................................ 647.6 Deleting text..................................................................................... 657.7 Pattern search.................................................................................. 65

Page 10: BSIT 43 Content Raghu

X

7.8 Pattern search and Replace............................................................... 667.9 Miscellaneous operators in command mode........................................ 667.10 Conclusion........................................................................................ 67

Exercises.......................................................................................... 67

Chapter 8

THE SHELL 68

8.1 The shell’s interpretive cycles............................................................ 688.2 Common shells................................................................................. 688.3 Pattern matching – the wild-cards...................................................... 698.4 Escaping and quoting......................................................................... 708.5 Redirection: The three standard files.................................................. 718.6 /dev/null and /dev/tty: two special files................................................ 748.7 pipes................................................................................................ 758.8 Creating a tee: tee............................................................................ 768.9 Command substitution....................................................................... 778.10 Shell variables................................................................................... 778.11 Conclusion........................................................................................ 79

Exercises......................................................................................... 79

Chapter 9

THE PROCESS 80

9.1 Basics of process.............................................................................. 809.2 Process status: ps............................................................................. 829.3 System process................................................................................. 839.4 Mechanism of process creation.......................................................... 849.5 Running jobs in background................................................................ 859.6 Jobs execution with low priority: nice................................................. 869.7 Killing process with signals................................................................ 879.8 Execute later: at and batch............................................................... 889.9 Running jobs periodically: cron........................................................... 899.10 Timing processes: time...................................................................... 909.11 Conclusion........................................................................................ 91

Exercises......................................................................................... 91

Chapter 10

COMMUNICATION AND ELECTRONIC MAIL 92

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

Page 11: BSIT 43 Content Raghu

XI

10.2 Refusing and accepting messages...................................................... 9310.3 E-mail basics.................................................................................... 9310.4 The universal mailer: mailx............................................................... 9410.5 A full-screen mail program: pine....................................................... 9610.6 Details of Users: finger..................................................................... 9910.7 Conclusion........................................................................................ 99

Exercises......................................................................................... 100

Chapter 11

SIMPLE FILTERS 101

11.1 The sample database......................................................................... 10111.2 Paginating files: pr............................................................................ 10211.3 Displaying the beginning of file: head................................................. 10211.4 Displaying the end of file: tail............................................................ 10311.5 Slitting a file vertically: cut................................................................. 103

11.5.1. Cutting Columns (-c)........................................................... 10411.5.2. Cutting Fields (-f)................................................................ 104

11.6 Pasting files: paste............................................................................ 10511.7 Ordering a file: sort.......................................................................... 107

11.7.1. Sort Options.......................................................................... 10711.8 Locating repeated and non repeated lines: uniq................................... 10911.9 Translating characters: tr................................................................... 11011.10 Searching for a pattern: grep............................................................. 111

11.10.1 grep options........................................................................ 11211.11 Conclusion........................................................................................ 113

Exercises.......................................................................................... 113

Chapter 12

SHELL PROGRAMMING 114

12.1 A simple shell script.......................................................................... 11412.2 Subshells.......................................................................................... 11512.3 The shell as a programming language................................................. 11612.4 Variables.......................................................................................... 11612.5 Input using the read statement........................................................... 11812.6 The set command............................................................................. 11912.7 Arithmetic operations using the expr utility......................................... 120

Page 12: BSIT 43 Content Raghu

XII

12.8 Control structures.............................................................................. 12112.9 The if statement and test command.................................................... 12212.10 The elif and else statements............................................................... 12212.11 The case statement........................................................................... 12312.12 for loops........................................................................................... 12412.13 while loops....................................................................................... 12512.14 until loops......................................................................................... 12512.15 Conclusion........................................................................................ 126

Exercises......................................................................................... 126

Chapter 13

DEVELOPING SHELL SCRIPTS 127

13.1 Creating executable file..................................................................... 12713.2 Assigning Labels to wc (Word Count) command: mywc....................... 12813.3 Deleting files safely:del...................................................................... 13013.4 A Daily Reminder System: tickle....................................................... 13113.5 Displaying arguments multiple times:echo.sh...................................... 13213.6 Exercises.......................................................................................... 134