Você está na página 1de 146

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 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......................................................................................... 1 1 2 2 3 4 5 5 6

Chapter 2 UNDERSTANDING THE UNIX COMMANDS 2.1 2.2 2.3 2.4 2.5 2.6 2.7 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......................................................................................... 7 7 8 8 8 9 10 11 11

VIII
Chapter 3 GENERAL-PURPOSE UTILITIES 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 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......................................................................................... 12 12 14 15 15 16 17 18 19 20 21 21 23 23 25 26 26

Chapter 4 THE FILE SYSTEM 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 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......................................................................................... 27 27 28 28 29 30 30 31 31 32 32 36 37 37

Chapter 5 MANAGING ORDINARY FILES 5.1 Displaying and creating files: cat........................................................ 38 38

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 6.1 6.2 6.3 6.4 6.5 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......................................................................................... 49 49 50 51 54 54 54 56 57 58 60 60

6.6 6.7 6.8

Chapter 7 THE VI EDITOR 7.1 7.2 7.3 7.4 7.5 7.6 7.7 The Basics of vi................................................................................ The modes of vi................................................................................ Inserting and Replacing Text.............................................................. Navigation in command mode............................................................ Saving text........................................................................................ Deleting text..................................................................................... Pattern search.................................................................................. 61 61 61 62 63 64 65 65

X
7.8 Pattern search and Replace............................................................... 7.9 Miscellaneous operators in command mode........................................ 7.10 Conclusion........................................................................................ Exercises.......................................................................................... Chapter 8 THE SHELL 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 8.11 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......................................................................................... 68 68 68 69 70 71 74 75 76 77 77 79 79 66 66 67 67

Chapter 9 THE PROCESS 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 9.11 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......................................................................................... 80 80 82 83 84 85 86 87 88 89 90 91 91

Chapter 10 COMMUNICATION AND ELECTRONIC MAIL 10.1 Write and talk.................................................................................... 92 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 11.1 11.2 11.3 11.4 11.5 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.......................................................................................... 101 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

Chapter 12 SHELL PROGRAMMING 12.1 12.2 12.3 12.4 12.5 12.6 12.7 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......................................... 114 114 115 116 116 118 119 120

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 13.1 13.2 13.3 13.4 13.5 13.6 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.......................................................................................... 127 127 128 130 131 132 134

Chapter 1

Introduction to UNIX and its Background

1.1 THE OPERATING SYSTEM

n operating system (OS) is a collection of programs used by a computer to manage its own operations and resources. To control the hardware and allow applications to do something useful with it, the computer loads a master program at startup (boot) time called the operating system.A typical operating system gives users a way to create and manage files as well as run application programs. Operating systems perform basic tasks, such as recognizing input from the keyboard, sending output to the display screen, keeping track of files and directories on the disk, and controlling peripheral devices such as disk drives and printers. For large systems, the operating system makes sure that different programs and users running at the same time do not interfere with each other. The operating system is also responsible for security, ensuring that unauthorized users do not access the system. Operating systems can be classified as follows: multi-user : Allows two or more users to run programs at the same time. Some operating systems permit hundreds or even thousands of concurrent users. multiprocessing : Supports running a program on more than one CPU. multitasking : Allows more than one program to run at the same time. multithreading : Allows different parts of a single program to run simultaneously real time: Responds to input instantly. General-purpose operating systems, such as DOS and Unix, are not real-time.

Chapter 1 - Introduction to UNIX and its Background

Chapter 1 - Introduction to UNIX and its Background

1.2 THE UNIX OPERATING SYSTEM


Unix is an increasingly popular operating system. Traditionally used on minicomputers and workstations in the academic community, Unix is now available on personal computers, and the business community is choosing Unix for its openness. Previous PC and mainframe users are now looking to Unix as their operating system solution. Due to the popularity of personal computers, there are many versions of Unix available for Intel platforms. Choosing from the versions and trying to find software for the version chosen can be a tricky business because the Unix industry has not settled on a complete binary standard for the Intel platform. There are two basic categories of UNIX systems on Intel hardware, the SVR4-based systems and the older, more established SVR3.2 systems. SVR4 vendors include NCR, IBM, Sequent, SunSoft (which sells Solaris for Intel), and Novell (which sells UnixWare). The Santa Cruz Operation (SCO) is a major vendor for the SVR3.2 system. Efforts are being made to clean up the standards to gain full shrink-wrap portability between their versions of UNIX. This will make UNIX-on-Intel applications available, shrinkwrapped for any version of UNIX, similar to the availability of applications for MS-DOS or Microsoft Windows. SCO UNIX currently has a much larger base of available applications and is working to achieve binary compatibility with UnixWare. Several versions of Unix and Unix-like systems have been made that are free or very cheap and include source code. These versions are useful to the modern-day hobbyist, who can now run a Unixsystem at home for little investment and an opportunity to experiment with the operating system or make changes to suit particular needs. A popular source version of Unix is Linux. Linux was designed from the ground up by Linus Torvalds to be a free replacement for UNIX, and it aims for POSIX compliance. Efforts are on to make Linux reliably run both SVR3.2 and SVR4 binaries.

1.3 THE ORIGINS OF UNIX


In the mid-1960s, among others, AT&T Bell Laboratories was participating in an effort to develop a new operating system called Multics. Multics was intended to supply large-scale computing services as a utility. In 1969, Bell Labs withdrew from the Multics project. Ken Thompson, Dennis Ritchie then designed and built a small multitasking system supporting two users. This new system had an elegant file system,a command interpreter and set of utilities. A member of the team Brian Kerninghan suggested the name UNICS ( Uniplexed Information and Computing System) for this operating system. In 1970 UNICS finally became the name by which its known today UNIX. In 1973, C programming language was developed on and for Unix, and the Unix operating system itself was rewritten into C. This enabled Unix to become the open portable system it is today.

BSIT 43 Unix and Shell Programming

AT&T was not allowed to market computer systems and Bell Labs. could not sell Unix. Nevertheless, it became more and more popular through internal use at AT&T and licenses were given to universities for educational use. By 1977 commercial licenses for Unix were granted, and Unix systems were sold for office automation. Later versions developed at AT&T included System III and several releases of System V. The two most recent releases of System V, Release 3 (SVR3.2) and Release 4 (SVR4; the most recent version of SVR4 is SVR4.2) remain popular for computers ranging from PCs to mainframes. All versions of UNIX based on the AT&T work require a license from the present owner, UNIX System Laboratories. While AT&T through its research group, developed Unix, the universities that had acquired educational licenses were coming out with their own releases. . Most notably, the Computer Science Research Group at the University of California at Berkeley (UCB) developed a series of releases known as the Berkeley Software Distribution, or BSD. Because of the multiple versions of UNIX and frequent cross-pollination between variants, many features have diverged in the different versions of UNIX. With the increasing popularity of UNIX in the commercial and government sector there was a need to standardize the features of UNIX so that a user or developer using UNIX could depend on those features. The Institute of Electrical and Electronic Engineers (IEEE) created a series of standards committees to create standards for An Industry-Recognized Operating Systems Interface Standard based on the UNIX Operating System. Of these, the standards set up by two of the committees are important for the general user and developer. The POSIX.1 committee standardizes the C library interface used to write programs for UNIX and the POSIX.2 committee standardizes the commands that are available for the general user.

1.4 HOW TO WORK WITH UNIX? A BRIEF SESSION


Every user has to login with a username and password. Because the system can be used by many users, someone has to be given the charge of administration of the system. This person is known as system administrator. The system administrator is responsible for creating user accounts. While assigning user accounts, the system administrator takes care to see that:

The account name is atleast three characters long and not more than eight characters long. No two accounts have the same name. The account name chosen is preferably related to the users name. Account names have only lower case letters and numbers, uppercase letters and punctuations should be avoided.

Chapter 1 - Introduction to UNIX and its Background

The system administrator also gives the account an initial password, which can be later changed by the user. To start a Unix session, the user has to login using the account name. The account name is also referred to as the login name or login. When a user logs into a system, the password has to entered to prevent unauthorized access. The system maintains a password file that contains one entry for each user (login, ID, encrypted password, home directory, ). When a user logs in, password is encrypted and is compared with the encrypted password in the password file. A login prompt on a Unix terminal indicates that the terminal is ready for a user to login. The user enters a login name and presses [enter]. If the username successfully matched then system gives the output password prompt. Now the system request to enter the password. The password entered is not visible. This is a security feature in Unix to ensure that the password remains known only to the user. If the login name does not exist or the password entered is incorrect then the system displays a login incorrect message. When both the login name and password are correct, the system displays the Unix prompt ($) and the cursor waits beside the prompt for the user to enter a command. login: username Password:

1.5 ARCHITECTURE OF UNIX


UNIX, like other operating systems, is a layer between the hardware and the applications that run on the computer. It has functions that manage the hardware and functions that manage executing applications. So whats the difference between UNIX and any other operating system? Basically two things: internal implementation and the interface that is seen and used by users. The functioning of UNIX is managed in three levels. On the outer crust reside the application programs and other utilities. At the heart of UNIX, on the other hand is the kernel, which interacts with the actual hardware in machine language. The streamlining of these two modes of communication is done by the middle layer called the shell. The part of UNIX operating system which is responsible for the creation and management of files and process is called the kernel. It is also a collection of programs written in C which directly communicate with the hardware. Application programs (user programs) communicate with the hardware by using the services of the kernel. Along with the memory management, the kernel also schedules processes and decides their priorities. The user commands are translated into action by the shell which acts as an interpreter. The shell forms the outer part of the operating system and forms the interface between the user and the kernel. For each user logged in, there is shell in action. When a command is given by the user, it is examined by the

BSIT 43 Unix and Shell Programming

shell and communicated to the kernel for execution. The functions used to communicate with the kernel are called system calls and are built into the kernel. The system calls are in all the flavors of UNIX are the same. Hence, software developed on one UNIX system can be easily run on another UNIX system.

Application portability is the ability of a single application to be executed on various types of computer hardware without being modified. This can be achieved if the application uses the UNIX interface to manage its hardware needs. UNIXs layered design insulates the application from the different types of hardware. This allows the software developer to support the single application on multiple hardware types with minimal effort. The application writer has lower development costs and a larger potential customer base. Users not only have more applications available, but can rely on being able to use the same applications on different computer hardware.

6 1.6 UNIX FEATURES

Chapter 1 - Introduction to UNIX and its Background

The following are the some features of UNIX operating system:

UNIX is a multiuser operating system where the memory, CPU and hard disk are shared between multiple users. UNIX goes beyond the traditional operating system by providing a standard set of libraries and applications that developers and users can use. This standard interface allows application portability and facilitates user familiarity with the interface. The UNIX system is supported by the file and the process. Directories and devices are treated as files and there are many text manipulation tools to edit these files. When a file is executed as a program, it is called a process. There are tools to control processes like sending a processes into background or even terminating it. Unix is a multitasking operating system, where a single user can also run multiple tasks concurrently at the same time. By going quickly from one task to another and performing a little bit of each task every time, the operating system gives an impression of doing many things at the same time. Unix uses this technique of time-sharing. Unix is also able to interact with more than one user at a time. This feature of Unix makes it a multitasking system.

1.7 CONCLUSION
Unix is an open system and has been adapted to run on a wide variety of computers. It is a portable operating system, which can run on everything from personal computers to mainframes more than a billion times as powerful as the first system.

EXERCISES
1. 2. 3. 4. 5. 6. Why does a computer require an operating system? How is Unix different from other operating systems ? What is the function of the Unix kernel ? What is the meaning of multitasking? What is the importance of a user name and a password ? What are the features of UNIX operating system.

Chapter 2

Understanding the UNIX Commands

2.1 LOOKING FOR A UNIX COMMANDS


ost of the Unix command names are short, single words in lowercase. The commands are basically programs written in C. Traditionally, Unix system programs are stored in directories called /bin and /usr/bin, with additional programs usually used only by system administrators in /etc and /usr/etc. Many versions of Unix also have programs stored in /usr/ucb There may be other directories containing programs. Users may also have their own directories where custom commands and scripts are kept, such as /usr/local/bin. A command can be located by using the type command. For example to get the location of the cat command in the Unix system, the following command is entered$ type cat cat is /bin/cat When the cat command is entered, the shell searches for the command in the search path. The search path is stored in an environment variable called PATH. The path is searched in order, so if there are two commands with the same name, the one that is found first in the path will be executed. The search path isnt built into the shell; it is specified in the shell setup files Example $ echo $PATH /bin:/usr/bin:/college1/sibns/bin:. There are four directories in this list. Each directory list is seperated by colon.Note that this list includes the current directory indicating by a singular dot at the end.

BSIT 43 Unix and Shell Programming

Chapter 2 - Understanding the UNIX Commands

2.2 INTERNAL AND EXTERNAL COMMANDS


Some commands in Unix are internal, built into the shell. For example, the echo command is built-in. Example: $type echo echo is a shell built-in That is, the shell interprets the command and changes the current directory. On the other hand, cat command is an external program stored in the file /bin/cat. The shell doesnt start a separate process to run internal commands. External commands require the shell to run a new subprocess; this takes some time, especially if the system is busy.

2.3 STRUCTURE OF UNIX COMMANDS


The basic structure of a Unix command is: command [options,...] [arguments,...] where command is the name of the command. Command names are typically very short, and are often cryptic in their meaning. The options modify the action of the command and are generally single characters. There may be no options or there may be several acting on the same command. Options are generally preceded by a hyphen (-) character. Grouping of options have no consistent rule and some commands allow a list of options with just a single hyphen at the beginning of the list. Some other commands may require a hyphen before each option. There are some options which allow a value, maybe a filename, to be given following the option. The value may be placed immediately following the option letter or a space may be required between the option letter and the value. This depends on the options. The arguments are values upon which the command is to operate. These are very often filenames, and depending on the command there may be several arguments or none. Unix being case-sensitive, the exact combination of upper and lower case letters used in a command, option or filename are important.

2.4 FLEXIBILITY OF COMMAND USAGE


1. A number of commands can be entered in a single command line. The commands are separated

BSIT 43 Unix and Shell Programming

by semicolons (;). When the shell finds a semicolon in the command line, it executes the commands on either side of the semicolon separately. The semicolon in this context is called a metacharacter because it has a special meaning to the shell. Example: $wc note;ls l note

In this example two commands wc and ls entered in a Same command line using semicolon.

2. Long command lines can be split and entered in several lines. The shell recognizes an incomplete command and displays a secondary prompt (>). Example: $echo This is an > UNIX operating > System

2.5 USING MANUAL PAGES ON-LINE


Online manual pages are available for most commands and applications on the system. Using the man command followed by the name of the command accesses the man page for a command. For example, to find out more about how to use man the following command is typed $ man man To find out more about the ls command, the following command can be used: $ man ls The manual pages are displayed one screenful at a time with a pause after each page. The man command uses the pager called more to do this. Other pagers can also be used by setting it in the PAGER variable. The pager used, controls the man pages display and at the end of each page, a prompt (colon) is displayed. Keys can be pressed to navigate or search the pages. Typing q at the prompt quits man and returns the user to the shell. The following keys are used for navigating and searching:

key f or spacebar b /

Action displays the next page displays the previous page Searches for the string following the /

There is also a limited help facility built into the man command for searching subject lines by keyword When the k option is used, man searches a summary database for the argument and displays all the lines

10

Chapter 2 - Understanding the UNIX Commands

that contain the argument. For example, to get a one-line description of the command uname, the following command can be given: $ man -k uname uname (C) - print the name of the operating system

uucp, uulog, uuname (C) - UNIX-to-UNIX system copy uuname (C) uname (C) - list names of systems known to uucp - print the name of the operating system

uucp, uulog, uuname (C) - UNIX-to-UNIX system copy uuname (C) - list names of systems known to uucp

Note: C is a manual section indicates operating system commands, utilities available to all users

2.6 WHEN THINGS GO WRONG


All terminals and keyboards are not similar and the keyboard operation depends on the terminal settings. The terminal settings are found in the TERM variable. So when commands do not work as expected, the following keystrokes can be tried.

Keys to Press [Ctrl+h] [Ctrl+d] [Ctrl+s] [Ctrl+q] [Ctrl+u] [Ctrl+\] [Ctrl+z] [Ctrl+j]or Ctrl+m]

Functions

Erases text Terminates a login session Stops scrolling of screen output Resumes scrolling of screen output Kills a command line without executing it Kills a command being executed creates a core file Suspends a command execution & returns to shell prompt Same as enter

[Ctrl+c]or [Delete] Interrupts a command

BSIT 43 Unix and Shell Programming

11

2.7 CONCLUSION
In this chapter, we learned about Unix commands and how they can be used with arguments and options. The on-line documentation available in the Unix system is helpful in looking up commands and their descriptions.

EXERCISES
1. 2. 3. 4. 5. 6. 7. How are commands located in Unix ? How does the type command work ? What is the structure of UNIX command? How do you find out whether a command is internal or external ? What is a secondary prompt ? How do you stop and resume the scrolling of the screen display ? How do you get one-line descriptions of any command in Unix?

Chapter 3

General-purpose Utilities

3.1 DISPLAYING CALENDAR: cal


cal - print a calendar
Syntax: cal [ [ month ] year ]

The argument for the cal command is either month or year, where year is a number between 1 and 9999 and month is a number between 1 and 12 . Example 1: No arguments are specified The current, previous, and following months are printed, along with the current date and time. $ cal

12

Chapter 3 - General-Purpose Utilities

BSIT 43 Unix and Shell Programming

13

Example 2: Only month is specified $ cal jan

$cal 1

Example 3: Calendar for the year 2005 is displayed $ cal 2005

