2nd Edition. — Indianapolis: Wiley Publishing, 2008. — 1035 p. ISBN: 978-0-470-18301-4.
Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and Unix Gurus
In Unix there are many ways to accomplish the same task. Given a problem to solve, we may be able to get to a solution in any number of ways. Of course, some techniques will be more efficient, use fewer system resources, and may or may not give the user feedback on what is going on or give more accurate details and more precision to the result. In this book we are going to step through every detail of creating shell scripts to solve real-world Unix problems and tasks. The shell scripts range from using a pseudo-random number generator to creating passwords using arrays to replicating data with rsync to working with record files. The scope of solutions is broad and detailed. The details required to write a good shell script include commenting each step for future reference. Other details include combining many commands together into a single command statement when desirable, separating commands on several lines of code when readability and understanding the concept may be diminished, and making a script readable and easy to maintain through the life cycle. We will see the benefits of variables and files to store data, show methods to strip out unneeded data from command output, and format data for a particular purpose. Additionally, we are going to show how to write and use functions in our shell scripts and demonstrate the benefits of functions over a shell script written without functions.
The Basics of Shell Scripting
Scripting Quick Start and Review
Case Sensitivity
Unix Special Characters
Shells
Shell Scripts
Functions
Running a Shell Script
Declare the Shell in the Shell Script
Comments and Style in Shell Scripts
Control Structures
if...then statement
if...then...else statement
if...then...elif...(else) statement
for...in statement
while statement
until statement
case statement
Using break, continue, exit, and return
Here Document
Shell Script Commands
Symbol Commands
Variables
Command-Line Arguments
shift Command
Special Parameters $* and $@
Special Parameter Definitions
Double Quotes, Forward Tics, and Back Tics
www.it-ebooks.infoxii
Using awk on Solaris
Using the echo Command Correctly
Math in a Shell Script
Operators
Built-In Mathematical Functions
File Permissions, suid and sgid Programs
chmod Command Syntax for Each Purpose
To Make a Script Executable
To Set a Program to Always Execute as the Owner
To Set a Program to Always Execute as a Member of the
File Owner’s Group
To Set a Program to Always Execute as Both the File
Owner and the File Owner’s Group
Running Commands on a Remote Host
Setting Traps
User-Information Commands
who Command
w Command
last Command
ps Command
Communicating with Users
Uppercase or Lowercase Text for Easy Testing
Check the Return Code
Time-Based Script Execution
Cron Tables
Cron Table Entry Syntax
at Command
Output Control
Silent Running
Using getopts to Parse Command-Line Arguments
Making a Co-Process with Background Function
Catching a Delayed Command Output
Fastest Ways to Process a File Line-by-Line
Using Command Output in a Loop
Mail Notification Techniques
Using the mail and mailx Commands
Using the sendmail Command to Send Outbound Mail
Creating a Progress Indicator
A Series of Dots
A Rotating Line
Elapsed Time
Working with Record Files
Working with Strings
Creating a Pseudo-Random Number
Using /dev/random and /dev/urandom
Checking for Stale Disk Partitions in AIX
Automated Host Pinging
Highlighting Specific Text in a File
Keeping the Printers Printing
AIX ‘‘Classic’’ Printer Subsystem
System V and CUPS Printing
Automated FTP File Transfer
Using rsync to Replicate Data
Simple Generic rsync Shell Script
Capturing a List of Files Larger than $MEG
Capturing a User’s Keystrokes
Using the bc Utility for Floating-Point Math
Number Base Conversions
Using the typeset Command
Using the printf Command
Create a Menu with the select Command
Removing Repeated Lines in a File
Removing Blank Lines from a File
Testing for a Null Variable
Directly Access the Value of the Last Positional Parameter, $#
Remove the Column Headings in a Command Output
Arrays
Loading an Array
Testing a String