Shell Programming for System Administrators Student Guide D61776GC21 Edition 2.1 April 2010 D66885
Copyright 2010, Oracle and/or its affiliates. All rights reserved. Disclaimer This document contains proprietary information, is provided under a license agreement containing restrictions on use and disclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except as expressly permitted in your license agreement or allowed by law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle. The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free. Sun Microsystems, Inc. Disclaimer This training manual may include references to materials, offerings, or products that were previously offered by Sun Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided. Oracle and its affiliates cannot be held responsible for any such references should they appear in the text provided. Restricted Rights Notice If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS The U.S. Government s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract. Trademark Notice Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark licensed through X/Open Company, Ltd.
This page intentionally left blank.
This page intentionally left blank.
Table of Contents About This Course...Preface-i Course Goal... Preface-i Course Overview... Preface-ii Course Map...Preface-iii Module-by-Module Overview... Preface-iv Course Objectives... Preface-vii Topics Not Covered...Preface-viii How Prepared Are You?... Preface-ix Introductions... Preface-x How to Use Course Materials... Preface-xi Conventions... Preface-xii Icons... Preface-xii Typographical Conventions...Preface-xiii UNIX Shells and Shell Scripts...1-1 Objectives... 1-1 What Is a Shell?... 1-2 What Are Shell Functions?... 1-3 Command-Line Interpreter... 1-3 Programming Language... 1-3 User Environment... 1-4 Solaris OS Shells... 1-5 The Bourne Shell... 1-5 The C Shell... 1-5 The Korn Shell... 1-6 Additional Solaris OS Shells... 1-7 The GNU Bourne-Again Shell... 1-7 The Desktop Korn Shell... 1-7 The Job Control Shell... 1-8 The Restricted Shell Command Interpreter... 1-8 The Enhanced C Shell... 1-9 Z Shell... 1-9 Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Learning Services, Revision B.1 5
Shells Discussed in This Course... 1-10 Subshells Child Processes... 1-11 What Is a Shell Script?... 1-13 Developing a Script... 1-14 Programming Terminology... 1-15 Logic-Flow Design... 1-16 The echoscript1.sh Example Bourne Script... 1-17 The echoscript2.ksh Example Korn Script... 1-18 The /etc/init.d/volmgt Example Boot Script... 1-19 Exercise: Reviewing UNIX Shells and Shell Scripts... 1-20 Preparation... 1-20 Tasks... 1-20 Exercise Summary... 1-22 Task Solutions... 1-23 Writing and Debugging Scripts... 2-1 Objectives... 2-1 Creating Shell Scripts... 2-2 Executing a Shell Script... 2-3 Executing the firstscript.sh Script... 2-4 Starting a Script With the #! Characters... 2-6 Putting Comments in a Script... 2-7 Adding the Debugging Statement... 2-9 Debug Mode Controls... 2-10 Example: Debug Mode Specified on the #! Line... 2-11 Example: Debug Mode With the set -x Option... 2-13 Example: Debug Mode With the set -v Option... 2-14 Syntax Comparison for Bourne and Korn Shell Options...2-15 Example: Debug Mode With the set -o noglob Option... 2-16 Exercise: Writing Shell Scripts... 2-17 Preparation... 2-17 Tasks... 2-17 Exercise Summary... 2-20 Task Solutions... 2-21 The Shell Environment... 3-1 Objectives... 3-1 User Startup Scripts... 3-2 The /etc/profile Script... 3-2 The $HOME/.profile Script... 3-2 The $HOME/.kshrc Script... 3-3 Modifying a Configuration File... 3-4 Shell Variables... 3-5 Creating Variables in the Shell... 3-7 Exporting Variables to Subshells... 3-9 Reserved Variables... 3-11 Special Shell Variables... 3-12 6 Shell Programming for System Administrators Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision B.1
Process Identification... 3-12 Exit Status... 3-12 Background Process Identification... 3-13 Quoting Characters... 3-14 A Pair of Single Quotes... 3-14 A Pair of Double Quotes... 3-15 Backslash... 3-15 The eval Command... 3-16 Shell Command Substitution... 3-17 Korn Shell Tilde Expansion... 3-19 Examples... 3-19 Arithmetic Operations on Bourne Shell Variables... 3-20 Arithmetic Operations on Korn Shell Variables... 3-23 Arithmetic Precedence... 3-24 The Korn Shell let Statement... 3-27 Script Math... 3-28 Korn Shell Aliases... 3-29 Removing Aliases... 3-29 Alias Inheritance... 3-29 Automatic (Tracked) Aliases... 3-30 Built-in Aliases... 3-31 Shell Parse Order... 3-32 Special Characters Precedence... 3-33 Exercise: Using the Shell Environment... 3-34 Preparation... 3-34 Tasks... 3-34 Exercise Summary... 3-37 Task Solutions... 3-38 Regular Expressions and the grep Command...4-1 Objectives... 4-1 The grep Command... 4-2 The grep Options... 4-3 Regular Expression Metacharacters... 4-5 Regular Expressions... 4-6 Escaping a Regular Expression... 4-7 Line Anchors... 4-9 Word Anchors... 4-10 Character Classes... 4-11 Character Match... 4-12 Closure (*)... 4-13 The egrep Command... 4-14 Exercise: Using Regular Expressions and the grep Command... 4-15 Preparation... 4-15 Tasks... 4-15 Exercise Summary... 4-17 Task Solutions... 4-18 Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Learning Services, Revision B.1 7
Using the sed Editor... 5-1 Objectives... 5-1 Introduction to the sed Editor... 5-2 Command Format... 5-3 Editing Commands... 5-4 Addressing... 5-5 Using sed to Print Text... 5-6 Using sed to Substitute Text... 5-8 Reading From a File for New Text... 5-10 Using sed to Delete Text... 5-11 Reading sed Commands From a File... 5-12 Using sed to Write Output Files... 5-13 Exercise: Using the sed Editor... 5-14 Preparation... 5-14 Tasks... 5-14 Exercise Summary... 5-16 Task Solutions... 5-17 The nawk Programming Language... 6-1 Objectives... 6-1 Introduction to the nawk Programming Language... 6-2 Format of the nawk Command... 6-3 Executing nawk Scripts... 6-3 Using nawk to Print Selected Fields... 6-4 Formatting With print Statements... 6-5 Using Regular Expressions... 6-7 The BEGIN and END Special Patterns... 6-9 Using nawk Scripts... 6-11 Using Built-in Variables... 6-12 Working With Variables... 6-13 Input Field Separator... 6-13 Output Field Separator... 6-15 Number of Records... 6-16 Exercise: Using nawk and Regular Expressions... 6-17 Preparation... 6-17 Tasks... 6-17 Exercise Solution... 6-18 User-Defined Variables... 6-19 Adding a Field of Numbers... 6-19 Variable Examples... 6-21 Writing Output to Files... 6-23 The printf() Statement... 6-24 Exercise: Using nawk to Create a Report... 6-26 Tasks... 6-26 Exercise: Using nawk Scripts to process Text Files... 6-27 Preparation... 6-27 8 Shell Programming for System Administrators Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision B.1
Tasks... 6-27 Exercise Summary... 6-29 Task Solutions... 6-30 Conditionals...7-1 Objectives... 7-1 The if Statement... 7-2 Parts of the if Statement... 7-4 Command... 7-4 Block of Statements... 7-4 End of the if Statement... 7-4 Exit Status... 7-5 Numeric and String Comparison... 7-6 Syntax for if/then/else Statements... 7-10 Syntax for if/then/elif/else Statements... 7-11 Positional Parameters... 7-14 Using if to Check Command-Line Arguments... 7-15 Creating the USAGE Message... 7-16 Using if to Check Leap Years... 7-18 Nested if Statements... 7-19 Testing File Objects... 7-21 Boolean AND, OR, and NOT Operators... 7-23 The case Statement... 7-26 Example of Using the case Statement... 7-27 Replacing Complex if Statements With a case Statement... 7-29 The exit Statement... 7-31 Exercise: Using Conditionals... 7-33 Preparation... 7-33 Tasks... 7-33 Exercise Summary... 7-36 Task Solutions... 7-37 Interactive Scripts...8-1 Objectives... 8-1 Input and Output in a Script... 8-2 The Korn Shell print Statement... 8-3 Examples of Using the print Statement... 8-4 Examples of Using the echo Statement... 8-5 The read Statement... 8-6 Examples of Using the read Statement... 8-7 Capturing a Command Result... 8-8 Printing a Prompt... 8-9 Prompting for Input Korn Shell Shortcut... 8-11 File Input and Output... 8-13 User-Defined File Descriptors... 8-14 File Descriptors in the Bourne Shell... 8-15 Korn Shell File Descriptors... 8-18 Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Learning Services, Revision B.1 9
The here Document... 8-20 Exercise: Using Interactive Scripts... 8-22 Preparation... 8-22 Tasks... 8-23 Exercise Summary... 8-25 Task Solutions... 8-26 Loops... 9-1 Objectives... 9-1 Shell Loops... 9-2 The for Loop Syntax... 9-3 The for Loop Argument List... 9-4 Using an Explicit List to Specify Arguments... 9-4 Using Variable Contents to Specify Arguments... 9-5 Using Command-Line Arguments to Specify Arguments... 9-6 Using Command Substitution to Specify Arguments... 9-6 Using File Names in Command Substitution to Specify Arguments 9-9 Using File-Name Substitution to Specify Arguments... 9-10 Exercise: Using for Loops... 9-12 Preparation... 9-12 Tasks... 9-12 Task Solutions... 9-13 The while Loop... 9-15 The while Loop Syntax... 9-15 Example of Using a while Loop... 9-17 Keyboard Input... 9-19 Redirecting Input for a while Loop... 9-20 The until Loop... 9-21 The break Statement... 9-22 The continue Statement... 9-23 Example of Using the continue Statement... 9-23 The Korn Shell select Loop... 9-26 The PS3 Reserved Variable... 9-26 Example of Using a select Loop... 9-27 Exiting the select Loop... 9-29 Submenus... 9-31 Example of Using Submenus... 9-31 The for and select Statements Revisited... 9-35 The shift Statement... 9-36 Example of Using the shift Statement... 9-37 Exercise: Using Loops and Menus... 9-39 Preparation... 9-39 Tasks... 9-39 Exercise Summary... 9-41 Task Solutions... 9-42 10 Shell Programming for System Administrators Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision B.1
The getopts Statement...10-1 Objective... 10-1 Processing Script Options With the getopts Statement... 10-2 Using the getopts Statement... 10-3 Handling Invalid Options... 10-4 Specifying Arguments to Options... 10-7 Example of Using the getopts Statement... 10-8 Forgetting an Argument to an Option... 10-10 Exercise: Using the getopts Statement... 10-13 Preparation... 10-13 Tasks... 10-13 Exercise Summary... 10-14 Task Solutions... 10-15 Advanced Variables, Parameters, and Argument Lists...11-1 Objectives... 11-1 Variable Types... 11-2 Assessing Variable Values... 11-2 The Korn Shell typeset Statement... 11-3 Example of Using String Manipulations... 11-3 Declaring an Integer Variable... 11-6 Creating Bourne Shell Constants... 11-7 Creating Korn Shell Constants... 11-8 Removing Portions of a String... 11-9 Examples of Removing Portions of a String... 11-9 Korn Shell Arrays... 11-11 Examples of Using Arrays... 11-12 Using the shift Statement With Positional Parameters... 11-14 The Values of the $@" and "$*" Positional Parameters... 11-17 Exercise: Using Advanced Variables, Parameters, and Argument Lists... 11-25 Preparation... 11-22 Tasks... 11-22 Exercise Summary... 11-24 Exercise Solutions... 11-25 Functions...12-1 Objectives... 12-1 Functions in the Shell... 12-2 Syntax... 12-2 Function Execution... 12-2 Positional Parameters and Functions... 12-3 Return Values... 12-5 The typeset and unset Statements... 12-6 Function Files... 12-7 Autoloading Korn Shell Functions With the FPATH Variable... 12-8 Exercise: Using Functions... 12-10 Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Learning Services, Revision B.1 11
Preparation... 12-10 Tasks... 12-10 Exercise Summary... 12-12 Task Solutions... 12-13 Traps... 13-1 Objectives... 13-1 Shell Signal Values... 13-2 Catching Signals With the trap Statement... 13-5 Example of Using the trap Statement... 13-6 Catching User Errors With the trap Statement... 13-9 Example of Using the trap Statement With the ERR Signal... 13-11 When to Declare a trap Statement... 13-13 Exercise: Using Traps... 13-15 Preparation... 13-15 Tasks... 13-15 Exercise Summary... 13-16 Task Solutions... 13-17 Advanced nawk Programming...A-1 Programming Concepts... A-2 The if Statement... A-3 Conditional Printing With the nawk Language... A-4 String Comparisons and Relational and Logical Operators... A-5 Logical Operators... A-6 The while Loop in the nawk Language... A-7 The do while Loop... A-7 The for Loop in the nawk Language... A-9 Using Loops With Arrays... A-10 Nonnumeric Array Indices... A-11 The break and continue Statements... A-13 The continue Statement... A-13 The next and exit Statements... A-15 User-Defined Functions... A-16 Command-Line Arguments... A-18 Using Built-in Variables... A-19 Built-in Arithmetic Functions... A-20 Built-in String Functions... A-21 Built-in I/O Processing Functions... A-23 The printf() Statement... A-24 Built-in Operators... A-25 Additional grep Functionality...B-1 Regular Expression Metacharacters...B-2 Character Classes...B-3 Tagged Regular Expressions...B-5 12 Shell Programming for System Administrators Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision B.1
Additional sed Functionality... C-1 Editing Commands...C-2 Placing Multiple Edits in a Single sed Command...C-2 Using sed to Append, Insert, or Change Text...C-3 Shell Metacharacters for Pattern Matching for File Names... D-1 Metacharacter Examples... D-3 UNIX Commands and Utilities... E-1 Status Commands...E-2 The date Command...E-2 The ps Command...E-2 The who Command...E-4 The rusers Command...E-4 The finger Command...E-5 The uptime Command...E-6 The rup Command...E-6 The w Command...E-6 File Access Commands...E-8 The find Command...E-8 The sort Command...E-9 Reading Part of a File...E-12 The tr Command...E-12 The cut Command...E-13 The paste Command...E-15 The cmp, diff, and sdiff Commands...E-16 Combining Commands...E-17 Pipes...E-17 Redirection...E-17 Shell Comparison... F-1 Shell Comparison... F-2 Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. Sun Learning Services, Revision B.1 13