14

Chapter 3 - General-Purpose Utilities

3.2 TO DISPLAY SYSTEM DATE AND TIME: date


date - to see the date and time
Syntax: date [ +format ]

The format specifier is preceded by a + followed by a %. Some of the format specifiers are as follows:

Format

What it specifies

+%m +%h +%B +%d +%y +%H, +%M and +%S +%D +%T

Specifies the month number (1 to 12) Specifies the abbreviated month (Jan Dec) Specifies the full month name Specifies the day of the month (1 to 31) Specifies the year (2 digits) Specify the hour, minute and seconds Specifies the date as mm/dd/yy Specifies the time as hh:mm:ss

Example 1: without arguments, date displays the current date and time $ date Thu Dec 16 13:25:00 IST 2004 Example 2: $ date +%D 12/27/04 Example 3: $ date +%d %h %y 27 Dec 04

BSIT 43 Unix and Shell Programming

15

Example 4: $date +%H%M%S 13 25 00 Example 5: $date +%T 13:25:00

3.3 DISPLAYING MESSAGE: echo


echo - writes its arguments separated by blanks and terminated by a new line on the standard output. Syntax: echo [ -n] text The -n option prints a line without the newline. Example: $ echo Hello Hello

3.4 AN ALTERNATIVE TO ECHO: printf


printf-Write formatted output
Syntax: printf format [ argument]

printf formats the given arguments according to the format string and writes them to the standard output. Example 1: Returns hello to the screen. $ printf hello hello Example 2: Using a format string $ printf %s %0.2f \n Rupees 550 Rupees 550.00

16 3.5 THE CALCULATOR: bc


bc- invoke a calculator

Chapter 3 - General-Purpose Utilities

Syntax: bc It is an interactive tool for performing calculator-like computations. It is commonly used in computation with large integers, computations accurate to many decimal places and conversions of numbers from one base to another base.

Simple arithmetic expressions can be formed with operators that include: +-*/%^ i.e., addition, subtraction, multiplication, division, modulo (remainder) and exponentiation. Division by zero produces an error message. In more complex expressions with several operators and with parentheses exponentiation (^) performed first, then multiplication (*), division (/), modulo (%), and finally, addition (+), and subtraction (-). The contents of parentheses are evaluated before expressions outside the parentheses. All of the above operations are performed from left to right, except exponentiation, which is performed from right to left. When invoked, bc expects input from the keyboard. An arithmetic expression is typed in and bcdisplays the result. Ctrl-d is used to exit bc. Example 1: $ bc 10+20 30 Ctrl-d type bc at the command prompt and press ENTER type an arithmetic expression and press ENTER the answer is displayed press Ctrl-d to exit bc and return to the Unix prompt

While computing, bc truncates the decimal portion in the result. To enable floating-point computation, the number of digits of precision is set. Example 2: Enable floating point computation $ bc scale=5 10/3 3.33333 Ctrl-d specify the number of digits after the decimal point using scale

BSIT 43 Unix and Shell Programming

17

Example 3: Converting numbers from one base to another base. $bc ibase=2 11001010 202 obase=2 15 1111 Ctrl-d Example 4: Used with variable $ bc x=3;y=4;z=5 p=x+y+z p 12 Ctrl-d set output base as binary set input base as binary

3.6 RECORDING YOUR SESSION: script


script - makes a typescript of a terminal session

Syntax: script[-a][file] Example: Start a script session. If no filename is given, script will use the default filename typescript. $script script started, file is typescript $-

18

Chapter 3 - General-Purpose Utilities

Append activities to an existing file typescript use the following: $ script a We can also specify the file name as a argument for the script command as follows: To Start a script session and record terminal session to the file scriptfile $ script scriptfile Start a script session append to an existing scriptfile $ script a scriptfile To exits a script session use as follows: $ exit script done, file is typescript The contents of the script file can be viewed using cat after ending a script session.

3.7 CHANGING YOUR PASSWORD: passwd


passwd - change login password The passwd command is used to change a login password. The login password is one of the most important defenses against security breaches. A password should be such that it is easily remembered by the user and difficult to guess by others. The general guidelines to form passwords are:

A password should be atleast six characters long. Passwords can be combinations of numbers, upper-case and lower-case letters, and punctuations Obvious passwords like common names, words from the dictionary, names of places, etc. should not be used There should be a significant difference between the old password and the new password It is advisable to change the password frequently to prevent unauthorized access to a login account.

Syntax: passwd

The system will prompt the user for the old password. Once this is entered, the new password is requested. The system once again prompts the user to retype the new password, to make sure that there

BSIT 43 Unix and Shell Programming

19

is no mistake in the entry. If the new passwords do not match or the old password is not entered correctly, the password change will be rejected otherwise. It is to be noted that, whenever passwords are entered, they are not visible. Example: $ passwd Setting password for user: usr1 Old password: New password: Re-enter password:

3.8 WHO ARE THE USERS LOGGED-IN? : who


who - list the users logged on the system Syntax: who Examples: 1. Lists the users name, terminal line and login date and time $ who sibns fysr jcepay ttyp0 ttyp1 ttyp2 Dec 29 13:08 Dec 29 13:51 Dec 29 13:54

2. who command can be used with arguments am and I displays a login details of the user who invoked this command. $ who am i sibns ttyp0 Dec 29 13:08 3. Lists only the names and number of users logged in

20
$ who -q sibns fysr jcepay # users=3 4. More user details with headers $ who Hu NAME LINE sibns fysr jcepay ttyp0 ttyp1 ttyp2 TIME Dec 29 13:08 Dec 29 13:51 Dec 29 13:54 IDLE . 0:20 0:25

Chapter 3 - General-Purpose Utilities

PID COMMENTS 34 35 36

3.9 KNOW YOUR MACHINES NAME: uname


uname - displays the name of the operating system Syntax: uname [options]

Examples: 1. Displays all the information $ uname -a SCO_SV sco5 3.2 5.0.5 i386 2. Displays the machines node name in the communications network. $uname -n sco5 3. Displays the operating system release. $ uname -r 3.2 4. Displays the name of the operating system $ uname -s SCO_SV

BSIT 43 Unix and Shell Programming

21

5. Displays the operating system version $ uname -v 5.0.5 6. Displays the information about system name, node name, operating system release number, kernel ID,processor type, bus type, serial number, number of users license, OEM number, origin number and number of CPUs. $ uname -X System = SCO_SV Node = sco5 Release = 3.2v5.0.5 KernelID = 98/07/02 Machine = PentII BusType = ISA Serial = 5GE048977 Users = 5-user OEM# = 0 Origin# = 1 NumCPU = 1

3.10 KNOWING YOUR TERMINAL: tty


tty(teletype) display the terminals name Syntax: tty Example The terminal file name which is present in the /dev directory is displayed $ tty /dev/ttyp0

3.11 DISPLAYING AND SETTING TERMINAL CHARACTER:stty


stty - display and set terminal modes

22
Syntax: stty [options] Examples:

Chapter 3 - General-Purpose Utilities

1. Displays all the terminal settings like the speed of the terminal (baud rate), interrupt character, erase character, end-of-file character, etc. $ stty -a speed 38400 baud; ispeed 38400 baud; ospeed 38400 baud; line = 0(tty); intr = DEL; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = ^@; swtch = <undef>; susp = <undef>; start = ^Q; stop = ^S; -parenb -parodd cs8 -cstopb hupcl cread -clocal -loblk -ortsfl -ctsflow -rtsflow -ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc ixon ixany -ixoff isig icanon -xcase echo -echoe echok -echonl -noflsh -iexten -tostop -xclude opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel -isscancode 2. To turn off keyboard input $ stty echo 3. To restore keyboard input $ stty echo 4. To erase character while doing a backspace $ stty echoe 5. To reverse the above setting and do a backspace without erasing the character $stty echoe 6. To change the interrupt character from [delete] to ^c. To use a control character in an stty setting, a literal caret is preceded by a backslash. $ stty intr \^c 7. To set the terminal characteristics to work with most of the terminals $ stty sane

BSIT 43 Unix and Shell Programming

23

3.12 LOCK YOUR TERMINAL: lock


lock lock a users terminal Syntax: lock [ -v ] [ -number ] The lock command requests a password from the user, requests it again for verification, then locks the terminal until the password is reentered. If a number is specified in the lock command, the terminal is automatically logged out and made available to another user after that number of minutes has passed. Examples: 1. Lock the terminal $ lock Password:******* Re-enter password: ******* Terminal locked by usr 0 minutes ago The $ prompt disappears and the system locked in this condition for 30 minutes. If the user are not come back by that time, then it will logout. Any time before that user can unlock the terminal simply by reentering the same password. ******* 2. The -v option specifies verbose operation. $lock v Password: Re-enter password: Terminal locked by usr 0 minutes ago 3. The terminal is logged out after 10 minutes. $ lock 10 Password: Re-enter password: Terminal locked by usr 0 minutes ago

3.13 CHECK YOUR SPELLINGS: spell and ispell


spell - find spelling errors

24
Syntax: spell [ file ]

Chapter 3 - General-Purpose Utilities

where file is the name of the file to be checked; if no file name is given, input is take from the keyboard. Spell collects words from the named file and looks them up in a spelling list. Words that neither occur among nor are derivable (by applying certain inflections, prefixes, and/or suffixes) from words in the spelling list are printed on the standard output. If no file is named, words are collected from the standard input. Example: List the misspelt words in the file infile $ spell infile ispell - find spelling errors Syntax: ispell filename where filename is the name of the file to be checked. The spell checking program, spell works well for quick checks of spelling in a short document. ispell is a real spell checker, which not only displays the misspelled words in context, but offers to change them too. The syntax is the same as that for spell. The advantages of using ispell are as follows:

In the case of ispell, each word that does not appear in its dictionary will be displayed at the top of the screen allowing the user to change it. If the dictionary contains any near misses (i.e., words which differ by only a single letter, a missing or extra letter, a missing space or hyphen or a pair of transposed letters,), ispell will display them. ispell may display guesses at ways to make the word from a known root, with each guess preceded by question marks. ispell prints the line containing the word and the previous line at the bottom of the screen. If the terminal can display in reverse video, ispell highlights the word itself. The user can then replace the word completely, or choose one of the suggested words.

BSIT 43 Unix and Shell Programming

25

Some of the basic commands used in ispell are listed below:

Command
R

Action Replace the misspelled word completely accept the word this time only accept the word for the rest of this ispell session. accept the word, capitalized as it is in the file, and update private dictionary Accept the word, and add a lowercase version to the private dictionary Replace with the suggested word corresponding to that number. Look up words in system dictionary Write the rest of this file, ignoring misspellings, and start the next file. Exit immediately and leave the file unchanged Escape to the Unix shell. Redraw the screen. Suspend ispell Show the help screen.

Spacebar A I u 0, 1, ... n l (the letter) x q ! Ctrl-l (the letter) Ctrl-z ?

3.14 TAMING THE CURSOR: tput


tput - query the terminfo database The tput command uses the terminfo(F) database to make the values of terminal-dependent capabilities and information available to the shell. Example 1: Clears the Screen $ tput clear

26

Chapter 3 - General-Purpose Utilities

Example 2: Print the number of columns for the current terminal. $ tput cols 80 Example 3: To position the cursor at row number 20 and column number 10. $tput cup 20 10 Note: tput always require an argument.

3.15 CONCLUSION
All the Unix commands discussed in this chapter were used as single commands. The output of these commands can be used by other Unix commands (filters) for further processing. Combining commands to perform complicated tasks is a feature unique to UNIX.

EXERCISES
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. Use the cal command to find out whether the year 1900 was a leap year. Use the echo command to display the message Good Morning. The cursor should move to the next line after displaying the message. Display the current date in the mm/dd/yy format. Use bc to divide 35 by 9. Set the scale to 2. Record your login session into a file. List out the users logged into the system. Display your terminal filename. Display the release version of your operating system. How do you check a file for spelling errors. How do you lock your terminal for 20 minutes ? How do you check your terminal settings ? What is the alternative command to echo? Give one example.

Chapter 4

The File System

4.1 CONCEPT OF A UNIX FILE

Unix file is a storehouse of information. Every item in a UNIX file system can be divide into four possible types:

1. Ordinary file 2. Directory file 3. Device file 4. Link file 1. Ordinary files: Ordinary files can contain text, data, or program information. An ordinary file cannot contain another file, or directory. An ordinary file can be a text file or a binary file. A text file contains lines of printable characters where every line is terminated with a newline character. A binary file can contain any of the ASCII characters. Most of the UNIX commands are binary files 2. Directory files: Directories are containers that can hold files, and other directories. A directory is actually implemented as a file that has one line for each item contained within the directory. Each line in a directory file contains only the name of the item, and a numerical reference to the location of the item. The reference is called an i-number, and is an index to a table known as the i-list. The i-list is a complete list of all the storage space available to the file system. 3. Device files: Device files represent input/output (i/o) devices, like a tty (terminal), a disk drive, or a printer. Because UNIX treats such devices as files, some of the commands used to access ordinary files will also work with device files. This allows for more efficient use of software. Device files can be either BSIT 43 Unix and Shell Programming

27

28

Chapter 4 - The File System

character device files,that deal with streams of characters, or block device files, that operate on larger blocks of data. Typical block sizes are 512 bytes, 1024 bytes, and 2048 bytes. 4. Link files: A link is a pointer to another file. Since a directory is a list of the names and i-numbers of files, a directory entry can be a hard link, in which the i-number points directly to another file. A hard link to a file cannot be distinguished from the file itself. When a hard link is made, then the i-numbers of two different directory file entries point to the same inode. Hence, hard links cannot span across file systems. A soft link or a symbolic link provides an indirect pointer to a file. A soft link is implemented as a directory file entry containing a pathname. Soft links are distinguishable from files, and can span across file systems. Soft links are not supported in all versions of UNIX.

4.2 WHATS IN A FILE NAME?


