Book description
Shell Scripting Recipes is filled with over 150 much-needed and practical recipes that follow a problem-solution format, and help all Unix users regain some of the lost time spent creating and testing shell scripts. Most scripts in this book are POSIX (Portable Operating System Interface)-compliant, so they are supported by many of the major shell variants, including Bash, ksh, and sh, among others. This completely updated second edition takes into account the many changes to shell scripting that have occurred since its original publication nearly ten years ago, and all the recipes are now relevant for a modern audience.
Each real-world example recipe follows the same structure and easily shows you what's going on in each case. File conversion (DOS, UNIX, and Mac), system administration, and resource monitoring are just a few of the topics covered in this highly practical shell scripting reference. You will also find recipes for filename management, complex date calculations, screen control capabilities, and more.
Author Chris Johnson's code is clear, direct, and applicable, and he explains everything well in each recipe so that you understand not just how to do something, but why to do it that way. Shell Scripting Recipes is an invaluable book and one to add to your library today.
Table of contents
- Cover
- Title
- Copyright
- Dedication
- Contents at a Glance
- Contents
- About the Authors
- Acknowledgments
-
Chapter 1 : The POSIX Shell and Command-Line Utilities
- Shell Commands
- Parameters and Variables
- Patterns
- Parameter Expansion
- Shell Arithmetic
- Aliases
- Sourcing a File
- Functions
-
standard-funcs: A Collection of Useful Commands
- 1.1 get_key—Get a Single Keystroke from the User
- 1.2 getline—Prompt User to Enter a Line
- 1.3 press_any_key—Prompt for a Single Keypress
- 1.4 menu1—Print a Menu and Execute a Selected Command
- 1.5 arg—Prompt for Required Argument If None Supplied
- 1.6 die—Print Error Message and Exit with Error Status
- 1.7 show_date—Display Date in D[D] MMM YYYY Format
- 1.8 date_vars—Set Date and Time Variables
- 1.9 is_num—Is This a Positive Integer?
- 1.10 abbrev_num—Abbreviate Large Numbers
- 1.11 commas—Add Thousands Separators to a Number
- 1.12 pr1—Print Arguments, One to a Line
- 1.13 checkdirs—Check for Directories; Create If Necessary
- 1.14 checkfiles—Check That a Directory Contains Certain Files
- 1.15 zpad—Pad a Number with Leading Zeroes
- 1.16 cleanup—Remove Temporary Files and Reset Terminal on Exit
-
The Unix Utilities
- cat: Concatenate Files to the Standard Output
- sed: A Text Stream Editor
- awk: Pattern Scanning and Processing Language
- grep: Print Lines Matching a Regular Expression
- date: Show or Set the System Date
- tr: A Character Translation Utility
- wc: Count Characters,Words, and Lines in a File
- file: Determine the File Type
- ls: Sort and Provide Details About Files
- uniq: Remove Consecutive Duplicate Lines
- sudo: Execute Commands as the Superuser
- split: Divide a File into Equal-Sized Pieces
- which: Show the Full Path to a Command
- gs, gv: Render, Convert, or View PostScript and PDF Files
- Summary
- Chapter 2 : Playing with Files: Viewing, Manipulating, and Editing Text Files
- Chapter 3 : String Briefs
-
Chapter 4 : What’s in a Word?
- Finding and Massaging Word Lists
-
wf-funcs: WordFinder Function Library
- 4.1 write_config—Write User’s Information to the Configuration File
- 4.2 do_config—Check For and Source Default Configuration File
- 4.3 set_sysdict—Select the Dictionary Directory
- 4.4 mkwsig—Sort Letters in a Word
- 4.5 wf-clean—Remove Carriage Returns and Accents
- 4.6 wf-compounds—Squish Compound Words and Save with Lengths
- 4.7 wf-setup—Prepare Word and Anagram Lists
-
Playing with Matches
- 4.8 wf—Find Words That Match a Pattern
- 4.9 wfb—Find Words That Begin with a Given Pattern
- 4.10 wfe—Find Words That End with a Given Pattern
- 4.11 wfc—Find Words That Contain a Given Pattern
- 4.12 wfit—Find Words That Fit Together in a Grid
- 4.13 anagram—Find Words That Are Anagrams of a Given Word
- 4.14 aplus—Find Anagrams of a Word with a Letter Added
- 4.15 aminus—Remove Each Letter in Turn and Anagram What’s Left
- Summary
-
Chapter 5 : Scripting by Numbers
-
The math-funcs Library
- 5.1 fpmul—Multiply Decimal Fractions
- 5.2 int—Return the Integer Portion of a Decimal Fraction
- 5.3 round—Round the Argument to the Nearest Integer
- 5.4 pow—Raise a Number to Any Given Power
- 5.5 square—Raise a Number to the Second Power
- 5.6 cube—Raise a Number to the Third Power
- 5.7 calc—A Simple Command-Line Calculator
- Adding and Averaging
- Converting Between Unit Systems
- Summary
-
The math-funcs Library
-
Chapter 6 : Loose Names Sink Scripts: Bringing Sanity to Filenames
- What’s in a Name?
-
Functioning Filenames: The filename-funcs Library
- 6.1 basename—Extract the Last Element of a Pathname
- 6.2 dirname—Return All but the Last Element of a Pathname
- 6.3 is_pfname—Check for POSIX Portable Filename
- 6.4 is_OKfname—Check Whether a Filename Is Acceptable
- 6.5 pfname—Convert Nonportable Characters in Filename
- 6.6 OKfname—Make a Filename Acceptable
- 6.7 is_whitespc—Does the Filename Contain Whitespace Characters?
- 6.8 whitespc—Fix Filenames Containing Whitespace Characters
- 6.9 is_dir—Is This a Directory I See Before Me?
- 6.10 nodoublechar—Remove Duplicate Characters from a String
- 6.11 new_filename—Change Filename to Desired Character Set
- 6.12 fix_pwd—Fix All the Filenames in the Current Directory
- 6.13 fixfname—Convert Filenames to Sensible Names
- Summary
- Chapter 7 : Treading a Righteous PATH
-
Chapter 8 : The Dating Game
-
The date-funcs Library
- 8.1 split_date—Divide a Date into Day, Month, and Year
- 8.2 is_leap_year—Is There an Extra Day This Year?
- 8.3 days_in_month—How Many Days Hath September?
- 8.4 Julian Dates
- 8.5 dateshift—Add or Subtract a Number of Days
- 8.6 diffdate—Find the Number of Days Between Two Dates
- 8.7 day_of_week—Find the Day of the Week for Any Date
- 8.8 display_date—Show a Date in Text Format
- 8.9 parse_date—Decipher Various Forms of Date String
- 8.10 valid_date—Where Was I on November 31st?
- Summary
-
The date-funcs Library
- Chapter 9 : Good Housekeeping: Monitoring and Tidying Up File Systems
-
Chapter 10 : Screenplay: The screen–funcs Library
- 10.1 screen-vars—Variables for Screen Manipulation
- 10.2 set_attr—Set Screen-Printing Attributes
- 10.3 set_fg, set_bg, set_fgbg—Set Colors for Printing to the Screen
- 10.4 cls—Clear the Screen
- 10.5 printat—Position Cursor by Row and Column
- 10.6 put_block_at—Print Lines in a Column Anywhere on the Screen
- 10.7 get_size—Set LINES and COLUMNS Variables
- 10.8 max_length—Find the Length of the Longest Argument
- 10.9 print_block_at—Print a Block of Lines Anywhere on the Screen
- 10.10 vbar, hbar—Print a Vertical or Horizontal Bar
- 10.11 center—Center a String on N Columns
- 10.12 flush_right—Align String with the Right Margin
- 10.13 ruler—Draw a Ruler Across the Width and Height of the Window
- 10.14 box_block, box_block_at—Print Text Surrounded by a Box
- 10.15 clear_area, clear_area_at—Clear an Area of the Screen
- 10.16 box_area, box_area_at—Draw a Box Around an Area
- 10.17 screen-demo—Saving and Redisplaying Areas of the Screen
- Summary
- Chapter 11 : Aging, Archiving, and Deleting Files
-
Chapter 12 : Covering All Your Databases
- 12.1 lookup—Find the Corresponding Value for a Key
- shdb-funcs: Shell Database Function Library
- 12.2 load_db—Import Database into Shell Array
- 12.3 split_record—Split a Record into Fields
- 12.4 csv_split—Extract Fields from CSV Records
- 12.5 put_record—Assemble a Database Record from an Array
- 12.6 put_csv—Assemble Fields into a CSV Record
- 12.7 db-demo—View and Edit a Password File
- PhoneBase: A Simple Phone Number Database
- 12.8 ph—Look Up a Phone Number
- 12.9 phadd—Add an Entry to PhoneBase
- 12.10 phdel—Delete an Entry from PhoneBase
- 12.11 phx—Show ph Search Results in an X Window
- Summary
- Chapter 13 : Home on the Web
- Chapter 14 : Taking Care of Business
-
Chapter 15 : Random Acts of Scripting
- The rand-funcs Library
- 15.1 random—Return One or More Random Integers in a Given Range
- 15.2 toss—Simulate Tossing a Coin
- 15.3 randstr—Select a String at Random
- A Random Sampling of Scripts
- 15.4 rand-date—Generate Random Dates in ISO Format
- 15.5 randsort—Print Lines in Random Order
- 15.6 randomword—Generate Random Words According to Format Specifications
- 15.7 dice—Roll a Set of Dice
- 15.8 throw—Throw a Pair of Dice
- Summary
-
Chapter 16 : A Smorgasbord of Scripts
- 16.1 topntail—Remove Top and Bottom Lines from a File
- 16.2 flocate—Locate Files by Filename Alone
- 16.3 sus—Display a POSIX Man Page
- 16.4 cwbw—Count Words Beginning With
- 16.5 cci—Configure, Compile, and Install from Tarball
- 16.6 ipaddr—Find a Computer’s Network Address
- 16.7 ipaddr.cgi—Print the Remote Address of an HTTP Connection
- 16.8 iprev—Reverse the Order of Digits in an IP Address
- 16.9 intersperse—Insert a String Between Doubled Characters
- 16.10 ll—Use a Pager for a Directory Listing Only If Necessary
- 16.11 name-split—Divide a Person’s Full Name into First, Last, and Middle Names
- 16.12 rot13—Encode or Decode Text
- 16.13 showfstab—Show Information from /etc/fstab
- 16.14 unique—Remove All Duplicate Lines from a File
- Summary
- Chapter 17 : Script Development Management
- Appendix A: Internet Scripting Resources
- Index
Product information
- Title: Shell Scripting Recipes: A Problem-Solution Approach, Second Edition
- Author(s):
- Release date: October 2015
- Publisher(s): Apress
- ISBN: 9781484202203
You might also like
book
Shell Scripting: Expert Recipes for Linux, Bash, and More
A compendium of shell scripting recipes that can immediately be used, adjusted, and applied The shell …
book
Shell Programming in Unix, Linux and OS X, Fourth Edition
Shell Programming in Unix, Linux and OS X is a thoroughly updated revision of Kochan and …
book
Linux Phrasebook, Second Edition
Get more done faster at the Linux command line! This best-selling Linux Phrasebook has been thoroughly …
book
Wicked Cool Shell Scripts, 2nd Edition
Shell scripts are an efficient way to interact with your machine and manage your files and …