Basic Shell Scripting

Topics: Unix shell, Shell script, Shell Pages: 151 (33932 words) Published: July 19, 2013
Shell Scripting Primer

Contents

Introduction 12
Organization of This Document 12

Before You Begin 14
Obtaining a Shell Prompt 14 In Mac OS X 14 In Other UNIX Variants or Linux Variants 15 In Windows 15 Familiarize Yourself With the Command Line 15 Tips for Shell Users 15 The alias Builtin 15 Entering Special Characters 17 Creating Text Files in Your Home Directory 17 In TextEdit 17 In pico or nano 18

Shell Script Basics 19
Shell Script Dialects 19 She Sells C Shells 21 Shell Variables and Printing 21 Using Arguments And Variables That Contain Spaces 23 Handling Quotation Marks in Strings 25 Exporting Shell Variables 26 Using the export Builtin (Bourne Shell) 27 Overriding Environment Variables for Child Processes (Bourne Shell) 28 Using the setenv Builtin (C shell) 29 Overriding Environment Variables for Child Processes (C Shell) 31 Deleting Shell Variables 32

Shell Input and Output 33
Shell Script Input and Output Using printf and read 33 Bulk I/O Using the cat Command 35 Pipes and Redirection 38 Basic File Redirection 38

2011-07-27 | © 2003, 2011 Apple Inc. All Rights Reserved.

2

Contents

Pipes and File Descriptor Redirection (Bourne Shell) 40 Pipes and File Descriptor Redirection (C Shell) 42

Flow Control, Expansion, and Parsing 44
Basic Control Statements 44 The if Statement 44 The test Command and Bracket Notation 46 The while Statement 48 The for Statement 50 The case statement 53 The expr Command 56 Parsing, Variable Expansion, and Quoting 59 Variable Expansion and Field Separators 60 Special Characters Explained 61 Quoting Special Characters 64 Inline Execution 66

Result Codes, Chaining, and Flags 68
Working with Result Codes 68 Chaining Execution 69 Handling Flags and Arguments 72 Special Multi-argument Variables 72 The shift Builtin 74 The getopts builtin and the getopt command 75

Subroutines, Scoping, and Sourcing 81
Subroutine Basics 81 Anonymous Subroutines 82 Variable Scoping 84 Declaring a Local Variable 84 Using Global Variables in Subroutines 85 Including One Shell Script Inside Another (Sourcing) 87 Finding the Absolute Path of the Current Script 89

Paint by Numbers 91
The expr Command Also Does Math 91 The Easy Way: Parentheses 92 Common Mistakes 93 Beyond Basic Math 96 Floating Point Math Using Inline Perl 96

2011-07-27 | © 2003, 2011 Apple Inc. All Rights Reserved.

3

Contents

Floating Point Math Using the bc Command 97

Regular Expressions Unfettered 98
Where Can I Use Regular Expressions? 99 Types of Regular Expressions 100 Regular Expression Syntax 100 Positional Anchors and Flags 101 Wildcards and Repetition Operators 102 Character Classes and Groups 104 Predefined Character Classes 105 Custom Character Classes 106 Grouping Operators 106 Using Empty Subexpressions 108 Quoting Special Characters 109 Capturing Operators and Variables 110 Mixing Capturing and Grouping Operators 112 Using Modifiers 113 Perl and Python Extensions 114 Character Class Shortcuts 115 Nongreedy Wildcard Matching 116 Noncapturing Parentheses 117 For More Information 117 Using Regular Expressions in Control Statements 118

How AWK-ward 120
What Is AWK? 120 A Simple AWK Script 121 Conditional Filter Rules in AWK 122 Regular Expressions in AWK 123 Expression Ranges in awk 124 Relational Expressions in AWK 124 Special Patterns in AWK: BEGIN and END 125 Conditional Pattern Matching with Variables 126 Changing the Record and Field Separators in AWK Scripts 127 Control Statements in AWK 128 The if Statement 128 The while Statement 129 The for Statement 129 Skipping Records and Files 130 Functions in AWK 131

2011-07-27 | © 2003, 2011 Apple Inc. All Rights Reserved.

4

Contents

Working with Arrays in AWK 131 Array Basics 132 Creating Arrays with split 134 Copying and Joining an Array 135 Deleting Array Elements 137 File Input and Output 138 Integrating AWK Scripts with Shell Scripts 140 Accepting Arguments from Shell Scripts 140 Reading Environment Variables...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Shell Scripting Essay
  • Scripting Essay
  • Shell Scripting in Linux Essay
  • Unit 2 Shell Scripting Research Essay
  • shell Essay
  • Essay about Basic
  • Scripting Essay
  • SHELL Essay

Become a StudyMode Member

Sign Up - It's Free