A UNIX filename can be upto 255 characters long and may or may not have extensions. Filenames can be a combination of any of the following characters1. Upper case letters (A to Z) 2. Lower case letters (a to z) 3. Numbers (0 to 9) 4. period (.), comma (,) and underscore (_) The following characters should be avoided while naming files: & * \ | () [] {} $ < > ? / ; ^ ! % ~ ` # The Unix command names should also not be used for naming files. While naming files, it is better to choose names that can give the user an idea of its contents. It is also good practice to use extension to file names. Extensions are a few characters separated from the file name by a dot (.). For example, if there are files containing the marks list of students in the first, second and third years, they could be named marks.yr1, marks.yr2 and marks.yr3. Here yr1, yr2 and yr3 are extension names to the file name marks. Since Unix is case sensitive, a filename with uppercase letters will be different from a filename with lowercase letters. For example, the file MARKS.YR1 will be different from the file marks.yr1.

4.3 THE PARENT-CHILD RELATIONSHIP


In UNIX all files are related to one another and are organized in a hierarchical (inverted tree) structure. This is called the UNIX file system and at the very top of the tree is the root directory, named / which

BSIT 43 Unix and Shell Programming

29

serves as the reference point for all the files. The root directory has a number of subdirectories, which in turn can have subdirectories and files. Therefore, every file except the root will have a parent. A sample Unix file system tree:

In the file system tree shown above, the directory. user1 is the parent of the file pay.lst and the directory cprog, users is in turn the parent of user1 and user2 and / is the parent of users Thus, in a parentchild relationship, the parent is always a directory.

4.4 CHECKING YOUR PRESENT WORKING DIRECTORY: pwd


In UNIX, the users location in the filesystem hierarchy is known as the current working directory. When the user logs in, the system places the user automatically in the users home directory. The location or the absolute pathname of the home directory is given by the command pwd which stands for print working directory.

30
Example: $ pwd /users/user1

Chapter 4 - The File System

pwd here tells that, you are placed in the directory users, which has the parent directory users, which in turn is directory under root.

4.5 CHANGING THE CURRENT DIRECTORY: cd


To change the location in the file system hierarchy, the cd (change directory) command is used followed by an argument defining the destination location. The argument can be either an absolute path to the destination, or a relative path Example 1: change to cprog directory from user1 $pwd /users/user1 $ cd cprog $ pwd /users/user1/cprog Example 2: cd without arguments returns to the users home directory $pwd /users/user1/cprog $ cd $ pwd /users/user1

4.6 CREATING DIRECTORIES: mkdir


Syntax: mkdir directoryname

The Unix mkdir command is used to make directories. Example 1: create a subdirectory textfiles for user1 $ mkdir textfiles

BSIT 43 Unix and Shell Programming

31

Example 2: create new directory and within subdirectory $mkdir pis pis/cg pis/pg Example 3: create a directory set1 in the subdirectory cprog of user1 $ mkdir cprog/set1 Example 4: If it is already exists,it display message. $mkdir textfiles mkdir:cant make directory textfiles

4.7 REMOVING DIRECTORIES: rmdir


The UNIX rmdir command removes a directory from the file system tree. The rmdir command does not work unless the directory to be removed is completely empty. A directory can be removed only if it is hierarchically below the working directory. Example 1: remove the directory textfiles which should be empty $ rmdir textfiles Example 2: remove the directory set1 from the directory cprog $ rmdir cprog/set1

4.8 ABSOLUTE PATHNAMES


A pathname gives the files position in the file system. An absolute or full pathname specifies the location of a file in relation to the top of the file system which is the / (root).

An absolute pathname starts at the / (root ) directory and lists each directory along the path to the destination file (or directory). An absolute pathname uses a slash ( / ) between each directory name in the path to indicate different directories.

Example: $ cd /user/user1/cprog A full pathname can become very lengthy and tedious to enter.

32 4.9 RELATIVE PATHNAMES

Chapter 4 - The File System

A relative pathname specifies a file in relation to the current directory and hence the relative pathname depends on the current directory. A relative pathname starts from the current directory. In a relative pathname, a single dot ( . ) represents the current working directory and two dots ( .. ) represent the parent of the current working directory. For files or directories in the current working directory, the relative path of a file or directory is the file name or directory name itself. The parent of user1 is users and the relative pathname of the parent is .. (double dot) and the relative pathname of the root directory is ../.. (double dot/double dot). Examples: 1. Change to the directory set1 which is in cprog located in the working directory. $ cd cprog/set1 2. Move up the file system tree by using the .. (double dot) $ pwd /user/user1/cprog/set1 $ cd .. $ pwd /user/user1/cprog 3. Any number of sets of .. (double dot) with a / (slash) can be used $ pwd /user/user1/cprog $ cd ../.. $ pwd /user

4.10 LISTING DIRECTORY CONTENTS: ls


The ls command shows the contents of a directory, and a view of basic information (like size, ownership, and access permissions) about files and directories. The ls command has numerous options and a complete listing can be obtained from the manual page on ls (by typing man ls). The ls command also accepts one or more arguments. The arguments can be directories, or files.

BSIT 43 Unix and Shell Programming

33

Some of the commonly used options are listed below.

34
Examples: 1. One filename in each line $ ls -1 arith.lst exp.lst oper.c sqlscripts

Chapter 4 - The File System

2. All files including those beginning with a dot (.), current directory (.) and directory above (..) $ ls -a .. arith.lst exp.lst oper.c sqlscripts 3. All files including those beginning with a dot (.) does not list current directory (.) and directory above (..) $ ls -A arith.lst exp.lst oper.c sqlscripts 4. Marks directories with a /, executables with a * and symbolic links with a @ $ ls -F arith.lst exp.lst imp* oper.c sib5* sqlscripts/ 5. Lists in long format, giving mode, number of links, owner, group, size in bytes, the time that each file was last modified. $ ls -l -rw-r 1 sibns group 643 Sep 22 11:31 arith.lst -rw-r 1 sibns group 103 Sep 22 11:31 exp.lst

BSIT 43 Unix and Shell Programming

35

-rwxrr 1 sibns group 17 Feb 12 13:09 imp -rw-r 1 sibns group 348 Sep 22 11:31 oper.c -rwxrr 1 sibns group 15 Feb 12 13:08 sib5 drwxr-xr-x 2 sibns group 512 Feb 12 13:17 sqlscripts 6. Sorts the filenames in the reverse order $ ls -r sqlscripts sib5 oper.c imp exp.lst arith.lst 7. Lists files in columns with entries sorted across $ ls -x arith.lst exp.lst imp oper.c sib5 sqlscripts 8. Combining the l and F options $ ls -lF -rw-r 1 sibns group 643 Sep 22 11:31 arith.lst -rw-r 1 sibns group 103 Sep 22 11:31 exp.lst -rwxrr 1 sibns group 17 Feb 12 13:09 imp* -rw-r 1 sibns group 348 Sep 22 11:31 oper.c -rwxrr 1 sibns group 15 Feb 12 13:08 sib5* drwxr-xr-x 2 sibns group 512 Feb 12 13:17 sqlscripts/ 9. Combining the l and a options $ ls -la drwxr-x 3 sibns group 512 Feb 12 13:20 . drwxr-xr-x 11 sibns group 5120 Feb 12 12:06 .. -rw-r 1 sibns group 643 Sep 22 11:31 arith.lst -rw-r 1 sibns group 103 Sep 22 11:31 exp.lst -rwxrr 1 sibns group 17 Feb 12 13:09 imp -rw-r 1 sibns group 348 Sep 22 11:31 oper.c -rwxrr 1 sibns group 15 Feb 12 13:08 sib5 drwxr-xr-x 2 sibns group 512 Feb 12 13:17 sqlscripts

36 4.11 THE UNIX FILE SYSTEM

Chapter 4 - The File System

While there is no standard Unix file structure for all flavors of Unix, there are some common directories created at the time of Unix installation. The directory structure of a typical Unix system is as shown:

The very top of the UNIX directory structure is called the root directory referred to as slash. (/). This directory is full of other directories. Most of these directories are created at the time of installati on and contain critical user functions. /bin and /usr/bin are directories where all the commonly used commands are located. Since most of the command files are binary files, the directory is called bin. /sbin and /usr/sbin are directories which contain commands that only the system administrator can execute. /etc directory has the configuration files of the system. The login names and passwords are stored in the file /etc/passwd and /etc/shadow. /dev contains all the device files. These files do not occupy any system space. /lib and /usr/lib contain library files in a binary form. Programs written in C use these library routines. /usr/include contains all the header files used by C programs. /tmp is directory where users can create temporary files. /var contains variable data like print requests, incoming and outgoing mail.

BSIT 43 Unix and Shell Programming

37

4.12 CONCLUSION
In this chapter, it was observed that Unix has different file types. Each file type is handled by its own set of commands. Commands like cd, pwd, mkdir are used exclusively with directories. The set of commands required to handle ordinary files are discussed in the next chapter.

EXERCISES
1. 2. What are the rules to be followed while naming files in Unix ? Which of the following file names are not valid and why? a) Books b) file? c) cat d) file name e) book.lst f) rank* g) slno# 3. 4. 5. 6. 7. 8. 9. 10. 11. Suppose the working directory is user1 and the parent is user, what is the absolute pathname of the file list.txt in the working directory user1. What are the different types of files in Unix ? Why is the size of a directory file small ? Create a new directory in your home directory and change to it. Display your absolute path. Can a directory be removed if it is not empty ? What are hidden files and how can they be displayed ? When will rmdir abc fail to work? Which ls options marks directories and executables separately? How will you move from /usr/spool/lp/admins to /usr/spool/mail?

Chapter 5

Managing Ordinary Files

5.1 DISPLAYING AND CREATING FILES: cat


cat concatenates and displays the contents of files Syntax: cat[-v]filename The command cat is normally used to display the contents of text files. With the v option, non-printing characters can also be displayed. Example 1: displays the contents of the file sample.txt $ cat sample.txt The cat command can also be used to create small files without using an editor. Example 2: To create a file called file1 $ cat > file1 When the enter key is pressed, the Unix prompt disappears and cat waits for the user to type in the contents. After each line the enter key is pressed and finally ctrl-d is pressed to terminate the cat command.

5.2 COPYING THE FILES: cp


cp Copies files from a source to a destination

38

Chapter 5 - Managing Ordinary Files

BSIT 43 Unix and Shell Programming

39

Syntax: cp [i][-R] source target i means interactive and R means Recursive. Examples: Source and target are both files. The contents of file infile1 are copied to target file infile2. $ cp infile1 infile2 When target is a directory, cp copies each named file to a destination of the same name within the target directory. Files file1 and file2 are copied to the directory myfolder. $ cp file1 file2 myfolder Or $cp file* myfolder The i option prompts the user for confirmation before copying the file. $ cp i infile1 infile2 cp: overwrite infile2?y The R option copies all files and subdirectories in the directory cprogs to the directory cprogback $ cp R cprogs cprogsback Note: Sometimes, its not possible to copy a file. This can happen if its read-protected or the destination file or directory is write-potected.

5.3 DELETING THE FILES: rm


rm - Removes files or directories Syntax: rm [-ifrR] file The rm command has to be used with caution because a file once deleted cannot be recovered. Examples: 1. Remove the file named infile $ rm infile

40
2. Remove more than one file at a time $ rm file1 infile file2

Chapter 5 - Managing Ordinary Files

3. The interactive option causes rm to ask for confirmation before each deletion. $ rm i file1 4. With the f option, rm forces the deletion of write protected files. $ rm f infile1 5. With r, rm recursively deletes the contents of the directory specified and the directory itself $ rm r cprogs 6. Deletes all files or subdirectories in current directory. $rm r *

5.4 RENAMING THE FILES: mv


mv - move or rename files or directories Syntax: mv [-i] mv source file . target directory Examples: 1. Rename file1 to file2 $ mv file1 file2 2. Move files file1.txt and file2.txt to the directory myfolder $ mv file1.txt file2.txt myfolder

5.5 PAGING OUTPUT: more


more view a file one screenful at a time

BSIT 43 Unix and Shell Programming

41

Syntax: more [ -options ] filename The command more displays a text file one screenful at a time. After each screenful, more pauses and displays the name of the text file being viewed at the bottom. Pressing the ENTER key will continue the display line by line and pressing the SPACEBAR will scroll the display by another screenful. Example 1: Display contents of file on the screen, one page at a time. $more file1 Example 2: Display multiple file contents one after the another. $more file1 file2 file3 Some of the commands that can be given to more when it pauses are listed below:
Command Spacebar f b j k 200G G /pattern ?pattern N or n v . :n :p q Action Scrolls forward one screen Scrolls forward one screen Scrolls backward one screen Scrolls forward one line Scrolls backward one line Goes to line number 200 Goes to last line of file Searches for pattern forward Searches for pattern backward Repeats last search Starts up vi editor Repeats previous command Skips to next file specified in command line Skips to previous file specified in command line Exits from more

42 5.6 PRINTING A FILE: lp


lp print a file Syntax: lp filename

Chapter 5 - Managing Ordinary Files

Unix does not allow direct access to a printer. Files to be printed have to be spooled into a print queue. Each print request is given a job number. The spooling facility ensures printing of files in an orderly manner. When the lp command is given, it displays the request-id, which is a combination of the printer name and the job number. Example: $ lp prog1.c request id is mt-15 (1 file) The print jobs in the queue can be viewed by typing lpstat. This command shows the request-id which can be used to cancel a print job in the queue. A print job can be cancelled either by the user who owns the print job or the system administrator. The system administrator can cancel any job. Example: $ lpstat mt-20 mt-21 mt-22 user1 24330 Mar 12 13:46 user1 15508 Mar 12 13:46 user1 12151 Mar 12 13:46

$ cancel mt-22 request mt-22 canceled $ lpstat mt-20 mt-21 sibns sibns 24330 Mar 12 13:46 15508 Mar 12 13:46

To cancel current job on printer laser $cancel laser To print a title on the first page use t option $lp t First program prog1.c If there are more than one printer in system, then use d option with printer name. $lp dlaser prog1.c

BSIT 43 Unix and Shell Programming

43

5.7 KNOWING FILE TYPES: file


file determines file type Syntax: file filename(s) This command identifies the file as regular, directory or device file. Further attempts at classification are done by testing each filename. The filetype is identified by examining the magic number found in the first few bytes of the file. Each file type has a unique magic number. Example: * denotes all files. Various types of files are identified by file. $ file * abs.dat: absdel.cob: add.lst: cob: hist: ascii text English text empty directory commands text

If all these files exist under directory progs $file progs/*

5.8 COUNTING LINES, WORDS, AND CHARACTERS: wc


wc Counts lines, words and characters Syntax: wc [-lw][-c] filename(s) The command counts lines, words and characters in the file(s) named depending on the option used. Examples: 1. Count lies, words and characters in the file sib0.cob $wc sib0.cob 582 2378 22706 sib0.cob 2. Count lines in the file sib0.cob $ wc -l sib0.cob 582 sib0.cob

44
3. Count words in the file sib0.cob $ wc -lw sib0.cob 2378 sib0.cob 4. Count characters in the file sib0.cob $ wc -c sib0.cob 22706 sib0.cob

Chapter 5 - Managing Ordinary Files

5.9 DISPLAYING A DATA IN OCTAL: od


od display files in octal format
Syntax: od [options]file

The od command makes non-printing characters in a file visible by displaying the ASCII octal value of the characters. The command line options are listed as:

Example $ cat f1

BSIT 43 Unix and Shell Programming

45

carriage return ^M The file f1 when displayed with od and the bc options. $ od bc f1

Each line in the file f1 is displayed in two lines. The first line shows the octal representation preceded by the position, within the file, of the first byte in the line. The second line shows the printable characters and the escape sequences.

5.10 SPLITTING A FILE INTO MULTIPLE FILES: split


split - split large files into smaller files Syntax: split [options] filename prefix where filename is the name of the large file to be split, prefix is the name to be given the small output files and options can either be excluded or can be one of the following: -l linenumber -b bytes If -l (the letter l) option is used, linenumber will be the number of lines to be put in each of the smaller files (the default is 1,000). If the -b option is used, bytes will be the number of bytes to be put in each of the smaller files. The split command will give each output file created the name prefix with an extension attached to the end to indicate its order. By default, the split command adds aa to the first output file, proceeding through the alphabet to zz for subsequent files. If no prefix is specified, most systems use x . Examples: 1. Assuming that file.txt is 3,000 lines long, it will output three files, xaa, xab, and xac, and each one will be 1,000 lines long.

46
$ split file.txt

Chapter 5 - Managing Ordinary Files

2. This will output six 500-line files: fileaa, fileab, fileac, filead, fileae, and fileaf. $ split -l 500 file.txt file 3. Assuming that file.txt has 200 kilobytes, this will output five 40 kilbyte files: fileaa, filetab, fileac, filead and fileae $ split -b 40k file.txt file

5.11 COMPARING TWO FILES: cmp


cmp compare two files Syntax: cmp [-l] file1 file2 The cmp command compares two files of any type and sends the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported. The numbering of Bytes and lines begin with one. The option l displays the byte number in decimal and the differing byte values in octal whenever a difference in the two files occurs. Example: $ cat fruits1 apple mango pineapple orange $ cat fruits2 apple grapes pineapple orange $ cmp fruits1 fruits2 fruits1 fruits2 differ : char 7, line 2

BSIT 43 Unix and Shell Programming

47

5.12 FINDING WHAT IS COMMON: comm


comm lines common to two sorted files Syntax: comm [-123] file1 file2 The comm command reads two sorted files, file1 and file2, to produce a three-column output. The first column gives lines that exist only in file1; the second column displays lines that exist only in file2; and the third column shows the lines common in both files. Particular columns in the output can be dropped using the following options-

Example: $ comm fruits1 fruits2 apple grapes mango pineapple orange $ comm -23 fruits1 fruits2 mango $ comm -12 fruits1 fruits2 apple pineapple orange

5.13 CONVERTING ONE FILE TO OTHER: diff


diff compares two files and displays the lines that are different

48
Syntax: diff file1 file2

Chapter 5 - Managing Ordinary Files

The diff command displays the line numbers of the changed lines in the two files. Special symbols are used as instruction to indicate changes to be made to make the files identical. Each instruction is a combination of the line address and action to be taken in the first to make the differing lines identical. For example, the instruction 1a1, append after line1 of the first file with first line of second file. Example: $ diff fruits1 fruits2 2c2 < mango > grapes #change line2 in the first file replace this by the second line of second file

2c2 changes line 2, which is line 2 in the second file.

5.14 CONCLUSION
Some of the commands in this chapter use input from the keyboard. Some others take the output of another command as their input. Commands like wc, cat and cmp can also send their output to a file. This will be discussed in later chapters.

EXERCISES
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. Create a file called lesson1 in your home directory using the cat command. Create a directory called lessons and copy the file lesson1 to this directory Remove the file lesson1 from the home directory. Create another file called lesson2 and move it to the directory lessons. Change to directory lessons and count the number of characters and lines in the files lesson1 and lesson2. What happens when lesson1 and lesson2 are compared. List the contents of the directory lessons datewise. Create a file with at least 100 bytes and split it into files of 20 bytes each How to copy a directory structure from dir1 to dir2? How to display only the lines common to two files? When creating a file with cat, how can terminate input?

Chapter 6

Basic File Attributes

6.1 LISTING FILE ATTRIBUTES: ls l

he command ls l lists seven attributes of all the files in the working directory. The top the list gives the number of blocks occupied by the files (a block is 512 bytes). The first column gives the file mode, which consists of a string of 10 characters. The first character indicates the file type:

BSIT 43 Unix and Shell Programming

49

50
Character d l b c p s File type Ordinary file directory file symbolic link file block device file character device file FIFO file or pipe file Socket file

Chapter 6 - Basic File Attributes

The nine characters after the file type are the file permissions, which are displayed in three sets of three characters each. The first set refers to the owner permission, the second set to the group permission and the third to permissions given to other users. In each set of permissions the three characters can be r (read), w (write), x (execute) or (no permission is set). The x permission for a directory means it can be searched. In Unix, a single copy of a file can be referred to by several filenames. The second column gives the number of filenames or links associated with the file. If the link count is greater than one it indicates that the file has more than one name. The third column gives the file ownership. When a user creates a file, the file automatically belongs to the user. The third column displays the owner of the file. The owner has the right to change file permissions, alter the contents of the file and remove the file. The fourth column shows the group ownership of each file. At the time of user account creation, the system administrator assigns the user to a group. All the users belonging to this group will have same privileges to the files with the same group id. The fifth column displays the file size in bytes. This does not mean the amount of disk space occupied by the files. The space occupied by files is measured in blocks of 1024 bytes each. Directories show smaller sizes because a directory maintains the list of filenames and their identification number for each file within the directory. The size of the directory is therefore the size of this list of filenames. The sixth column gives the date and time of modification of the files. The last column gives the name of the file and the list is arranged in the alphabetical order.

6.2 FILE PERMISSIONS


In Unix, the system of assigning permissions to files is simple and well defined, There are three levels of file protection that determine a file access rights.

BSIT 43 Unix and Shell Programming

51

A file permission string has 10 characters, in which the first character gives the type of file and the remaining 9 characters form three groups of three characters each. $ ls l sal -rwxr-xr 1 sibns sibgrp 366 Sep 22 11:31 sal The first character is a hyphen and indicates that sal is an ordinary file. The remaining nine character are grouped as follows :

The first group of characters, rwx shows that the user (owner) has all permissions, ie., read, write and execute. In the second group of three characters, r-x the second character is a hyphen which means that only read and execute permissions are given to all the users who belong to the group sibgrp. Finally, in the last group of three characters, rthe second and third characters are hyphens which indicates that only read permission is given to others.

6.3 CHANGING FILE PERMISSIONS: chmod


chmod - change the access permissions of a file or directory Syntax in Symbolic mode: chmod [ -R ] [ category ] [operator] [ permission ] file

Syntax in absolute mode: chmod [ -R ] mode file The chmod command changes the access permissions (or mode) of the specified file or directory. Users who own the file or directory use it to control file and directory access. Mode is an expression made up of letters and operators (called symbolic mode), or a number (called absolute mode). The R option is used to change file mode characters recursively. For a specified directory, chmod will change the file mode characters of the directory and all files in the file hierarchy below it. In the symbolic mode, category can be any one, or a combination, of the following letters:

52
Letter u g User Category

Chapter 6 - Basic File Attributes

a The operators are:

For user, the owner of the file or directory. For group, all users who have the same group ID as the owner of the file or directory For others, all the other users on the system who neither belong to the group nor own the file . For all users

The mode can be any combination of the following letters:

Multiple symbolic modes can be given on a single command line, separated by commas. Examples: 1. Gives execute permission to the user for the file siblst $ chmod u+x siblst 2. Gives write permission to user, group and others $ chmod ugo+w siblst 3. Using multiple modes in a single command $ chmod g+w,o+r siblst

BSIT 43 Unix and Shell Programming

53

In absolute mode, each type of permission is an octal number (base 8). For each category, the permissions are assigned by adding the octal numbers representing the permissions as shown below-

The permission combinations are listed in the table below:

The permission fields in octal numbers are listed below:

Examples $ chmod 764 siblst For the file siblst, chmod assigns 7 (4+2+1) read , write and execute permission to the user(owner) 6 (4+2) read and write permission to the group and 4 read permission to others. $ chmod 777 siblst All permissions are assigned to all categories of users - user, group and others (ugo) The chmod command can also be used recursively with the R option.

54

Chapter 6 - Basic File Attributes

Example: Assigns execute permission to all the users for all the files in the cprogs directory. $ chmod -R a+x cprogs Example for Absolute assignment:Assign the only permission to all users and remove the other permission. $chmod ugo=r siblst $chmod a=r siblst

6.4 FILE SYSTEM AND INODES


A Unix system may have more than one file system where each file system has its own tree-like directory structure with a root directory. When there are more than one file systems, one of them which contains all the essential files of the Unix system will be the main file system. This is the root file system and all the other file systems, if they exist, attach themselves to the root file system. The file attributes (except name and content) of each file in the file system are stored in a table. This table is called the inode and can be accessed by the inode number. The inode number along with the filename is stored in the directory. When a Unix command uses a filename in its command line, the kernel locates the inode number of the file from the directory and then reads the inode to get all the details of the specified file. In every file system, a part of the file system is reserved for storing inodes which is accessible only to the kernel. Using simple arithmetic, the kernel locates the inode number of any file in the file system. The inode number of a file is unique in the file system. The ls command uses the inode to get a files attributes. The inode number can be displayed using the ls command as follows$ ls il siblst 2737 -rw-rr 1 sib sibgrp 38200 Mar 29 16:16 siblst The inode number for the file siblst is 2737. This number cannot be assigned to any other filename unless the file is deleted. If the file is deleted, the inode number is given to another file by the kernel.

6.5 HARD LINKS AND SYMBOLIC LINKS 6.5.1 Hard Links


In Unix, a single copy of a file can have several names, which are links to the same file. Since all the filenames refer to the same file, they have the same inode number. The second column in output of the ls l command gives the number of links that a file has. The number is 1 for unlinked files.

BSIT 43 Unix and Shell Programming

55

Hard links can be created using the ln command. The syntax is the same as the syntax of the cp command. Example: Displays the link count of siblst as one $ ls il siblst 2737 -rw-rr 1 sib sibgrp 38200 Mar 29 16:16 siblst Create a hard link to siblst called sibrep using the following command: $ ln siblst sibrep Display the inode numbers and link counts of the two files $ ls il siblst sibrep 2737 -rw-rr 2 sib sibgrp 38200 Mar 29 16:16 siblst 2737 -rw-rr 2 sib sibgrp 38200 Mar 29 16:16 sibrep It is observed that the two files have the same inode number and the link count displayed in the second column of the file attributes is two. If another hard link is created to the file siblst, then the link count increases to three. The link count gives the number of links created for a file. More than one file can be linked in a single command line. In this case the destination is a directory. Example: 1. Links are created to each one of the files file1, file2 and file3 in the directory filelnk. $ ln file1 file2 file3 filelnk Display inode numbers and link counts of file1, file2, file3 $ ls -il file1 file2 file3 4014 -rw-rr 2 sibns group 10240 Apr 5 12:34 file1 4015 -rw-rr 2 sibns group 10240 Apr 5 12:34 file2 4017 -rw-rr 2 sibns group 7214 Apr 5 12:34 file3 Display files in the filelnk directory $ ls -il filelnk total 56 4014 -rw-rr 2 sibns group 10240 Apr 5 12:34 file1 4015 -rw-rr 2 sibns group 10240 Apr 5 12:34 file2 4017 -rw-rr 2 sibns group 7214 Apr 5 12:34 file3

56

Chapter 6 - Basic File Attributes

Hard links have their own uses. When files in a directory are shifted to another directory, these files can still be made available by creating hard links. The use of hard links also prevents accidental deletions of files.

Hard links have some limitations.

Unix permits hard links to files, but, not to directories. Each directory inode is allowed to appearonce in exactly one parent directory and no more. This restriction means that every sub-directory only has one parent directory, and that means the special name .. (dot dot) in a sub-directory always refers unambiguously to its unique parent directory. Hard links cannot be created across file systems. ie., a file in the /user1 file system cannot have a link in another file system /user2.

These limitations can be overcome by the use of symbolic links.

6.5.2 Symbolic Links


A symbolic link is a special file that contains the pathname of the file to which it is linked. This is unlike the hard link wherein the linked file also contains the contents of the file to which it is linked. Symbolic links are also called as soft links and are more flexible. They can be used to link to directories, or to files on other file systems. When a target file is deleted, a symbolic link to that file become unusable. This is unlike a hard link where the contents of the file are preserved. The ln command along with the s option is used to create a symbolic link $ ln -s o2 o2lnk $ ls -il o2 o2lnk 3367 -rw-rr 2823 lrwxrwxrwx 1 1 sibns sibgrp 12451 Apr 1 12:44 o2 sibns sibgrp 2 Apr 1 12:42 o2lnk -> o2

The first character in the permission string of the symbolic link file will be l. The notation o2lnk -> o2 indicates that the file o2lnk contains the pathname for the file o2. In symbolic links, the linked files are two different files. A symbolic link has a separate directory entry with its own inode number. If the symbolic link file, o2lnk, is deleted, it will not matter because it contains only the pathname of the file to which it linked. On the other hand, if the file o2 is deleted, the data is lost and the symbolic link becomes a dangling symbolic link. This is because the data file it is linked to, no longer exists.

BSIT 43 Unix and Shell Programming

57

Difference between hardlink and symbolic link Hard link 1. Does not create a new inode 2. Cannot link directories unless this is done by root 3. Cannot link file across filesystem 4. Increase hard link count. Symolic link 1. Create a new inode 2. Can link directories. 3. Can link file across filesystem 4. Does not change the hard link count.

6.6 DEFAULT FILE AND DIRECTORY PERMISSIONS: umask


Every time a file or a directory is created, default permissions are established for it. These default permissions are initially assigned either by the operating system or the program being run. Setting default permissions saves us the trouble of specifying permission codes explicitly every time a file or directory is created. The operating system assigns the default permission values of 777 for executable files and 666 for all other files. To put further restrictions on the permissions assigned by a program when it creates a file or directory, a user mask is specified with the umask command. The user mask is a numeric value that determines the access permissions when a file or directory is created. Consequently, when a file or directory is created, its permissions are set to the permissions specified by the creating program minus the permission values denied by the umask value. Example The command umask without arguments gives the octal value of the user mask. $ umask 022 The octal number 022 is subtracted from the system default permissions to get the actual default permission. Therefore, by default, an executable file will have the default permission set to 755 (777-022) or rwxr-xr-x. All other files will have the permission 644 (666-022) or rw-rr. The umask command can be used by the user to set a new default permission mask. The syntax is: $umask octalnumber Setting the umask, actually means specifying which permissions are not to be granted regardless of the permissions requested by the file creating program.

58
The eight possible umask permission combinations are listed below.

Chapter 6 - Basic File Attributes

A suitable user mask value to be set for a users files and directories depends upon how freely information resources are shared on the system. In an open computing environment, the umask value maybe set to 000, which allows no restrictions on file and directory access. In such a case, when a program creates a file and specifies permission codes for it, the user mask puts no restrictions on what the creating program has specified. In a more secure computing environment, the user mask value 066 maybe specified. This allows the owner complete access, but prevents all others from being able to read or write the owners files. Therefore, when a file is created, its permissions are set to what the creating program specifies, subtracted by the user mask value. This denies read and write access to everyone but the owner. In a very secure computing environment, the user mask value will be 077. This allows allows access only to the owner. When a file is created, its permissions are set to what the creating program specifies, minus the user mask value which prevents anyone else from reading, writing, or executing the owners files. Example $ umask 037 If the file is executable, this umask value will set a permission code of 740 (777-037) or rwx-r, which means the owner is allowed all permissions, group members are allowed only read permission and others are not allowed any permissions.

6.7 LOCATING FILES: find


find - find files matching a set of selection criteria

BSIT 43 Unix and Shell Programming

59

Syntax in absolute mode: find pathname-list expression The command find recursively descends the directory tree for each pathname in the pathname-list looking for files that match a boolean expression. The pathname-list can have one or more directories separated by whitespace. The expression is a selection criteria followed by the action to be taken. Example Find and display all the files which are extended with .lst in the current directory. The dot (.) after find indicates the current directory. The print action displays the location of the file file1.lst in the current directory (dot). $ find . name file1.lst print Files of different types can also be looked up using find. File type can be f (for ordinary files), d (for directories), l (for symbolic links), c (for character special files) and b (for block special files). Find and display all the directories in the file system /user1 $ find /user1 type d print Find and display all the files with permission 777 in the root file system $ find / -perm 777 print Find all the directories in the home directory with permissions 777. An implied and condition between perm and type is applied by find here. $ find $HOME -perm 777 type d print Find all files modified within the last 7 days $ find /user1 mtime 7 print Find all files not accessed for more than a year $ find /user1 atime +365 print The logical operators a (and), -o (or) and ! (not) can also be used the selection criteria of find. Find the files sample.txt or file1.txt in /user1 $ find /user1 name sample.lst o name file1.txt print Find the file file1.txt in /user1 and remove it; the rm command is followed by a pair of flower brackets, a backslash and a semicolon. $ find /user1 name file1.txt exec rm {} \;

60 6.8 CONCLUSION

Chapter 6 - Basic File Attributes

Every directory and file on the system has an owner, and also an associated group. It also has a set of permission flags which specify separate read, write and execute permissions for the user (owner), group, and other (everyone else with an account on the computer) The ls command shows the permissions and group associated with files when used with the -l option. In this chapter, all the file and directory attributes have been discussed. The use of inodes to store file attributes, hard links and their limitations and symbolic links have also been discussed.

EXERCISES
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. How do you list all the attributes of a file in your home directory ? Create a file called sample.txt. How do you assign all permissions to the owner, read and write permissions to the group and no permissions to others using relative permissions and absolute permissions. What happens if a directory has permissions 777 ? How are default file and directory permissions changed ? What are the changes seen in the files inode number when a link is created ? How are inode numbers displayed ? If two files have the same inode numbers, what do you conclude ? what happens when you delete one of these files ? What are the uses of hard links ? How are hard links created ? For the file sample.txt, create a symbolic link. What is a dangling symbolic link ? How do you order the list of files by their modification time ? Find all the directories in the /user directory ? Find all the files having the same inode number in the /usr directory. Find all the files called core in youe home directory and remove it. What is the difference between hard link and symbolic link?

Chapter 7

The Vi Editor

7.1 THE BASICS OF vi

i is a full screen text editor which can be used to create and edit text files. To invoke vi, the user types vi followed by the filename at the Unix shell prompt. For example to create a file called sample.txt, vi is invoked with the following command

$ vi sample.txt If the file sample.txt already exists, vi opens it for editing. vi makes use of 24 lines of the terminal display for editing.

7.2 THE MODES OF vi


The three modes of vi are the command mode, the input mode and the ex or last line mode.The command mode is the default vi mode. Every key pressed in this mode is interpreted as a command to be executed on the text. The key pressed is not displayed on the screen. If the [Esc] key is pressed in this mode, a beep is sounded. Commands can be given to perform functions like moving the cursor around the screen, delete lines, words or characters, etc. The input mode is used to enter text. Every key pressed after entering this mode is displayed on the screen. After completing text entry, the cursor is positioned on the last character of the last line. This line is called the current line and the cursor position is called the current cursor position. The [backspace] key moves the cursor backwards and wipes out the character entered. To return back to the command mode, [Esc] key is pressed.

BSIT 43 Unix and Shell Programming

61

62

Chapter 7 - The vi Editor

The ex mode or the last line mode is used to enter and run commands like saving files, reading files, text replacing, exiting vi, etc. The text entered in the input mode resides in a temporary storage called the buffer. To save text to a file, from the command mode, the user has to change to the ex mode which is done by entering a colon followed by the save command and the [Enter] key. After the command is executed, vi returns to the command mode. Many commands in the command mode and the input mode have the repeat factor. A command is executed as many times as the prefix given to the command. For example, if a character is deleted with the command x, with a repeat factor of 4, the command deletes the character below the cursor as well as the next three characters. This helps in speeding up operations. An illustration of the three vi modes is shown below:

7.3 INSERTING AND REPLACING TEXT


The following key commands are used to insert or replace text in the input mode:
Command i I a Function Insert text to left of cursor Insert text at the beginning of line Appends text to right off cursor

BSIT 43 Unix and Shell Programming

63

A o O r R s S

Appends text at end of line Opens a line below Opens a line above Replaces single character under cursor Replaces text from cursor to right Replaces single character under cursor with any number of characters Replaces entire line

7.4 NAVIGATION IN COMMAND MODE


In the command mode, navigation keys for the movement of the cursor by characters, words and lines are as follows:

Keys h or backspace j k l or spacebar w b e ^ $ 0

Function Moves cursor left Moves cursor down Moves cursor up Moves cursor right Moves forward to beginning of word Moves back to end of word Moves forward to end of word Moves first character of line Moves end of line Moves the beginning of line

64

Chapter 7 - The vi Editor

The screen display can be controlled by scrolling the display forward and backward using the following keys:

Keys [ctrl-f] [ctrl-h] [ctrl-d] [ctrl-u]

Screen/page display Scroll full page forward Scroll full page backward Scroll half-page forward Scroll half-page backward

The current line number of the cursor position can be obtained by pressing [ctrl-g]. The cursor can be positioned to the end of the file or to any line in the file by a single keystroke as follows:

7.5 SAVING TEXT


When a file is edited in vi, the changes are only made in the buffer. The buffer has to be saved to the file. The following are the save and exit commands:

Command :w :w filename :wq :x :q! :.w

Action

write buffer to the current filename and remain in vi write buffer to file filename. and remain in vi write buffer to current filename and quit vi same as :wq, saves buffer to file and exits vi quit vi without saving buffer filename write current line to file

BSIT 43 Unix and Shell Programming

65

:.w :$w filename :n1,1n2w filename :e filename ZZ

filename write current line to file write last line to file selected lines saved to file, writes line n1 - n2 to file close current buffer and filename save to current file and exit vi

If for some reason, the Unix system crashes while in vi, the user can still recover the buffer by typing $ vi r filename

7.6 DELETING TEXT


The following commands are used for deletion of text:

7.7 PATTERN SEARCH


Searching for text in vi can be done in both forward and backward directions. The search command can also be repeated. The commands are listed below:

Command /pattern[Enter]

What happens Searches forward and positions cursor on the first occurrence of the pattern in the text being searched

66
?pattern[Enter] n
N

Chapter 7 - The vi Editor

Searches backward and positions cursor on the first occurrence of previous line matching pattern Repeat search in same direction of previous search Repeat search in direction opposite to previous search

7.8 PATTERN SEARCH AND REPLACE


vi has a powerful search and replace feature which is very useful. It is used in the colon mode and the syntax is as follows: :line address/pattern to search/replace pattern/flags Example: :1,$s/sales/market/gp vi searches for the pattern sales in the entire file and replaces it by the text market.. The address 1,$ represents all lines in the file. The flag g indicates that the substitution is to be done globally and p displays the number of substitutions done.

7.9 MISCELLANEOUS OPERATIONS IN COMMAND MODE


C change rest of line starting at cursor (same as c$) D delete rest of line starting at cursor (same as d$) J join lines Y yank lines ie. Copy lines (same as yy) p put back text after cursor P put back text before cursor u undo last change U restore current line . repeat last change

BSIT 43 Unix and Shell Programming

67

7.10 CONCLUSION
vi is a very powerful screen editor. Some of them have been featured in this chapter There are a lot of other features available with vi. It requires practice to master the vi editor.

EXERCISES
1. How do you do the following using vi: a) delete a line b) overwrite text c) move the cursor four lines down d) add a new line above a line of text e) remove two lines from the end of a text and add it to the beginning of the file. f) open a file and move to the fifteenth line in the file. 2. 3. 4. 5. 6. 7. What are the different ways of inserting text using vi ? How do you replace all the occurrences of the word printf with fprintf in a file ? What are the different ways of coming out of vi after saving the file ? How do you yank and paste lines ? How to abort an editing session? What is the function of following command i)dd ii)e iii)u iv)$ v)G

Chapter 8

The Shell

8.1 THE SHELLS INTERPRETIVE CYCLES

shell is a program which is run every time a user logs in and is known as a command interpreter. Shell is a agency that sits between user and UNIX system.All the commands entered are run by the shell. The prompt that appears when a user logs in is a shell and waits for the user to enter a command. The following activities are typically performed by the shell in each cycle: It issues the prompt and waits for user to enter command. After a command has been entered, the shell scans the command line for metacharacters, ie., characters that have a special meaning to the shell. After processing the metacharacters, the shell send the command line to the kernel for execution. The shell waits for the kernel to complete execution and then displays the prompt for the next command.

This interpretive cycle continues for the next command

8.2 COMMON SHELLS


There are several different shells that can be used in Unix. Some of the most common Unix shells are Bourne shell (sh), C shell (csh) , and tcsh. The bourne shell (/bin/sh) is the original Unix shell written at AT&T. Some of its features are wildcard

68

Chapter 8 - The Shell

BSIT 43 Unix and Shell Programming

69

substitution, I/O redirection, environment variables, and a simple script programming language. The korn shell (/bin/ksh) and Bash (/bin/bash) are its derivatives. The C shell (/bin/csh), written at Berkeley, is an improvement over the bourne shell. The C shell has some additional features like filename completion, command aliasing, history substitution, job control, a number of built-in commands, and an improved programming language. The tcsh is a derivative of the C shell. This shell has a number of built-in features, the most useful being the command line editing, and the use of the arrow keys for browsing the command history.

8.3 PATTERN MATCHING THE WILD-CARDS


Wildcards are a special set of characters used by the shell to match filenames. A wildcard stands in for other characters of the filename. The filenames pattern is formed by ordinary characters and metacharacters using well defined rules. When the pattern is used as an argument with a command, the shell first suitably expands the argument and then executes the command. The use of wildcards makes it easier to deal with files in Unix. File names can be abbreviated using wildcards. The shells wildcards are listed below: The question mark, ?, stands in for any single character. For example, suppose there are three files, file1, file2 and file12 in a directory. The ls command with file? As argument will display the following: $ ls file? file1 file2 file12 will not be listed because the question mark can stand in for either 1 or 2, but not both at the same time. Since, the asterisk, *, stands in for any number of charactersincluding zero characters, the following command lists all the files that begin with file. $ ls file* file1 file2 file12 The command ls * will list all files except those that begin with a . (dot). If such files also have to be listed, the . (dot) has to be explicitly matched. For example: $ ls .* will list all files, including the ones that begin with a . (dot). Matching filenames can be restricted by using the character class. The character class is a set of characters enclosed by the rectangular brackets ( [ ] ).

70
Example:

Chapter 8 - The Shell

1. Matches all filenames beginning with file and ending with characters 1, 2 and 5 $ ls file[125] file1 file2 2. Matches all filenames beginning with file and ending with the range of characters 1 to 5 $ ls file[1-5] file1 file2 3. Matches all filenames beginning with an alphabet- uppercase or lowercase $ ls [a-zA-Z]* file1 file2 file12 4. Matches all files $ ls * file1 file2 file12 5. Remove all files with filenames ending with .txt $ rm *.txt 6. Remove all files having filenames starting with file, any character and then ending with .lst $ rm file?.lst 7. Remove all files ending with a, character, a $ rm *.a?a 8. Move all files beginning with lesson and ending with .txt to the lessonfiles directory mv lesson*.txt lessonfiles 9. Wildcards can also be combined to match file names. Files that begin with file and end with lst?. The ? means any single character $ ls file*.lst?

BSIT 43 Unix and Shell Programming

71

8.4 ESCAPING AND QUOTING


In Unix, file names should not contain any of the following special characters: & * ? [] <> | \ { } $ ( ) # / : ^ ! - % `

