Introduction
Provides a guided tour of (Bourne) shell scripting, including control structures, numerical computation, regular expressions, subroutines, and error handling.
Organization of This Document
This document is organized as a series of topics. These topics can be read linearly as a tutorial, but are also organized with the intent to be a quick reference on key subjects.
Before You Begin —explains how to get a command prompt in OS X and other operating systems, provides pointers to documentation about using the command line interactively, and provides useful command-line tips (such as how to enter control characters).
Shell Script Basics —introduces basic concepts of shell scripting, including variables, control statements, file I/O, pipes, redirection, and argument handling.
Subroutines, Scoping, and Sourcing —describes how to obtain result codes from outside executables, how to write and call subroutines, subroutine variable scoping rules, how to include one shell script inside another (sourcing), and how to use job control to run tasks in the background.
Paint by Numbers —explains how to use integer math in shell scripts. This section also explains how to use the <!--a target="_self" -->bc<!--/a--> command-line utility or Perl to handle more complex math, such as floating-point calculations.
Regular Expressions Unfettered —describes basic and extended regular expressions and how to use them. This section also describes the differences between these regular expression dialects and the dialect supported by Perl, and shows how to use Perl regular expressions through inline scripting.
How AWK-ward —explains the AWK command, which provides a data-driven programming language based on regular expressions and tabular data.
Designing Scripts for Cross-Platform Deployment —describes key differences in the shell scripting environments provided by various operating systems and provides tips for writing portable scripts.
Advanced Techniques —shows you how to simulate data structures and pointers, perform nonblocking I/O, write timing loops, trap signals, use special built-in shell variables, draw styled text using ANSI color and formatting commands, find the absolute path of a script, use <!--a target="_self" -->osascript<!--/a--> to manipulate graphical applications, and use file descriptors and named pipes to treat command-line tools as filters.
Performance Tuning —describes techniques for improving the performance of complex scripts.
Other Tools and Information —provides a basic summary of various commands that may be useful to shell script developers, including links to OS X documentation for each of them.
Starting Points —provides several sample shell scripts and snippets that automate real-world tasks. This appendix also provides links to other complete examples elsewhere in the book.
An Extreme Example: The Monte Carlo (Bourne) Method for Pi —provides a complex example to showcase the power of shell scripts to perform complex tasks (slowly). The code example shows a shell script implementation of the Monte Carlo method for approximating the value of Pi. The code example takes advantage of a number of numerical and string handling techniques described in the previous chapters. By showing some of the same calculations written in multiple ways, it also illustrates why it is often beneficial, performance-wise, to embed scripts written in other languages such as Perl or AWK when attempting tasks that suit those languages better.
Happy scripting!
Next
Copyright © 2003, 2014 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2014-03-10