Escolar Documentos
Profissional Documentos
Cultura Documentos
BSIT - 43
Kuvempu University
In col l aborati on w i th
II
First Edition : May 2005 Second Edition : December 2010 Third Edition : May 2012
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 :
III
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
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
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
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
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.
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.
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.
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:
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.
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
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
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
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
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
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
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
13
$cal 1
14
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
15
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
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
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
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
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.
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
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:
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
PID COMMENTS 34 35 36
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
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
22
Syntax: stty [options] Examples:
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
23
24
Syntax: spell [ file ]
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.
25
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.
26
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
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
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.
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.
30
Example: $ pwd /users/user1
pwd here tells that, you are placed in the directory users, which has the parent directory users, which in turn is directory under root.
The Unix mkdir command is used to make directories. Example 1: create a subdirectory textfiles for user1 $ mkdir textfiles
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
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.
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
33
34
Examples: 1. One filename in each line $ ls -1 arith.lst exp.lst oper.c sqlscripts
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
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
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.
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
38
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.
40
2. Remove more than one file at a time $ rm file1 infile file2
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 *
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
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
43
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
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
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.
46
$ split file.txt
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
47
Example: $ comm fruits1 fruits2 apple grapes mango pineapple orange $ comm -23 fruits1 fruits2 mango $ comm -12 fruits1 fruits2 apple pineapple orange
48
Syntax: diff file1 file2
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
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
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:
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
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.
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.
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
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
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
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-
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
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
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
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.
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.
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.
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.
58
The eight possible umask permission combinations are listed below.
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.
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
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
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.
61
62
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:
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
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
The screen display can be controlled by scrolling the display forward and backward using the following keys:
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:
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
65
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
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
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
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
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.
68
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.
70
Example:
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?
71
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.
72
2. Standard output (stdout), the file representing output to the screen
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].
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
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
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
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.
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
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:
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
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
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
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.
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
83
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
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.
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.
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:
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.
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.
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 &
88
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 $!
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.
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
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
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
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
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.
94
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.
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
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
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
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:
99
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 ?
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
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.
101
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
103
You can use the n option to display the first 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.
104
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.
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.
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.
106
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
107
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).
The sorting order can be reversed with r (reverse) option. The following sequence reverses a previous sorting order:
108
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
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.
110
04|Computer Science 05|Information Science 05|Information Science
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.
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.
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.
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:
113
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 .
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
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
Variables Input/output function Arithmetic operations Conditional expressions Selection structures Repetition structures
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.
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
You typed 6 arguments: The arguments are : Welcome to the world of UNIX The following table shows the shell parameter and its significance:
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
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
120
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
121
if/then/elif/else/fi case
There are three types of repetition or iteration structures for carrying out commands more than once:
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
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.
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.
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
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.
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
125
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
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
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
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.
127
128
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
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.
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
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.
131
132
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.
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.
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.