Each special character has a special meaning to the shell But, if a file name does contain any of these characters, the shell should take the special character literally. There are two ways of doing this

Escaping: Providing a backslash (\) before the wild-card to remove its meaning. Quoting: Enclosing the wild-card, or even the entire pattern, within quotes. Anything within the quotes are left alone by the shell and not interpreted.

Examples If there are four files with file names file1, file2, file3 and file*, to delete the file with the name file*, the escape character \ is used. $ rm file\* Without the escape character (\), the rm command would have interpreted * as a wildcard and deleted all the files with filenames beginning with file. 1. Display an asterisk $ echo \* $ echo how are you \? Or how are you? Can be enclosed in single quotes $ echo how are you? 2. Display three asterisks, enclosed in double quotes $ echo *** 3. Delete a file with filename file* $ rm file* The use of single quotes protects all the special characters, except single quotes. Double quotes are not so powerful and do not protect the $, backquote () and the double quote itself.

8.5 REDIRECTION: THE THREE STANDARD FILES


When a process starts executing, the operating system opens three files for the process: 1. Standard input (stdin), the file representing input from the keyboard

72
2. Standard output (stdout), the file representing output to the screen

Chapter 8 - The Shell

3. Standard error (stderr), the file which represents error meassages coming from the command and displayed on the screen The command expects to read its input from the standard input. The file representing standard input can have three sources: 1. The default source which is keyboard. 2. Input can be read from a file using redirection. 3. Input can be from another command using pipes. The program writes its output to standard output. The file representing standard output can have three sources: 1. By default, a process writes stdout to the screen. 2. Output can be redirected to a file 3. Can be used as an input to another command The program writes its error messages to standard error, which by default is the screen. A command usually reads input from the keyboard (standard input) and writes its output to the display (standard output). If a command needs to read its input from a file or write its output to a file, or both, the input and output files for a command can be selected using the following notations:

