|
Linux & UNIX Shell Programming
This comprehensive book is a practical, easy-to-use guide to programming and using the Bourne shell for beginners and experienced users - the Bourne shell is the standard shell for UNIX, and is also fully backward compatible to the Linux BASH shell. This book will fully illustrate the ability of the shell to unlock the real potential of UNIX and Linux,
and aims to get the reader up, running and creating robust shell scripts for real tasks and situations as quickly as possible shell scripts that will work on any mainstream UNIX or Linux machine.
If you are new to UNIX and Linux or if you are a power user in waiting then this book is for you. Most shell programming books merely annotate manual pages and syntax , but in this book users of all abilities will find plenty of practical working examples - all of which are available as full code script via an ftp site as well as a host of tips, tricks and code one-liners, that will save you time on a day-to-day basis. The book is organized into self-contained chapters on individual topics for ease of reference.
CONTENTS:
Acknowledgements.
Introduction.
The Bourne Shell.
Shell Portability.
Organization of the Book.
The Shell.
Text File Filtering.
Login Environment.
Basic Shell Programming.
Better Scripting Skills.
Assumptions.
Conventions Used in This Book.
I. THE SHELL.
1. File Security and Permissions.
Files.
Types of Files.
Permissions.
Changing Permission Bits.
Symbolic Mode.
CHMOD Examples.
Absolute Mode.
More CHMOD Examples.
Absolute or Symbolic, Your Choice.
Directories.
Set-UID.
Why Use Set-UID?
Adding Set-UID Permission Examples.
CHOWN and CHGRP.
CHOWN Examples.
CHGRP Examples.
Finding Out What Groups You Belong to.
Finding Out What Groups Other Users Belong to.
UMASK.
How to Work Out UMASK Values.
Common UMASK Values.
Symbolic (Soft) Links.
Using Soft Links Saves Multiple Copies of Files.
Example of Using a Soft Link.
Conclusion.
2. Using Find and XARGS.
Find Options.
Find Files by Name.
Find Files by Perm Mode.
Ignoring Directories.
Find Files by User and NoUser.
Find Files by Group and NoGroup.
Find Files by Modification Times.
Finding Files That Are Newer or Older in Days or Minutes.
Find Files by Type.
Find Files by Size.
Find Files by Depth.
Find Files by Mount.
Find Files Using CPIO.
Using Exec or OK to Run Shell Commands.
Find Examples.
XARGS.
Conclusion.
3. Running Commands Unattended.
CRON and CRONTAB.
CRONTAB Fields.
CRONTAB Entry Examples.
CRONTAB Options.
Creating a New CRONTAB Entry.
Listing Your CRONTAB.
Editing Your CRONTAB.
Removing Your CRONTAB.
Restoring a Lost CRONTAB.
The at Command.
Submitting Commands and Scripts Using at.
Listing Jobs Submitted 39
.
Deleting a Submitted Job.
The & Command.
Submitting a Command to the Background.
Checking the Process Using Ps.
Killing a Background Job.
The Nohup Command.
Submitting a Job Using Nohup.
Submitting Several Jobs at Once.
Conclusion.
4. Filename Substitution.
Using The.
Using the ?
Using and .
Conclusion.
5. Shell Input and Output.
Echo.
Read.
Cat.
Pipes.
Tee.
Standard Input, Output, and Errors.
Standard Input.
Standard Output.
Standard Error.
File Redirection.
Redirecting Standard Output.
Redirecting Standard Input.
Redirecting Standard Error.
Combining Standard Output and Error.
Merging Standard Output and Standard Error.
Exec.
Using File Descriptors.
Conclusion.
6. Command Execution Order.
Using &.
Using .
Grouping Commands Using () and {}.
Conclusion.
II. TEXT FILTERING.
7. Introducing Regular Expressions.
Matching a Single Character with a Period.
Matching a String or Character at the Beginning of a Line with a'^.
Matching a String or Character at the End of a Line with $.
Matching a Single or String of Characters With.
Escaping the Meaning of a Special Character Using \.
Matching Ranges or Sets Using .
Matching a Specific Number of Occurrenceswith \{ \}.
Conclusion.
8. The GREP Family.
GREP.
Please Quote Me.
GREP Options.
Searching More Than One File.
Line Matches.
Line Numbers.
Do Not Match.
Getting an Exact Match.
Being Case-Sensitive.
GREP and Regular Expressions.
Pattern Ranges.
Don't Match at the Beginning of a Line.
Trapping Upper and Lower Cases.
Matching Any Characters.
Date Searching.
Combining Ranges.
Occurrences in a Pattern.
Using GREP to Match Either or All Patterns.
Blank Lines.
Matching Special Characters.
Searching for Formatted Filenames.
Searching for IP Addresses.
Class Names.
Pattern Matching with Wildcards.
GREP on the System.
Directories.
Passwd File.
Using the Ps Command.
Using GREP on a String.
EGREP.
Conclusion.
9. Introducing AWK.
Calling AWK.
AWK Script.
Patterns and Actions.
Fields and Records.
Regular Expressions and Operators in AWK.
Metacharacters.
Conditional Operators.
AWK Built-In Variables.
NF, NR and FILENAME.
AWK Operators.
Built-In String Functions.
String Escape Sequences.
AWK Output Functions (Printf).
Printf Modifiers.
AWK Arrays.
Conclusion.
10. Using SED.
How SED Reads Data.
Calling SED.
Saving SED Output.
Ways to Find Text in a File Using SED.
Basic SED Editing Commands.
SED and Regular Expressions.
Basic SED Editing Examples.
Displaying Lines Using P(Rint).
Range Printing.
Pattern Printing.
Searching Using a Pattern and Line Number.
Matching a Metacharacter.
Displaying the Whole File.
Any Characters.
First Line.
Last Line.
Printing Line Numbers.
Appending Text.
Creating a SED Script File.
Inserting Text.
Changing Text.
Deleting Text.
Substituting Text.
Modifying Strings with Substitute (&).
Writing to a File Within SED.
Reading Text from a File.
Quitting After a Match.
Displaying Control Characters in a File.
Using SED on the System.
Dealing with Control Characters.
Dealing with Report Output.
Getting Rid of Numbers at the Beginning of a Line.
Appending Text.
Passing Values from the Shell to SED.
Assigning a Shell Variable from SED Output.
Quick One-Liners.
Conclusion.
11. Merge and Divide.
Using Sort.
A Word to the Wise.
Sort Options.
Saving Output.
How Sort Starts.
How Sort References Fields.
Is the File Already Sorted?
Basic Sort.
Reverse Sort.
Sorting on a Specific Field.
Numeric Field Sorting.
Unique Sorting.
Other Sort Methods Using -K.
Order of Sort Keys Using -K.
Specifying Sort Sequences.
Using Pos.
Sort Output Using Head and Tail.
Using Sort Output with AWK.
Merging Two Sorted Files.
Sort on the System.
Using UNIQ.
Occurrences.
Using Join.
Joining Two Files.
Using Cut.
Using the Field Separator.
Cutting Specific Fields.
Using Paste.
Specifying Columns.
Using a Different Field Separator.
Piping Into a Paste Command.
Using Split.
Conclusion.
12. Using TR.
About TR.
Character Ranges.
Saving Output.
Getting Rid of Repeated Characters.
Deleting Blank Lines.
Upper to Lower Case.
Lower to Upper Case.
Deleting Certain Characters.
Translating Control Characters.
Quick Conversions.
Matching More Than One Character.
Conclusion.
III. THE LOGIN ENVIRONMENT.
13. The Login Environment.
The /Etc/Profile.
Your $HOME .Profile.
Using STTY.
Creating a .Logout File.
Conclusion.
14. Environment and Shell Variables.
What Is a Shell Variable?
Local Variables.
Displaying a Variable.
Clearing a Variable.
Displaying All Local Shell Variables.
Variable Values Together.
Testing if a Variable Is Set (Substitution).
Using Variables to Hold Arguments for System Commands.
Making a Variable Read-Only.
Environment Variables.
to Assign an Environment Variable.
Displaying Environment Variables.
Clearing Environment Variables.
Built-In Shell Variables.
Other Environment Variables.
Using the Set Command.
Exporting Variables to Child Processes.
Positional Variable Parameters.
Using Positional Parameters in Scripts.
Passing Parameters to System Commands.
Special Variable Parameters.
Last Exit Status.
Conclusion.
15. Quoting.
The Need for Quoting.
Double Quotes.
Single Quotes.
Back Quote.
Backslash.
Conclusion.
IV. BASIC SHELL PROGRAMMING.
16. Introduction to Shell Scripts.
Reasons for Using a Shell Script.
Try Out New Ideas.
What a Script Contains.
Running a Script.
Conclusion.
17. Conditional Testing.
Testing for a File Status.
Using Logical Operators with Tests.
Testing Strings.
Testing Numbers.
Using EXPR.
Incrementing Counters.
Testing for a Number.
Pattern Matching.
Conclusion.
18. Control Flow Structures.
Exit Statuses.
Control Structures.
Flow Control.
Iteration.
if Then Else Statements.
Simple if Statements.
Testing Values of Variables.
Checking the Output of GREP.
Testing the Output of GREP on a Variable.
Checking the Outcome of a File Copy.
Testing for Current Directory.
Testing for File Permissions.
Testing for Parameters Passed to a Script.
Determining if a Script Is in Interactive Mode.
Simple if Else Statements.
Testing for Set Variables.
Checking the User Who Is Running a Script.
Using Script Parameters to Pass Down to a System Command.
Using the Null: Command.
Testing the Outcome of a Directory Creation.
Another Copy.
More Than One if Statement.
Testing and Setting Environment Variables.
Checking the Last Command Status.
Adding and Checking Integer Values.
Simple Security Login Script.
Using ELIF.
Multiple Checks Using ELIF.
Checking Multiple File Locations.
Case Statement.
Simple Case Statement.
Using with Pattern Matching.
Prompting for a Y or N.
Case and Passing Command Parameters.
Trapping Input with No Pattern Commands.
Default Variable Values.
For Loop.
Simple for Loop.
Printing Out a String List.
Using Ls with the for Loop.
Using Parameters with a for Loop.
Pinging Servers with a for Loop.
Backing Up Files Using a for Loop.
Multiple Translation.
Multiple SED Deletes.
Counting Iterations.
For Loops and Here Documents.
Nested for Loops.
Until Loop.
Simple Until Loop.
Monitoring for a File Presence.
Monitoring Disk Space.
While Loop.
Simple While Loop.
Using the While Loop to Read from the Keyboard.
Using the While Loop to Read Data from Files.
Reading Files Using IFS.
Processing a File with Conditional Tests.
Running Totals.
Reading a Pair of Records at a Time.
Ignoring the # Character.
Processing Formatted Reports.
The While Loop and File Descriptors.
Controlling Loops Using Break and Continue.
Break.
Breaking Out of a Case Statement.
Continue.
Skipping Lines of Files.
Menus.
Conclusion.
19. Shell Functions.
Declaring Functions in a Script.
Using Functions in a Script.
Passing Parameters to a Function.
Returning from a Called Function.
Testing the Values Returned by a Function.
Using Functions in the Shell.
Creating a Function File.
Sourcing the File.
Checking the Loaded Functions.
Executing Shell Functions.
Deleting Shell Functions.
Editing Shell Functions.
Function Examples.
Putting It All Together.
Calling Functions.
Calling Functions Inside a Script.
Calling Functions from a Function File.
Sourcing Files Is Not Only for Functions.
Conclusion.
20. Passing Parameters to Scripts.
The Shift Command.
Simple Use of Shift.
Last Parameter Supplied on the Command Line.
Using Shift to Process File Conversions.
Getopts.
An Example Getopts Script.
How Getopts Works.
Specifying Values Using Getopts.
How Values Can Be Accessed.
Using Getopts to Process File Conversions.
Conclusion.
21. Creating Screen Output.
TPUT.
String Output.
Numeric Output.
Boolean Output.
Using TPUT.
Assigning TPUT Commands.
Using Boolean Output.
Using TPUT in Your Scripts.
Generating Escape Sequences.
Cursor Position.
Centering Text on the Screen.
Finding Out Your Terminal Attributes.
Using Function Keys with Your Scripts.
Using Colors.
Generating Colors.
Creating Better Menus.
Conclusion.
22. Creating Screen Input.
Adding Records.
Deleting Records.
Amending Records.
Viewing Records.
Conclusion.
23. Debugging Scripts.
Common Errors.
The Classic Missing Quote.
The Test Error.
Character Case.
For Loops.
Echo.
The Set Command.
Conclusion.
24. Shell Built-In Commands.
Complete List of Shell Built-In Commands.
PWD.
Set.
Times.
Type.
Ulimit.
Wait.
Conclusion.
V. BETTER SCRIPTING SKILLS.
25. Going Further with Here Documents.
Creating a Quick File.
Creating a Quick Print Document.
Automating Menus.
Automating FTP Transfers.
Accessing Databases.
Conclusion.
26. Shell Utilities.
Creating Hold Files.
Using Date to Create Log Files.
Creating Unique Temporary Files.
Signals.
Killing a Process.
Detecting a Signal.
Trap.
Trapping Signals and Taking Action.
Catching a Signal and Taking Action.
Locking Up Your Terminal.
Ignoring Signals.
Eval.
Executing Commands Held in a String.
Making a Value a Variable Name.
Logger Command.
Using the Logger Command.
Using Logger in Your Scripts.
Conclusion.
27. A Small Collection of Scripts.
Pingall.
Backup_Gen.
Del.Lines.
Access.Deny.
Logroll.
Nfsdown.
Conclusion.
28. Run Level Scripts.
How Can You Tell if You Have Run Level Directories?
Finding Out Your Current Run Level.
Bringing You Up to Speed on Inittab.
Now for the Run Levels.
The Different Run Levels.
Format of a Run Level Script.
Installing a Run Level Script.
Using Inittab to Launch Applications.
Other Methods of Starting and Shutting Down Services.
Conclusion.
29. CGI Scripts.
What Is a Web Page?
CGI.
Connecting to a Web Server.
CGI and HTM Script.
Basic CGI Script.
Displaying the Output of a Shell Command.
Using SSI.
Access Counter.
Printing Out Current Web Environment Settings Using a Link.
Other Common Environment Variables.
Introducing the Get and Post Methods.
Get Method.
Post Method.
Populating a List.
Automatically Refreshing a Web Page.
Conclusion.
A ASCII Chart.
Useful Shell Commands.
Index. 0201674726T04062001
|