All shells allow a user to redirect the standard input of a process to read input from a file, instead of from the keyboard. Any command that accepts input from the keyboard can use input redirection. Some commands like who, that do not accept input cannot have redirection for input. The < character is used to redirect input to a command from a file. Example: The command wc reads the input from the file file1.txt. $ wc < file1.txt Many commands allow the input file to be specified without the redirection notation. If no argument is given, wc reads its input from the keyboard. The end of input is indicated by the pressing [ctrl-d].

BSIT 43 Unix and Shell Programming

73

$ wc This is a sample file To be given as an input to The wc command [ctrl-d] However, a few commands like mail make use of < for special functions. $ mail user1 < letter.txt The mail command mails the file letter.txt to user1. The shell can redirect the standard output of a process from the screen (default), to a file. Hence, the output generated by a command can be stored in a new file using > or appended to an existing file using >>. If >> is used to write output to a file that does not exist, the shell creates the file containing the output of the command. Example: The output of ls goes to the file named file.lst If file.lst already exists, the shell overwrites its contents with the output of ls. If file.lst does not exist, the shell creates it. $ ls >file.lst The output of ls is added to the end of the file named file.lst: If file.lst does not exist, the shell creates it. $ ls >>file.lst Concatenate all the list files and redirect it to the file allfiles.lst $ cat *.lst > allfiles.lst In addition to the standard output, a command can also produce error or status messages known as diagnostic output. When a command is executed successfully, the results are displayed on the standard output. But, when a command executes unsuccessfully, an error message is displayed on the screen which is the default standard error file. The shell, however allows the error to be redirected to a file. The three standard files are represented by a number called the file descriptor digit. The kernel maintains a table of file descriptor digits for each one of the commands being executed on the system. The file descriptors are:

74
The general format for standard error redirection is as follows: command 2> errfile

Chapter 8 - The Shell

where errfile is the name of the file to which the process writes the standard error. The 2> is a file descriptor digit combined with >, the output redirection symbol. The file descriptor digit indicates to the shell which standard file is to be accessed so that its contents may be redirected. The file descriptor digit 2 is an indication that the standard error file is being redirected. Example: When the ls command tries to list a file which does not exist, an error is redirected to the file errorfile $ ls file1.lst 2> error Display the contents of errorfile. $ cat errorfile cat : cannot open file1.lst : no such file or directory (error 2) Both standard output and standard error can be redirected to different files. The syntax is: command > outfile 2> errorfile The process writes the standard output to outfile. The 2> symbol redirects the error output to errorfile. Example: The output of the shell script execution is redirected to result.txt and the error to err.lst $ trial.sh > result..txt 2> err.lst To redirect both standard output and standard error to the same file: command 1> outfile 2>&1 The 1> symbol redirects the standard output to outfile. The 2>&1 symbol tells the shell to write the standard error in outfile. $ trial.sh > result..txt 2>&1

8.6 /dev/null AND /dev/tty: TWO SPECIAL FILES


The /dev/null is a special file that always remains empty. Anything redirected to /dev/null disappears. Redirecting error messages to /dev/null will prevent them from being displayed on the terminal screen. This is the only device file which is not associated with any physical device.

BSIT 43 Unix and Shell Programming

75

Example $ trial.sh > result..txt 2> /dev/null $cat /dev/null $_ #size is always zero

The special file /dev/tty is associated with the terminal. This file can be used for redirection and as an argument in some Unix commands. This is not the file that represents standard output or standard error. Every terminal in the Unix system has a different terminal number. However, users logged into the system can refer to their respective terminals with the same file name /dev/tty. This is useful in programs or shell scripts where the output has to be explicitly redirected to the terminal, /dev/tty.

8.7 pipes
A pipeline is the simplest and most used inter-process communication (IPC) mechanism in Unix. A pipe is a communication channel defined by two file descriptors, one open for output and one for input The output from one process goes as an input to another process, which means data goes into one end of the pipe an comes out of the other. The data flowing in a pipe are managed directly by the kernel. The shell connects the standard output and standard input using the special pipe character ( | ). Pipes in Unix are commonly used to avoid writing temporary files to communicate between programs.: Example: $ ls -la | more Which takes the standard output (stdout) from the ls command and transfers it to the standard input (stdin) of the more command The more command list of the files one page at a time. If there were no pipe construct available, the process would be as follows: $ ls -la > filelist $ more < filelist $ rm filelst The output of the ls command is redirected to a file called filelist. The contents of filelist are then displayed using the more command. By running two separate commands, it is observed that: 1. When the first command takes a long time to execute, the second command will not run until the first command has completed execution.

76

Chapter 8 - The Shell

2. An intermediate file is used which, when large can take up disk space. This file has to be removed after the command execution is complete. Another example: Count the number of users currently logged into the system Using a pipe eliminates the use of an intermediate file. $ who | wc l Count the number of files and redirect the output to a file. $ ls | wc l > outfile Count the number of words in all the files ending with .txt. $ cat *.txt | wc w There is no restriction on the number of commands used in a pipe. However, proper placing of commands in the pipeline is important.

8.8 CREATING A TEE: tee


A tee allows a user to save the output from a command to a file and at the same time, pipe the output to another command or save the output to a file. tee is an external command and can be placed anywhere in a pipeline. With tee a user can read input from an input stream, and split the output stream so it is both displayed on screen (stdout) and also redirect it to a file. Example: Get a directory listing on the terminal, while also redirecting the output to a file named filelist.: $ ls -al | tee filelist When this command is run, the output of ls al will not only appear on the terminal as expected, but will also be saved in the file named filelist. The cat command displays a copy of the contents also sent to the file. $ cat filelist The tee command is very useful in a situation like this, where the user would like to see the output on screen and also capture it into an output file.

BSIT 43 Unix and Shell Programming

77

The ls command lists all .htm files in the current directory, count the number of lines and the output is displayed to the /dev/tty (terminal) also redirected to the hcount.txt. $ ls *.txt | wc -l | tee /dev/tty hcount.txt

8.9 COMMAND SUBSTITUTION


In command substitution, the shell alows the the user to use the output from one command as arguments to another command A pair of backquotes (), also called grave accents does command substitution in Unix shells. Only those commands that use standard output can be used in command substitution. Example: date is a Unix command whose output is given as an argument to echo. The command is enclosed in a pair of backquotes. $ echo today is date The shell executes the date command and replaces the enclosed command with the output of the command. Two commands are used in a pipeline and the output used as an argument to echo $ echo The number of files in the directory are ls | wc l

8.10 SHELL VARIABLES


A shell variable is a memory storage area that can be used to hold a value. A shell variable can be an environment variable or a user-defined variable. The environment shell variables tell the shell about the way an user account is set up. Some of the commonly used environment variables are listed as follows:

Some of the environment shell variables like HOME and SHELL are set automatically when the user logs in. Other variable like TERM maybe set by the user.

78
Example:

Chapter 8 - The Shell

If the user is working on a terminal of type vt100, the variable TERM is set to vt100. $ TERM=vt100 $ export TERM Users can define their own variables by giving the variable a name and value.User-defined variable names should begin with a letter and can contain letters, numbers and the underscore (_) character. Variable names are case-sensitive and are all of type string. Numbers are also stored as strings. The default value of a shell variable is always a null string. The syntax to assign a value to a variable is as follows: Variable=value There should be no space on either side of the = character. To display the value assigned to a variable, the name of the variable is preceded by a $. In a command line, any word preceded by a $ is recognized as a variable by the shell and replaces the word by its value. Example: Assign the value 100 to the variable abc $ abc=100 Display the value assigned to the variable abc $ echo $abc The value stored in one variable can be assigned to another variable. $ xyz=$abc $ echo $xyz assign a null string to a variable abc $ abc= A variable can be removed by using the shell internal command, unset. $ unset abc A multiword string can be assigned to a variable by the use of single quotes. Example $ abc=This is a multiword string

BSIT 43 Unix and Shell Programming

79

Variables can also be concatenated by placing them one beside the other. Example $ mesg1=This is a $ mesg2=sample concatenation $ mesgfull=$mesg1$mesg2 $ echo $mesgfull A variable can also be concatenated with a string. $ mesgfull=$mesg1sample concatenation

8.11 CONCLUSION
In this chapter we have learned about how a command line is interpreted by the shell. The command line words are parsed and variables are evaluated. We have also learned how to redirect standard output and standard error separately. Command substitution and setting up of pipelines were discussed. The shells programming features will be discussed in a later chapter.

EXERCISES
1. 2. 3. 4. 5. 6. 7. 8. 9. What is the file descriptor for standard out? standard in? standard error? Explain this command: cat > out.dat Write a command to pipe the output of cat into the more command? Write a command to append the file txt1 to the end of the file txt2. What does the character \ mean to the shell ? How can you remove a file named ?ops without removing the file wops ? How do you display the value of the shell variable PATH? What is this variable used for? How do find out the number of users logged in to the system ? What would be the effect of the following commands? i. ii. iii. iv. cat<file1>file2 wc l<aaa who|wc l>aaa cat aaa>bbb 2>ccc

Chapter 9

The Process

9.1 BASICS OF A PROCESS

process is born when a program starts execution and exists as long as the program is running. After execution the process is said to die.The name of the process is usually the name of the program being executed.

In Unix, the kernel is responsible for the process management. It determines the time and priority allocated to each process so that multiple processes can share the CPU resources. A process executes for a finite period of time and then gives control to another process. Sometimes the kernel stores pages of the processes in the swap area of the disk before calling them again for execution. Processes also have attributes. Some of the attributes are maintained in a structure called the process table by the kernel. The process table is stored in the memory. The important attributes of a process are: 1. The process-id (PID). Each process is identified by a unique number called the process-id which is alloted by the kernel when the process is born. 2. The parent PID (PPID) The PID of the parent is also a process attribute. When many processes have the same PPID it is easier to kill the parent process rather than the children separately. Other attributes are inherited by a child process from its parent. As soon as a user logs into the system, the kernel starts a process called the shell. The command representing the process could be any one of the shells, namely, sh, ksh, csh or Bash. Any Unix command typed at the shell prompt, is an input to the shell process. The shell process remains alive until the user logs out of the system.

80

Chapter 9 - The Process

BSIT 43 Unix and Shell Programming

81

The login shell process has a set of environment variables which can be displayed by the env command. $ env HOME=/college1/sibns HUSHLOGIN=FALSE HZ=100 LOGNAME=sibns MAIL=/usr/spool/mail/sibns PATH=/bin:/usr/bin:/college1/sibns/bin:. SHELL=/bin/sh TERM=vt100 TZ=IST-5:30 The value of environment variables can also be determined with the echo command. These variables are given default values or are set at login from the .profile. Each process can have different values for these variables. PATH determines the location and the order in which the shell will search for commands. TERM controls the emulation type of the current terminal session. The pathname of the shell is stored in the variable SHELL and the PID is stored in a special variable called $$. The shells PID can be displayed by typing. $ echo $$ 2814 The PID of the shell remains the same throughout the login session. The shell process gets killed once the user logs out. If the user logs in again, the shell PID changes. Every process has a parent which in turn is another process. For example when the command cat filename is run, the cat process is started by the shell process. In this case the shell is said to be the parent of cat and cat is called the child of the shell process. The multitasking feature of Unix allows a process to have one or more children. For example in the command $ cat filename | more Two processes cat and more are generated by the shell. Since, the shell itself is a process, there can be multiple login shell processes running (for every new terminal session a new shell process is opened). Any process in Unix may begin as a single process and then multiply into many processes. Shell processes, by default, run in the foreground. This means that the shell process (the parent) cannot do anything until the new process (the child) is completed. Processes can also run in the background which permits the shell process to continue working while the child process is running.

82 9.2 PROCESS STATUS: ps

Chapter 9 - The Process

The ps command is used to display some of the process attributes just as the ls command displays the file attributes. The ps command reads the kernels data structures and process tables to get the characteristics of the processes. Without options ps displays the processes associated with a user at the terminal. $ ps PID TTY TIME 3472 ttyp9 00:00:00 3473 ttyp9 00:00:00 CMD sh ps

The PID column gives the process ID, the TTY column names the terminal where the command was started, the TIME column shows the amount of CPU time used by the command and the CMD column shows the name of the command.. The several options of ps are listed below

Examples: Get a full listing of all the processes with the parent PID for each process

BSIT 43 Unix and Shell Programming

83

The processes being run by the user sibns

Shows processes of all users. System processes are not displayed.

9.3 SYSTEM PROCESS


In addition to the processes created by the user, there are a number of system processes also running.They are created during system startup and the system is in the multiuser state.The -e or -a option with ps will give a list of all the processes running on the system. All user and system processes are displayed $ ps e PID 0 1 2 TTY ? ? ? TIME 00:00:00 00:00:00 00:00:00 CMD sched init vhand

84
3 4 5 6 1942 43 47 48 36 74 87 53 55 ? ? ? ? tty01 ? ? ? ? ? ? ? ? 00:00:00 00:00:00 00:00:01 00:00:00 00:00:00 00:00:00 00:00:00 00:00:03 00:00:00 00:00:00 00:00:13 00:00:00 00:00:01 bdflush kmdaemon htepi_daemon strd login syslogd ifor_pmd ifor_pmd htepi_daemon strerr agent sco_cpd ifor_sld

Chapter 9 - The Process

On a Unix system with many users, a large number of processes will be listed. System processes display a ? in the TTY column because they are not associated with any terminal. These processes are called daemons. A daemon process is usually defined as a background process that does not belong to a terminal session. Daemons cannot be interrupted and become active only when they receive input. There are many system services performed by daemons, namely, network services, printing etc.

9.4 MECHANISM OF PROCESS CREATION


The creation of a process is in three phases and uses three system calls known as fork, exec and wait. fork is a system call that creates a new process from an existing process. The new process is called the child process, and the existing process is called the parent. The attributes of the parent and child are an identical image except for parameters like PID. The child gets a new PID and the process is forked. This is the mechanism used to multiply processes in the system.The child process overwrites the image with a copy of the programme that is to be executed by using the exec system call. No additional processes are created and the existing program is replaced with a new program. The PID of this process is the same as that of the child process which was forked. The wait system call is executed by the parent process to wait for the child process to get completed. The exit status of the child process is picked up by the parent process. When a process is forked, the parent and child have different PID and PPID but the child inherits the following attributes from its parent: 1. The real UID and real GID of the process. 2. The effective UID and GID of the process.

BSIT 43 Unix and Shell Programming

85

3. The process priority. 4. The current directory of the running process. 5. A description of all the files opened by the parent process. 6. Environment variables like HOME and PATH. To create a Unix shell, init forks and execs a getty for every active communication line in a multiuser system. The getty shows the login prompt on each terminal and sleeps. When the user logs in to the system at the terminal, the sleeping getty wakes up and does a fork and exec on the login program to check the login name and the password entered. If the login is successful, the login shell is created. The parent-child processes are shown below:

It is observed that the init process is an ancestor of the shell process. The init process again goes to sleep until the user logs out. When the user logs out, the shell gets killed and init wakes and another getty is born for the terminal until the next user logs in.

9.5 RUNNING JOBS IN BACKGROUND


Unix is a multitasking operating system and a number of programs can run at the same time. A command can be started and put in the background which can continue to run while another command is started in the foreground. This is particularly useful when a command has a longer execution time. To run a background process, the command is ended with an ampersand (&). Once a process is running in the background, the parent does not wait for the child process to die. When a background process is created, the following takes place:

The PID of the command entered is displayed. The PID is used to keep track of all processes currently running on the system. The prompt returns so that you can enter another command.

Because background processes increase the work load on the system, they may also slow down the rest of the system. Most processes, including background processes, send their output to standard output, ie., the terminal screen. Since, this may interfere with the other commands being run at the terminal, it is usually good practice to redirect the output of a background process to a file or to a printer. The output can then be examined whenever required.

86
Examples:

Chapter 9 - The Process

The find command runs in the background (by entering an ampersand [&]) and redirects its output to a file named ofile $ find / -type f -print >ofile & 24 When the background process starts, the system assigns it a PID (24 in this example), displays it, and then prompts the user for another command..In the Korn or C shell, job numbers are also assigned along with the PID. The PID of the last background process is stored in the system variable $!. The status of the process can be checked with the ps command. A process can be terminated with the kill command. A background process gets terminated when a user logs out. This is because the shell gets killed and when the parent dies, all the child procsses also get killed. This is taken care of by the nohup (no hangup) command. Example $ nohup find / -type f -print & 350 Sending output to nohup.out The shell dsplays the PID and the results of the find command are stored in the file nohup.out. The user can safely logout of the system without disturbing the process running in the background.

9.6 JOBS EXECUTION WITH LOW PRIORITY: nice


nice runs a command with an altered scheduling priority Syntax: nice [-increment | -n increment ] command [argument ... ]

The nice command is used to change the scheduling priority of a command being executed. Each process has a nice value which is used to calculate its priority. Nice values range from 0 to 39. Higher nice values result in lower priorities. By default, when nice is not used, commands have a nice value of 20. Using nice executes a command with a nice value equal to 20 plus increment. If no increment is specified, a value of 10 is assumed giving a nice value of 30. The -n option is preferrably used to specify the increment. Only the superuser (root) can increase the priority of a user.

BSIT 43 Unix and Shell Programming

87

Example: Execute a command with a low priority value $ nice sort file1.lst > file1.srt & The nice value is specified $ nice n 10 sort file1.lst > file1.srt &

9.7 KILLING A PROCESS WITH SIGNALS


The occurrence of an event is communicated to a process by sending a signal to the process. These signals are identified by numbers and are designated to perform specific functions. Signals are also identified by symbolic names with the prefix SIG. Signals are generated by the keyboard or by the kill command. When a process needs to be terminated the kill command is used to send a signal to the process to kill it. The kill command is an internal command The kill command can be used with or without a signal id Syntax: kill [options] pid kill [options] -signal pid where pid is the process identification number (given by the ps command), and signal (which is optional) is the signal to be sent to the process. The default signal is number 15, the SIGTERM signal, which tells the process to terminate. The signal can be either a number or its symbolic name. The option l with kill lists all the signal names. The kill command allows you to kill one process ID or multiple process IDs. Example: Kill a process with PID 2984 $ kill 2984 Kill many processes at the same time $ kill 2984 5841 654 If all these processes have the same parent, then the parent PID can be killed to kill all the child processes. Killing a users login shell is the same as logging the user out.

88

Chapter 9 - The Process

Some processes ignore the kill signal, SIGTERM. In such case the following command can be used: $ kill 9 5841 Processes running in the background can also be killed. Without using the ps command to find out the PID of a background process, it can be killed by the following command: $ kill $!

9.8 EXECUTE LATER: at and batch


The commands at and batch are used to schedule jobs to run at specified times. at:The argument for the command at is the time at which the program is to be executed Example When the command is entered with a scheduled time as its argument, the at prompt is displayed. The name of the program to be executed at the time mentioned is typed in and [ctrl-d] is entered to exit at. The job is put in a queue and is executed at the time given as argument to the at command. $ at 17:30 at>sample.sh [ctrl-d] The standard output and standard error of the shell script, sample.sh will be sent to the mail box of the user. This can be opened later by mailx. The output can also be redirected to a file as follows: $ at 17:30 at>sampl.sh > sample.out [ctrl-d] The keywords now, noon, midnight, today and tomorrow can also be used with the command at. Example: $ at tomorrow at>sample.sh [ctrl-d] The command at with the option l, displays a list of all the jobs. The r option can be used to remove a job.

BSIT 43 Unix and Shell Programming

89

batch: The batch command is also used to schedule jobs for later execution. Unlike at, time is not specified here and jobs are executed as soon as the system load reduces. The batch command does not need any arguments. It uses an internal algorithm to determine the execution time. Jobs that require a lot of CPU time are not allowed to run at the same time, which could slow down the system. Example: $ batch < sample.sh A job scheduled with the batch command goes to a special at queue. The job can be removed from the queue by using the r option with at.

9.9 RUNNING JOBS PERIODICALLY: cron


A task can be automatically run in the background at regular intervals by a Unix utility called cron. The cron daemon takes care of running these background jobs, which are called cron jobs. crontab or the crontable is a file which contains the schedule of cron entries to be run and at specified times. cron checks the crontable at regular tables to see if there are any jobs scheduled. A user can execute crontab if the users name appears in the file /usr/lib/cron/cron.allow. If the cron.allow does not exist, the cron.deny file in /usr/lib/cron is checked. If the users name is not in this file the user is allowed to use crontab. If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow and deny files contain one user name per line. The command crontab can be used to create a crontab file: $crontab cron.txt The following options can be used with crontab: crontab -e crontab -l crontab r Edit your crontab file, or create one if it doesnt already exist. Display your crontab file. Remove your crontab file.

An editor has to be specified to open the crontab file. Each line in a crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

90
* * * * * command to be executed

Chapter 9 - The Process

day of week(1-7)(Sunday=1) month(1-12)


day of the month(1-31) hour (0-23)

min(0-59) Example: The following line in the crontab file removes all core files /user/user1 everyday at 7:30 PM. 30 19 * * * rm /user/user1/core The time schedule can be changed as follows to run the command at 5:30 PM on the 1st of Mar, June and September. 30 17 1 3,6,9 rm /user/user1/core By default cron jobs send an e-mail to the user account executing the cronjob. If this is not needed put the following command

9.10 TIMING PROCESSES: time


The time command can be used to find out how efficiently a program uses the system resources. The time command accepts the command line of the program to be timed as its argument.The program is executed and the time usage is displayed on the terminal.. Example: The time taken to perform a find operation can be displayed by preceding the find command with time. $ time find / -name core print real 0m28.815s user 0m2.390s sys 0m10.890s The real time shows the time elapsed between start of exection to end of execution. The user time shows the time taken by the program to execute. The sys time indicates the time used by the kernel to the

BSIT 43 Unix and Shell Programming

91

work on behalf of the user process. The CPU time is the sum of the user time and the sys time. This could be appreciably less when compared to the real time on a very busy system

9.11 CONCLUSION
In this chapter the discussion was about the relationship of files and processes. The creation of a process by using fork and exec, kernel communication with the process through a signal, scheduling a process for a one-time execution using at or running it with batch were also discussed. The use of cron to run a process repeatedly at regular intervals and timing of a process execution by using the tool time were discussed.

EXERCISES
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. What command will give you a listing of all running processes that are owned by you? What is the name and PID of your shell process? How can you tell if a process is linked to a terminal? How do you run a process in the background ? How can you terminate a running process? Open another terminal session. Find the pid of this process and kill it. What command did you use? What is the significance of -9 in the command: kill -9 pid? What is a daemon process ? How do you kill a process running in the background ? What does the following crontab entry mean ? 30 15 * * * sort file1.lst > file1.srt 11. What would be the following commands do? a) at 18:32 tomorrow echo Happy Birthday <ctrl-d> b) 12. 13. nohup cat bigfile&

What is the difference between scheduling processes using batch and at command? Frame a crontab entry to execute the connect.sh script every 30 minutes on every Monday,Wednesday and Friday between 8am and 6pm.

Chapter 10

Communication and Electronic Mail

10.1 WRITE AND TALK

rite and talk are communication programs that allow users to talk to each other in a network.

The write command copies lines from one users terminal to that of another user. The command is used by typing the following $ write username Where username is another logged in user. The recipient gets a message from the sender along with the senders username. If the recipient of the message now writes back, communication will start between the two users and continues until an end-of-file is read from the terminal or an interrupt is sent. Communication terminates and write displays (end of message) on the other terminal and exits. Conventionally, each user should end every message with a distinct signal (o) for over and end the conversation with (oo) for over and out . If the recipient user is logged on to more than one terminal, the ttyname is used to indicate the appropriate terminal. The program talk is a visual communication program which copies lines from a users terminal to that of another user. It allows text-based communication between two users. For example, if user1 wishes to talk to user2 who should be logged into the the system, user1 types the following:

92

Chapter 10 - Communication and Electronic Mail

BSIT 43 Unix and Shell Programming

93

$ talk user2 The screen on user1s terminal immediately splits into half with a waiting for response message at the bottom of the screen. A message with the senders name also appears on user2s terminal. To have a conversation with user1, user2 should also type the following command $ talk user1 The screen on user2s terminal also splits into half and the conversation in real-time starts. Both user1 and user2 can send messages simultaneously. All incoming messages are displayed in the bottom half and all the outgoing messages in the top half of the screen. Either user1 or user2 or both by typing ctrl-c terminate the talk program. The terminal screen is restored to its previous state. To talk to a user who is logged in to more than one terminal, the ttyname is used to indicate the appropriate terminal name.

10.2 REFUSING AND ACCEPTING MESSAGES


Permission to receive messages from other users of the system may be granted or denied by use of the mesg command. By default, for reasons of security, users are not allowed to receive messages. This can be altered by the mesg command. mesg - allow or deny messages sent to the terminal Syntax: mesg [n] [-n] [y] [-y] The mesg command with argument n denies messages sent by write or talk. This is done by revoking the non-user write permission on the users terminal. The mesg command with argument y restores permission. When mesg is used without arguments, it reports the current state without changing it.

10.3 E-MAIL BASICS


E-mail messages are stored in the userss mailbox as and when they are received. There are two kinds of mailboxes in the system - the system mailbox and the personal mailbox. The system mailbox is a file that has all the mail received but not read, deleted or saved to another file. The system mailbox is generally located in the systems /var/spool/mail directory and has the same name as that of the users login name. For example, for a user with a login name, user1, the system mailbox file will be /var/spool/mail/user1.

94

Chapter 10 - Communication and Electronic Mail

The personal mailbox is a file called mbox in the users home directory. Any mail message that is read and not saved in another file or deleted, is saved in the mbox file when the user quits the mail program. The Unix shell checks the mailbox at regular intervals (set in the MAILCHECK variable) for the arrival of new mails. If there are any new e-mail messages and the user logs in, the message you have mail is displayed. The incoming mail can be viewed, answered, forwarded, saved, deleted or printed. The address of the sender can also be saved in the address book. An e-mail message when viewed, displays the header information and the message body. The header information consists of the following: subject field - optionally filled up date and from fields - automatically filled up by the system to field - mail address of the user to whom the mail is being sent Cc field - send a carbon copy to another user The message body is displayed after the header information. The message body contains the text message and attachments, if any. If the mail address of the person to whom the mail is being sent does not exist, the e-mail is returned with an error message.

10.4 THE UNIVERSAL MAILER: mailx


mailx is a command line tool which can run non-interactively from shell scripts. mailx can be invoked in sending and receiving mode. In the sending mode, mailx becomes interactive. For example typing $ mailx user1 will first prompt the user for the subject of the message. The message body is then entered. Pressing [ctrl-d] indicates the end of the message. In some systems, mailx may ask the user for carbon copies to be sent. If so, the mail address of the person to whom a carbon copy of the message is to be sent is entered. Finally the mail is sent to user1s mailbox. mailx can also be used to send mails non-interactively with the -s option. For example, The message body, mesg.txt is entererd separately using the vi editor and mailed to user1 noninteractively with the s option. $ mailx -s hello user1 < mesg.txt The e-mail is appended to user1s mailbox /var/spool/mail/user1. To receive mails using mailx, the command is used without any arguments. In the list of messages

BSIT 43 Unix and Shell Programming

95

displayed, the first column gives the message status. All unread old messages are marked with a U. All new messages are marked with a N. The messages are numbered and the third column shows the login name of the sender. The fourth column displays the date and time the message was received. The fifth column gives the number of lines and characters in the message. The ampersand (&) in the last line is the mail prompt. mailx now waits for the users instructions. A help facility gives you the list of instructions that can be entered at the mail prompt. Some of the commands are listed below: Example: $ mailx SCO OpenServer Mail Release 5.0 Type ? for help. /usr/spool/mail/sibns: 2 messages 2 new N 2 user1 >N 1 root &2 Message 2: From sibns Thu Nov 13 14:23:04 2003 Return-Path: sibns Received: (from sibns@localhost) by sco5.sjce.ac.in (8.8.8/SCO5) id OAA00740 for sibns; Thu, 13 Nov 2003 14:23:04 +0530 (IST) Message-Id: <200311130853.OAA00740@sco5.sjce.ac.in> From: sibns@sco5.sjce.ac.in (sibns) X-Mailer: SCO OpenServer Mail Release 5.0 To: sibns Date: Thu, 13 Nov 2003 14:23:04 IST Status: R A copy of an editor buffer of your file out was saved when the editor was killed. This buffer can be retrieved using the recover command of the editor. An easy way to do this is to give the command vi -r out. Thu Nov 13 14:23 14/612 hello Fri Jul 25 15:38 14/521 Welcome to sco5

96
This works for edit and ex also. &q Saved 1 message in /college1/sibns/mbox Held 1 message in /usr/spool/mail/sibns

Chapter 10 - Communication and Electronic Mail

Typing 2 at the mailx prompt (&) displays the second message. The message headers and the message body are displayed. Typing q at the prompt will quit mailx and save message number 2 in the users personal mailbox, mbox. The following command is used to list mail messages stored in the users personal mailbox, mbox: $ mailx f

10.5 A FULL-SCREEN MAIL PROGRAM: pine


pine is a character-based menu driven mailer with the additional feature of an address box. Incoming and outgoing mails are sent into different files called folders. pine is invoked by typing pine at the Unix prompt. The pine main menu window is shown below:

BSIT 43 Unix and Shell Programming

97

To compose and send a message, the key C is pressed. The compose message screen appears as shown below:

The mail address of the recipient is entered. The address of the person to whom a copy is to be sent is also entered. Files can be attached to the mail message by entering the name of the file to be attached. The message subject which is optional should be short. The body of the message is finally entered and sent by pressing [ctrl-x] at the end of the message. A confirmation is asked to which a Y or a N is entered. Pressing Y will send the message, a copy of which is saved in the sent-mail file. [ine now returns to the main menu. I n pine, a new user has three mail files the inbox which holds messages received, sent-mail which has copies of messages sent and saved-messages which stores the mails saved. Pressing the I key in the main menu of pine will display the folder index, which is numbered list of message headers of all messages received. The mail header has the following structure: The first column shows a + or a blank. The + says that no carbon copies were sent to any other user. The secon column gives the status of the message- N for new, A for answered and S for saved. The remianing columns show the message number, the date , the name of the sender, the size of the message and the subject. To read a message, the message to be read is first selected by moving the cursor to the messag header. Once the cursor is positioned, typing the V key will open the message viewer and the body of the

98

Chapter 10 - Communication and Electronic Mail

message is displayed. To view an attachment the V key is pressed again which shows a list of items. The first item will be the message body and the second will be the attachment, if there is any. The cursor is moved to select the attachment and the V key is pressed yet again. The attachment opens and the text is displayed which can be saved to a file by typing the S key followed by a file name. Pressing the E key twice will exit both the attachment viewer and the attachment index and the user is returned to the message body. To reply to a message, in the message body, the R key is typed. The original message can be optionally included in the reply. The compose message screen appears and the reply can be entered and message sent by typing [ctrl-x]. Messages can be saved to the saved-messages file by typing the S key in the message text. The saved-messages file is selected by default and it be optionall changed to any other file name. Every pine screen has a help option and pressing the ? key will display the help screen. The help screen can be scrolled forward and backward by the spacebar and the hyphen (-) key respectively. Typing the Q key will quit pine after a confirmation. A summary of commands used above is listed: In the compose message screen:

In the message text viewer:

BSIT 43 Unix and Shell Programming

99

In the attachment text viewer:

10.6 DETAILS OF USERS:finger


finger is a versatile command having communicative features. when used without arguments, it simply produces a list of users: $finger Login henry root Name Tty Henry James superuser Idle *01 *04 Login Time 16 32 Where Fri 08:56 Fri 09:26

finger can also provide details of a single user: $finger henry Login:henry Directory:/usr/henry On since Feb 16 08:56 Name:Henry James Shell:/bin/sh on tty01 16 minutes idle time

10.7 CONCLUSION
In this chapter the online communication tools write and talk were discussed. E-mails and the structure of a mail message and the functioning of the two character-based mailers mailx and pine function were also discussed.

100
EXERCISES
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. What is the difference between write and talk ?

Chapter 10 - Communication and Electronic Mail

If a user is logged into more than one terminal, how do you send the user a message using write ? Find a user who has an account on the system and send a message using maix. What happened if you type q at the mailx prompt ? Where is a users personal mailbox located ? Using the redirection operator on the command line, mail the file mesg.txt to user1. Include the subject my message in the command line, in exercise no. 6. How is pine different from mailx ? How do you create an address book using pine ? Send a mail to user1 using pine.

Chapter 11

Simple Filters

11.1 THE SAMPLE DATABASE

onsider the following employee database of an organisation, from here onwards, youll be learning the features of several UNIX command filters with reference to a file emp.lst. The following is the emp.lst file of an XYZ institution.

This is a text file designed on fixed format containing employee records in XYZ institution. There are 10 lines, where each line has six fields separated from one another by the delimiter | (pipe). The details of an employee are stored in one line. A person is identified by the emp-id, name, designation, department, date of birth and salary (totally six fields in emp.lst database), as indicated by the fields (in the same order). Youll be using different UNIX filters (commands) on this file to see the powerful feature of UNIX filters.

BSIT 43 Unix and Shell Programming

101

102 11.2 PAGINATING FILES: pr

Chapter 11 - Simple Filters

This command i.e. pr, prepares a file for printing by adding suitable headers, footers and formatted text. This command has to be used with a file name as an argument.

pr adds five lines of margin at the top and five at the bottom. The header shows the date and time of last modification of the file, along with the file name and page number. Theres one option that uses a number prefixed by a + to print from a specific page number. Option (l) set the page length , option k (k is an integer) produces output in more than one column and other options are d(double space input),-n(number lines). pr l 54 emp.lst pr +5 emp.lst pr -3 emp.lst Page set to 54 lines Starts printing from page 5 print the file in three column

11.3 DISPLAYING THE BEGINNING OF FILE: head


The head command, as the name implies, displays the top of the file. When used without an option, it displays the first ten lines of the file.

BSIT 43 Unix and Shell Programming

103

You can use the n option to display the first n number of lines.

11.4 DISPLAYING THE END OF A FILE: tail


The tail command displays the end of the file (as opposite to head command). Like head, it displays the last ten lines when used without arguments. You can use the n option to display last n number of lines.

You can also display lines from the beginning of the file instead of the end. The +count option allows you to do that, where count represents the line number from where the selection should begin.

11.5 CUTTING A FILE VERTICALLY: cut


The features of the cut and paste commands will be illustrated with specific reference to the file shortlist, which stores the first five lines of emp.lst file.

104

Chapter 11 - Simple Filters

Note the use of the tee facility that saves the output in shortlist and also displays it on the terminal. We can extract both columns and fields from this file with the cut command. Columns are specified with c option and fields with f option.

11.5.1 Cutting Columns(-c)


To extract specific columns, you need to follow the c option with a list of column numbers, delimited by a comma. Ranges can also be used using the hyphen. Heres how we extract the name and designation field from shortlist:

Note that there should be no whitespace in the column list. Moreover, cut uses a special form for selecting a column from the beginning and up to the end of a line.

The expression 59- indicates column number 59 to the end of the line. Similarly, -4 is the same as 1-4 i.e. from column numbers 1 to 4.

11.5.2 Cutting Fields (-f)


The c option is useful for fixed length lines. Most UNIX files (like /etc/passwd and /etc/group) dont contain fixed-length lines. To extract useful data from these files youll need to cut fields rather than columns. (A field is a set of columns) cut uses the tab as the default field delimiter, but can also work with a different delimiter. Two options need to be used here are d for the field delimiter and f for the field list. To cut second and third field from a file shortlist, the following is the command.

BSIT 43 Unix and Shell Programming

105

(Note: tee command is used to store the result in a file cutlist1 and also display the result on screen). The | was escaped to prevent the shell from interpreting it as the pipeline character; alternatively, it can also be quoted (-d |). To cut out fields numbered 1,4,5,6, and save the output in cutlist2, following is the command.

cut can be used to extract the first word of a line by specifying the space as the delimiter. cut can be used to extract user list from who command output. The following is the command.
$ who | cut -d -f1 root snn mallu project jayanthi

cut is a powerful text manipulator often used in combination with other commands or filters.

11.6 PASTING FILES: paste


What you cut with cut command can be pasted back with the paste command but vertically rather than horizontally. You can view two files side by side by pasting them. In the previous topic, cut was used to create the two files cutlist1 and cutlist2 containing two cut-out portions of the same file. Using paste, you can fix them. The following command does pasting job.

106

Chapter 11 - Simple Filters

The original contents have been restored to some extent, except that the fields have different relative locations, and pasting has taken place on whitespace. Like cut, paste also uses the tab as the default delimiter, but you can specify one or more delimiters with d:

Joining Lines (-s) paste is more useful than you might think. Consider this address book that contains details of three persons, with three lines for each:
$ cat addressbook Mallu Mallu@yahoo.com 2546131 Maharaj Maharaj@rediffmail.com 2512657 Nagendra san@sjce.ac.in 2121134

The s option joins lines in the same way vis J commands does. Using this option on this file (with paste s addressbook) would join all these nine lines to form a single line. This probably wont be of much use, but if we use the d option with multiple delimiters (say, the string ||\n), then we can join three lines at a time. The delimiters then are used in a circular manner; the first and second lines would be joined with the | as delimiter, and the same would be true for the second and third line. The third and fourth line would be separated by a new line. After the list is exhausted it is reused. The following is the command.
$ paste -s -d ||\n addressbook Mallu|Mallu@yahoo.com|2546131 Maharaj|Maharaj@rediffmail.com|2512657 Nagendra|san@yahoo.com|2121134

BSIT 43 Unix and Shell Programming

107

11.7 ORDERING A FILE: sort


Sort command in UNIX is used to orders a file. Like cut, it identifies fields and it can sort on specified fields. Well consider the important sort options by sorting the file shortlist in different ways. By default, the entire line is sorted.

By default, sort reorders line in ASCII collating sequence i.e. the whitespace first (blanks), then numerals, uppercase letters and finally lowercase letters. This default sorting sequence can be altered by using certain options. You can sort on one or more keys (fields).

11.7.1 Sort Options


Unlike cut and paste, sort uses the space as the default field separator. Whereas in cut and paste we have used tab as the default field separator. Here well use the t option to specify the delimiter. Sorting on primary key (-k): Lets now use the k option to sort on the second field (i.e name). The option should be k 2:

The sorting order can be reversed with r (reverse) option. The following sequence reverses a previous sorting order:

108

Chapter 11 - Simple Filters

Sorting on secondary key: You can sort on more than one key i.e. you can provide a secondary key to sort. If the primary key is the third field, and the secondary key is the second field, then you need to specify for every k option, where sort ends. This is done in this way:

This sorts the file by designation and name. k 3,3 indicates that sorts starts on the third field and ends on the same field. Sorting on columns: You can also specify a character position within a field to be the beginning of sort. If you are to sort the file according to year of birth, then you need to sort on the seventh and eight columns position within the fifth field.

The k option also uses the form k m.n, where m is the character position in the mth field. So, 5.7, 5.8 mean that sorting starts on column 7 of the fifth field and ends on column 8. Numeric sort (-n): When sort acts on numerals, strange things can happen. When you sort a file containing only numbers, you get a curious result:
$ sort numfile 10 2 27 4

This is probably not what you expected, but the ASCII collating sequence places 1 above 2, and 2 above 4. Thats why to 10 proceeded 2 and 27 proceeded 4. This can be overridden by the n (numeric) option.
$ sort -n numfile 2 4 10 27

BSIT 43 Unix and Shell Programming

109

Other sort options: Even though sorts output can be redirected to a file using redirection operators (> or >>), we can use its o option to specify the output filename. Curiously enough, the input and output filenames can even be the same.
$ sort -o sortedlist -k 3 shortlist ouput stored in sortedlist $ sort -o shortlist shortlist output stored in the same file

To check whether the file has actually been sorted in the default order, use the c (check) option.

You can also add the k option to the above to check whether a specific field is sorted. When sort is used with multiple filenames as arguments, it concatenates them and sort them collectively. The m (merge) option can merge two or more files that are sorted individually.

11.8 LOCATE REPEATED AND NON REPEATED LINES: uniq


When you concatenate or merge the files, youll face the problem of duplicate entries creeping in. UNIX offers a special tool to handle these lines the uniq command. Consider a sorted file dept.lst that includes repeated lines:
$ cat dept.lst 01|Civil Engineering 02|Mechanical Engineering 02|Mechanical Engineering 03|Electronics & Communication 04|Computer Science

110
04|Computer Science 05|Information Science 05|Information Science

Chapter 11 - Simple Filters

uniq simply fetches one copy of each line and writes it to the standard output.

Since uniq requires a sorted file as input, the general procedure is to sort file and pipe its output to uniq. The following pipeline also produces the same output, except that the output is saved in a file uniqlist:
$ sort dept.lst | uniq - uniqlist

uniq is indeed unique; if provided with two filenames as arguments, uniq will read the first file and write its output to the second. Here, it reads from the standard input and writes to uniqlist. uniq options are u to select the nonrepeated line,-d to select the duplicate lines and c to count the frequency of occurrence.

11.9 TRANSLATING CHARACTERS: tr


The tr (translate) filter manipulates individual characters in a line. More specifically, it translates characters using one or two compact expressions: tr options expression1 expression2 standard input Note that tr takes input only from the standard input; it doesnt take a filename as argument. By default, it translates each character in expression1 to its mapped counterpart in expression2. The first character in the first expression is replaced with the first character in the second expression, and similarly for the other characters. tr options are d to delete characters,-s to compress multiple consecutive characters and c to complement the expression. Lets use tr to replace the | with a ~ (tilde) and the / with a -.

BSIT 43 Unix and Shell Programming

111

Here is a command that converts all the upper case letters in a file dept.lst into lower case letters. The following is the command: Note that the lengths of the two expressions should be equal. If they are not, the longer expression will have unmapped characters. Single quotes are used here because no variable evaluation or command substitution is involved.

11.10 SEARCHING FOR A PATTERN: grep


The grep command in UNIX is used to search for a pattern in a given file. It displays the selected pattern, the line numbers or the filenames where the pattern occurs. The syntax is given below: Syntax: grep options pattern filename(s) grep searches for pattern in one or more filenames. The first argument is the pattern and ones remaining are filenames. Lets use grep to display lines containing the string Information from the file emp.lst; the content of this file are shown below:

We can also store the output of a grep to a file: $ who | grep kumar > user.lst Its generally safe to quote the pattern; if the string contains only one word quoting is not required where as if the string contains more than one word quoting is mandatory.

112 11.10.1 grep options

Chapter 11 - Simple Filters

grep is one of the most important UNIX commands, and you must know its different options. Ignoring Case (-i) When you look for a name, but are not sure of the case, grep offers the i option which ignores case for pattern matching:
$ grep -i anantha emp.lst 1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000

This locates the name S.K.Anantha. Deleting Lines (-v) grep can play an inverse role too; the v (inverse) option selects all except lines containing the pattern. Thus, you can create a file otherlist containing all but Lecturer.
$ grep -v Lecturer emp.lst > otherlist

The above UNIX command stores the result in a file called otherlist. The content of the file is as follows:

Displaying Line Numbers (-n) The n (numbers) option displays the line numbers containing the pattern, along with the lines:

The line numbers are shown at the beginning of each line, separated from the actual line by a colon(:). Counting Lines Containing Pattern (-c) How many Lecturers are there in the file? The c (count) option counts the number of lines containing the pattern (which is not the same as number of occurrences). The following shows that there are six of them:
$ grep -c Lecturer emp.lst 6

Matching Multiple Patterns (-e) With the e option, you can match the two Mahanand by using grep like this:

BSIT 43 Unix and Shell Programming

113

$ grep -e mallu -e MALLU emp.lst 1002|Mallu |Lecturer |Information Science|20/07/74|15000

Note: If the emp.lst file contains two mahanands like above pattern (Mahanad and MAHANAND) the grep command displays those two lines also.

11.11 CONCLUSION
This chapter presented some of the commonly used filters available in the UNIX system. You can use above filters in pipelines to perform tasks that apparently seem to difficult to achieve by conventional means. Finally you have seen grep command that is used to search for a pattern in a file with different options.

EXERCISES
1. 2. 3. 4. 5. 6. 7. Use head and tail to select lines 5 to 10 of a file. Generate a code list by selecting departments from emp.lst Remove repeated lines from a file. Covert the content of file emp.lst to uppercase. How will you find out the number of times the character ? occurs in a file? Mail a sorted list of users currently logged in to root, taking account of the possibility that a user may be logged in more than once. How to perform the following operation: a) b) c) d) Merge the contents of file f1 with input supplied from the keyboard and store output in a file f2. Search all lines in a file which end with semicolon. Search all lines in a file which donot end with semicolon. Merge and sort the contents of files a, b, c and display the sorted output on the screen.

Chapter 12

Shell Programming

U
cal date who

ntil now, you have been giving commands to the UNIX shell by typing them on the keyboard. When used this way, the shell is said to be a command interpreter. The shell can also be used as a high level programming language. Instead of entering commands one at a time in response to the shell prompt, you can put a number of commands in a file, to be executed all at once by the shell. A program consisting of shell commands is called a shell script or shell program .

12.1 A SIMPLE SHELL SCRIPT


Suppose you were to make up a file named commands containing following lines: # A simple shell script

The first line in this file begins with a # symbol, which indicates a comment line. Anything following the#, up to the end of the line, is ignored by the shell. The remaining three lines are shell commands: the first produces a calendar for the current month, the second gives the current date and time, and the third lists the users currently logged onto your system. We can get the Bourne Shell (sh) to run these commands by typing $ sh < commands The redirection operator (<) tells the shell to read from the file commands instead of from the standard

114

Chapter 12 - Shell Programming

BSIT 43 Unix and Shell Programming

115

input. It turns out, however, that the redirection symbol is not really needed in this case. Thus, you can alsorun the commands file by typing $ sh commands Is there any way to set up commands so that you can run it without explicitly invoking the shell? Inother words, can you run commands without first typing sh? The answer is yes, but you first have to makethe file executable. The chmod utility does this: $ chmod u+x commands The argument u+x tells the file name commands that assign execute permission to owner of the file commands. Now all you need do is enter the file name $ commands If your search path is set up to include the current directory, the shell will run the commands in the file.If it is not, the shell will complain that it cannot find the file you want it to execute: commands not found If this happens, you can still get the shell to run your command this way: $ ./commands Remember, dot (.) stands for the current working directory.

12.2 SUBSHELLS
When you tell the shell to run a script such as the commands file, your login shell actually calls up another shell process to run the script. (Remember, the shell is just another program, and UNIX can run more than one program at a time.) The parent shell waits for its child to finish, then takes over and gives you a prompt: $ Incidentally, a subshell can be different from its parent shell. For example, you can have ksh, csh, tcsh or bash as your login shell, but use sh to run your shell scripts. Many users in fact do this. When it comes time to run a script, the login shell simply calls up sh as a subshell to do the job. We will always use sh for running shell scripts. To make sure that sh is used, we will include the following line at the top of each shell script file. #!/bin/sh Thus, our commands file would look something like this.

116
#!/bin/sh # A simple shell script cal date who

Chapter 12 - Shell Programming

12.3 THE SHELL AS A PROGRAMMING LANGUAGE


The sample script commands is almost trivial - it does nothing more than execute three simple commands that you could just as easily type into the standard input. The shell can actually do much more. It is, in fact, a sophisticated programming language, with many of the same features found in other programming languages, including

Variables Input/output function Arithmetic operations Conditional expressions Selection structures Repetition structures

We will discuss each of these in order.

12.4 VARIABLES
Shell variables are an integral part of shell programming. They provide the ability to store and manipulate information within a shell program. There are three types of variables commonly used in Bourne Shell scripts:

Environment Variables: Sometimes called special shell variables, keyword variables, redefined shell variables, or standard shell variables, they are used to tailor the operating environment to suit your needs. Examples include PATH, TERM, HOME, and MAIL. User-defined Variables: These are variables that you create yourself. Positional Parameters: These are used by the shell to store the values of command-line arguments.

BSIT 43 Unix and Shell Programming

117

Of these, the environment variables and user-defined variables have been introduced already. The positional parameters, however, are new, and since they are very useful in shell programming, we will examine them in some detail now. The positional parameters are also called read-only variables, or automatic variables, because the shell sets them for you automatically. They capture the values of the command-line arguments that are to be used by a shell script. The positional parameters are numbered 0, 1, 2, 3,,9. To illustrate their use, consider the following shell script, and assume that it is contained in an executable file named display.args: #!/bin/sh # Illustrate the use of positional parameters echo $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 Suppose you run the script by typing the command line $ display.args I LOVE INDIA. The shell stores the name of the command display.args in the parameters $0: it puts the argument I in the parameter $1; it puts LOVE in the parameter $2, and INDIA. in parameter $3. Since that takes care of all the arguments, the rest of the parameters are left empty. Then the script prints the contents of the variables: display.args I LOVE INDIA. What if the user types in more than nine arguments? The positional parameter $* contains all of the arguments $1, $2, $3, ....$9, and any arguments beyond these nine. Thus, we can rewrite display.args to handle any number of arguments: #!/bin/sh # Illustrate the use of positional parameters echo $* The parameter $# contains the number of arguments that the user typed. We can modify the script display.args once again to use this parameter: #!/bin/sh # Illustrate the use of positional parameters echo You typed $# arguments : The arguments are: $* Suppose we were then to type the command line $ display.args Welcome to the world of UNIX The computer would respond with

118

Chapter 12 - Shell Programming

You typed 6 arguments: The arguments are : Welcome to the world of UNIX The following table shows the shell parameter and its significance:

Shell Parameter $1,$2. $# $0 $* $? $$ $! $@

Significance Positional parameters representing command line arguments Number of arguments specified in command line Name of executed command(shell script) Complete set of positional parameters as a string Exit status of last command PID of the current shell PID of the last background job Each quoted string treated as a separate argument

12.5 INPUT USING THE READ STATEMENT


The positional parameters are useful for capturing command-line arguments but they have a limitation: once the script begins running, the positional parameters cannot be used for obtaining more input from the standard input. For this you have to use the read statement. Lets modify the previous program to make use of read: #!/bin/sh # Illustrate the use of positional parameters, user-defined # Variables and the read command. echo What is your name? read name echo Well, $name, you typed $# arguments: echo $* In this script, name is a user-defined variable. The read command obtains the users response and stores it in name. With this modification, the script display.args works something like this: $ display.args Welcome to the world of UNIX The shell script would respond by prompting you for your name:

BSIT 43 Unix and Shell Programming

119

What is your name? Suppose you were to type Sachin Tendulkar It stores in the variable name and to access the value of variable use $prompt. The computer would respond with Well, Sachin Tendulkar, you typed 6 arguments: Welcome to the world of UNIX

12.6 THE set COMMAND


The positional parameters are sometimes called read-only variables, because the shell sets their values for you when you type arguments to the script. However, you can also set their values using the set command. To illustrate this, consider the following shell script, which we will assume is in the file setdate: #!/bin/sh # Demonstrate the set command set date echo Time: $4 $5 echo Day: $1 echo Date: $3 $2 $6 Assuming that setdate has been made executable with the chmod command, we can run the script by typing the command $ setdate The output will look something like this: Time:10:56:08 EST Day: Fri Date: 20 Aug 2004 What happened? Consider the command line set date The backquotes run the date command, which produces output something like this: Fri Aug 20 10:56:08 EST 2004

120

Chapter 12 - Shell Programming

This does not appear on the screen. Instead, the set command catches the output and stores it in the positional parameters $1 through $6: $1 contains Fri $2 Contains Aug $3 contains 20 $4 contains 10:56:08 $5 contains EST $6 contains 2004

12.7 ARITHMETIC OPERATIONS USING THE expr UTILITY


The shell is not intended for numerical work-if you have to do a lot of calculations, you should consider C, C++, or Fortran. Nevertheless, the expr utility may be used to perform simple arithmetic operations on integers. (expr is not a shell command, but rather a separate UNIX utility; however, it is most often used in shell scripts.) To use it in a shell script, you simply surround the expression with backquotes. For example, lets write a simple script called add that adds two numbers typed as arguments: # /bin/sh # Shell Script to Add two numbers sum=expr $1 + $2 echo $sum Here we defined a variable sum to hold the result of the operation. (Note the spaces around the plus sign, but not around the equals sign). To run this script, we might type the following line: (assume the script add is executable) $ add 4 3 The first argument (4) is stored in $1, and the second (3) is stored in $2. The expr utility then adds these quantities and stores the result in sum. Finally, the contents of sum are echoed on the screen: 7 $ The expr command only works on integers (i.e., whole numbers). It can perform addition (+), subtraction (-), multiplication (*), integer division (/), and integer remainder (%).

BSIT 43 Unix and Shell Programming

121

12.8 CONTROL STRUCTURES


Normally, the shell processes the commands in a script sequentially, one after another in the order they are written in the file. Often, however, you will want to change the way that commands are processed. You may want to choose to run one command or another, depending on the circumstances; or you may want to run a command more than once. To alter the normal sequential execution of commands, the shell offers a variety of control structures. There are two types of selection structures, which allow a choice between alternative commands:

if/then/elif/else/fi case

There are three types of repetition or iteration structures for carrying out commands more than once:

for while until

12.9 THE if STATEMENT AND test COMMAND


The if statement lets you choose whether to run a particular command (or group of commands), depending on some condition. The simplest version of this structure has the general form: if conditional expression then command(s) fi

When the shell encounters a structure such as this, it first checks to see whether the conditionalexpression is true. If so, the shell runs any commands that it finds between the then and the fi (which isjust if spelled backwards). If the conditional expression is not true, the shell skips the commands betweenthen and fi. Here is an example of a shell script that uses a simple if statement: #!/bin/sh set date if test $1 = Sun then

122
echo Thank goodness its Sunday! fi

Chapter 12 - Shell Programming

Here we have used the test command in our conditional expression. The expression test $1 = Sun checks to see if the parameter $1 contains Sun; if it does, the test command reports that the condition is true, and the message is printed. The test command can carryout a variety of tests; some of the arguments it takes are listed in Table 12-1.

12.10 THE elif AND else STATEMENTS


We can make the selection structures much more elaborate by combining the if with the elif (elseif) and else statements. Here is a simple example: #!/bin/sh set date if test $1 = Fri then echo Thank goodness its Friday! elif test $1 = Sat | | test $1 = Sun

BSIT 43 Unix and Shell Programming

123

then echo You should not work on weekends. echo Log off and go home. else echo It is not yet the weekend. echo Get to work! fi Here, the first conditional expression is tested to see if the day is a Friday. If it is, the message Thankgoodness its Friday! is printed, and the shell script is finished. If not, the second conditional expressionis tested. Note that we have used the OR operator ( | | ) in this expression to test whether the day is aSaturday or Sunday, in which case the second set of message will printed, and the script is finished.Otherwise, the third set of messages is printed. We could make even more elaborate selection structures by including more elif clauses. The importantthing to note about such structures is that only one of the alternatives may be chosen; as soon as one is,the remaining choices are skipped.

12.11 THE case STATEMENT


The shell provides another selection structure that may run faster than the if statement on some UNIXsystems. This is the case statement, and it has the following general form: case word in pattern-l) command(s) ;; pattern-2) command(s) ;; pattern-N) command(s) ;; esac The case statement compares word with pattern; if they match, the shell runs the command(s) on thefirst line. Otherwise, the shell checks the remaining patterns, one by one, until it finds one thatmatchesthe word; it then runs the command(s) on that line. Here is a simple shell script that uses the case statement: #!/bin/sh set date case $1 in

124
Fri) echo Thank goodness its Friday!;; Sat | Sun) echo You should not work on week-ends; echo Log off and go home!;; *) echo It is not yet the weekend.; echo Get to work!;; esac

Chapter 12 - Shell Programming

There are a few points to note about this structure. First, commands are separated by semicolons (;)and the end of a group of commands is indicated by two semicolons (;;) The OR symbol used in casestatements is a single vertical line (|), not the double vertical lines ( | | ) used in the if statement. The lastpattern (*) marks the default case, which is selected if no other pattern is matched.

12.12 for Loops


Sometimes we want to run a command (or group of commands) again and again. This is callediteration, repetition, or looping. The most commonly used shell repetition structure is the for loop,which has the general form for variable in list do command(s) done

Here is a simple application of the for loop: #!/bin/sh for name in $* do ps u $name done Each time through the for loop, the user-defined variable name takes on the value of the next argument n the list $*. This is then used as the argument to the ps u command (used to see the PID of the specified user). Assuming this script is contained in the executable file psall, it would be run by typing the name of the file, followed by the login names you wish to see the PID status: $ ps u kumar rao sachin

BSIT 43 Unix and Shell Programming

125

12.13 while Loops


The general form of the while loop is: while condition do command(s) done

As long as the condition is true, the commands between the do and the done are executed. Here is an example of a shell script that uses the expr utility with the while loop to echo the keyboard entry five times. #!/bin/sh # Print a message five times count=5 while test $count -gt 0 do echo $* count=expr $count l done

12.14 until Loops


Another kind of iteration structure is the until loop. It has the general form: until condition do command(s) done

This loop continues to execute the command(s) between the do and done until the condition is true. We can rewrite the previous script using an until loop instead of the while loop: #!/bin/sh # Print a message five times

126
count=5 until test $count -eq 0 do echo $* count=expr $count 1 done

Chapter 12 - Shell Programming

12.15 CONCLUSION
This chapter presented the essential programming constructs offered by the shell. If you have written programs, you can realize that the domain of shell programming is quite different from that of any high level language. Shell scripts are ideal for integrating your existing applications. You should now be able to rite scripts up to moderate complexity. If you are the system administrator, youll need the services of hell scripting to automate the routine functions, using cron scheduling when needed.

12.16 EXERCISES
1. Define each of the following terms: shell script, comment, subshell child process, positional parameter, selection structure default case, repetition structure, iterationloop 2. 3. 4. 5. 6. 7. 8. Develop a script logic that allows only kumar and sachin (login names) to execute a program. Develop a script logic that executes script only from terminal tty01 and tty02. Write a shell script that accepts one or more filenames as arguments, and converts them all to uppercase provided they exist in the current directory. Write a shell script that accepts two directory names, foo1 and foo2, and deletes those files in foo2 which are identical to their namesakes in foo1. Write a shell script that lists files by modification time when called with lm and by access time called with la. By default, the script should show the listing of all files in the current directory. Write a shell script to display the processes in the system every 30 seconds five timesusing a (i) while loop (ii) for loop. Write a shell script which receives two filenames as arguments, it should check whether two files contents are same or not. If they are same then second file should be deleted. [Hint: Use cmp command to compare files].

Chapter 13

Developing SHELL Scripts

his chapter presents a number of shell scripts that make use of the programming features discussed in the previous chapter. Before listing the code for each script, we will show a summary (in the form of a man page) describing the script.

13.1 CREATING EXECUTABLE FILE: chex


If you are planning to write a lot of shell scripts; you will find it convenient to have a script that makes files executable. If we were to write our own man page for such a script, it might look like Listing 13-1A.

BSIT 43 Unix and Shell Programming

127

128

Chapter 13 - Developing Shell Scripts

The shell script itself is shown in Listing 13-1B. Take a moment to study the outline, then create the script: 1. Use the text editor to create the script file. Open a file named chex and enter the script shown in the listing. Write and quit the file when finished. 2. Make the chex file executable. This requires you to change the access privileges on the file. One way to do this is to tell the shell to run chex on itself. Try this command line: $ sh chex chex

Listing 13-1B: The chex script.

This tells the shell to run chex, taking chex itself as the argument. The result is that chex makes itself executable. The output from this command will look some-thing like this: chex is now executable: -rwxr-xr-x 1 yourlogin 59 Date time chex Now you can use chex to change the protections on other files.

13.2 ASSIGNING LABELS TO wc (WORD COUNT) COMMAND: mywc


The wc (word count) filter counts the words, lines, and characters in a file. For example, try running wc on the chex file you have just created: $ wc chex 5 17 84 chex $ The output tells us that there are 5 lines, 17 words, and 84 characters in the file chex. This can be very useful information, but it would be a bit more convenient to use if the output were labeled. Listing 13- 2A summarizes the script; Listing 13-2B shows the actual shell script.

BSIT 43 Unix and Shell Programming

129

1. Use the text editor to create the script file. Open a file named mywc and enter the script shown in Listing 13-2B. Write and quit the file when finished.

2. Make the script executable. Use chex for this: $ chex mywc mywc is now executable: -rwxr-xr-x 1 yourlogin 59 Date time mywc 3. Test the new script. You might run mywc on the file chex: $ mywc chex File: chex Lines: 5 Words: 17 Characters: 84

130 13.3 DELETING FILES SAFELY: del

Chapter 13 - Developing Shell Scripts

The rm command can be very dangerous because it allows you to remove a file, but does not give you a way of getting back a file you may have removed accidentally. Most shells allow you to create an alias for the rm with the -i (interactive) option; it will ask you if you are sure you want to remove the file in question. But sh does not allow aliases. Lets create a script that will duplicate the effect of the rm I command. The script will also tell what action has been taken. Listing 13-3A summarizes the script; Listing 13-3B shows the script itself. After studying the listings, create the script: 1. Use the text editor to create the script file. Open a file named del and enter the script shown in Listing 13-3B. Write and quit the file when finished. 2. Make the script executable. Use chex for this: $ chex del del is now executable: -rwxr-xr-x 1 yourlogin 347 Date time del 3. Test the new script. Use del on a file you no longer need.

BSIT 43 Unix and Shell Programming

131

13.4 A DAILY REMINDER SYSTEM: tickle


We have already seen how you can use the calendar utility to remind yourself of important events. One limitation of calendar is that you have to enter each event individually, along with its date. This can be a problem for routine events that happen every day or every week. For reminding yourself of such events, the following tickle script can be very useful. It uses the date command to check the day of the week, then prints out an appropriate message.

132

Chapter 13 - Developing Shell Scripts

After studying the listings, create the script: 1. Use the text editor to create the script file. Open a file named tickle and enter the script shown in Listing 31-4B. Write and quit the file when finished. 2. Make the script executable. Use chex for this: $ chex tickle tickle is now executable: -rwxr-xr-x 1 yourlogin 457 Date time tickle 3. Test the news script. Enter the tickle command: $ tickle This should print out the appropriate message for the day.

13.5 DISPLAYING ARGUMENTS MULTIPLE TIMES: echo.sh


The standard echo command echoes its arguments just once. The script presented in Listing 13-5A and Listing 13-5B echoes its arguments as many times as the user chooses. Be sure you understand the listings, then create the script:

BSIT 43 Unix and Shell Programming

133

1. Use the text editor to create the script file. Open a file named echo.sh and enter the script shown in Listing 13-5B. Write and quit the file when finished. 2. Make the script executable. Use chex for this. $ chex echo.sh echo.sh is now executable -rwxr-xr-x 1 yourlogin 111 Date time echo.sh

134
3. Test the new script. Try this: $ echo.sh 5 Play it again, Sachin.

Chapter 13 - Developing Shell Scripts

The first argument (5) will be read in (stored in $1), then the rest of the command line will be repeated five times: Play it again, Sachin. Play it again, Sachin. Play it again, Sachin. Play it again, Sachin. Play it again,Sachin.

13.6 EXERCISES
1. 2. Write a shell script modch.sh that uses chmod to change the access permissions on a file so that only the owner may read, write, or execute it. Be sure to label the output to show what was done to the file. Modify the del script so that it detects whether the user has specified a directory to be deleted, in which case the script should call the rmdir command. (Hint: use test with the -d option to test for a directory.) i. Rewrite tickle to use an if/then/elif.../fi structure. ii. Rewrite echo.sh to use an until loop. iii. Write shell scipt lshead.sh that uses ls l command, to display the heading for ls l output.

Você também pode gostar