Book description
This book is geared towards any Unix user who doesn't want to spend time creating or testing shell scripts. Instead, Shell Scripting Recipes dissects and explains over 150 much-needed and practical real-world examples, and then shows the reader how and when to appropriately use them.
Because most scripts found in this book are POSIX (Portable Operating System Interface)-compliant, they are supported by many of the major shell variants, including Bash, ksh and sh, among others. File conversion, system administration, and resource monitoring are just a few of the topics covered in this highly practical shell scripting reference
Table of contents
- Copyright
- About the Author
- About the Technical Reviewer
- Acknowledgments
- Introduction
-
1. The POSIX Shell and Command-Line Utilities
- 1.1. Shell Commands
- 1.2. Parameters and Variables
- 1.3. Patterns
- 1.4. Parameter Expansion
- 1.5. Shell Arithmetic
- 1.6. Aliases
- 1.7. Sourcing a File
- 1.8. Functions
-
1.9. standard-funcs: A Collection of Useful Commands
- 1.9.1. get_key—Get a Single Keystroke from the User
- 1.9.2. getline—Prompt User to Enter a Line
- 1.9.3. press_any_key—Prompt for a Single Keypress
- 1.9.4. menu1—Print a Menu and Execute a Selected Command
- 1.9.5. arg—Prompt for Required Argument If None Supplied
- 1.9.6. die—Print Error Message and Exit with Error Status
- 1.9.7. show_date—Display Date in D[D] MMM YYYY Format
- 1.9.8. date_vars—Set Date and Time Variables
- 1.9.9. is_num—Is This a Positive Integer?
- 1.9.10. abbrev_num—Abbreviate Large Numbers
- 1.9.11. commas—Add Thousands Separators to a Number
- 1.9.12. pr1—Print Arguments, One to a Line
- 1.9.13. checkdirs—Check for Directories; Create If Necessary
- 1.9.14. checkfiles—Check That a Directory Contains Certain Files
- 1.9.15. zpad—Pad a Number with Leading Zeroes
- 1.9.16. cleanup—Remove Temporary Files and Reset Terminal on Exit
-
1.10. The Unix Utilities
- 1.10.1. cat: Concatenate Files to the Standard Output
- 1.10.2. sed: A Text Stream Editor
- 1.10.3. awk: Pattern Scanning and Processing Language
- 1.10.4. grep: Print Lines Matching a Regular Expression
- 1.10.5. date: Show or Set the System Date
- 1.10.6. tr: A Character Translation Utility
- 1.10.7. wc: Count Characters, Words, and Lines in a File
- 1.10.8. file: Determine the File Type
- 1.10.9. ls: Sort and Provide Details About Files
- 1.10.10. uniq: Remove Consecutive Duplicate Lines
- 1.10.11. sudo: Execute Commands as the Superuser
- 1.10.12. split: Divide a File into Equal-Sized Pieces
- 1.10.13. which: Show the Full Path to a Command
- 1.10.14. gs, gv: Render, Convert, or View PostScript and PDF Files
- 1.11. Summary
- 2. Playing with Files: Viewing, Manipulating, and Editing Text Files
-
3. String Briefs
- 3.1. Character Actions: The char-funcs Library
-
3.2. String Cleaning: The string-funcs Library
- 3.2.1. sub—Replace First Occurrence of a Pattern
- 3.2.2. gsub—Globally Replace a Pattern in a String
- 3.2.3. repeat—Build a String of a Specified Length
- 3.2.4. index, rindex—Find Position of One String Within Another
- 3.2.5. substr—Extract a Portion of a String
- 3.2.6. insert_str—Place One String Inside Another
- 3.3. Summary
-
4. What's in a Word?
- 4.1. Finding and Massaging Word Lists
-
4.2. wf-funcs: WordFinder Function Library
- 4.2.1. write_config—Write User's Information to the Configuration File
- 4.2.2. do_config—Check For and Source Default Configuration File
- 4.2.3. set_sysdict—Select the Dictionary Directory
- 4.2.4. mkwsig—Sort Letters in a Word
- 4.2.5. wf-clean—Remove Carriage Returns and Accents
- 4.2.6. wf-compounds—Squish Compound Words and Save with Lengths
- 4.2.7. wf-setup—Prepare Word and Anagram Lists
-
4.3. Playing with Matches
- 4.3.1. wf—Find Words That Match a Pattern
- 4.3.2. wfb—Find Words That Begin with a Given Pattern
- 4.3.3. wfe—Find Words That End with a Given Pattern
- 4.3.4. wfc—Find Words That Contain a Given Pattern
- 4.3.5. wfit—Find Words That Fit Together in a Grid
- 4.3.6. anagram—Find Words That Are Anagrams of a Given Word
- 4.3.7. aplus—Find Anagrams of a Word with a Letter Added
- 4.3.8. aminus—Remove Each Letter in Turn and Anagram What's Left
- 4.4. Summary
-
5. Scripting by Numbers
-
5.1. The math-funcs Library
- 5.1.1. fpmul—Multiply Decimal Fractions
- 5.1.2. int—Return the Integer Portion of a Decimal Fraction
- 5.1.3. round—Round the Argument to the Nearest Integer
- 5.1.4. pow—Raise a Number to Any Given Power
- 5.1.5. square—Raise a Number to the Second Power
- 5.1.6. cube—Raise a Number to the Third Power
- 5.1.7. calc—A Simple Command-Line Calculator
- 5.2. Adding and Averaging
- 5.3. Converting Between Unit Systems
- 5.4. Summary
-
5.1. The math-funcs Library
-
6. Loose Names Sink Scripts: Bringing Sanity to Filenames
- 6.1. What's in a Name?
-
6.2. Functioning Filenames: The filename-funcs Library
- 6.2.1. basename—Extract the Last Element of a Pathname
- 6.2.2. dirname—Return All but the Last Element of a Pathname
- 6.2.3. is_pfname—Check for POSIX Portable Filename
- 6.2.4. is_OKfname—Check Whether a Filename Is Acceptable
- 6.2.5. pfname—Convert Nonportable Characters in Filename
- 6.2.6. OKfname—Make a Filename Acceptable
- 6.2.7. is_whitespc—Does the Filename Contain Whitespace Characters?
- 6.2.8. whitespc—Fix Filenames Containing Whitespace Characters
- 6.2.9. is_dir—Is This a Directory I See Before Me?
- 6.2.10. nodoublechar—Remove Duplicate Characters from a String
- 6.2.11. new_filename—Change Filename to Desired Character Set
- 6.2.12. fix_pwd—Fix All the Filenames in the Current Directory
- 6.2.13. fixfname—Convert Filenames to Sensible Names
- 6.3. Summary
- 7. Treading a Righteous PATH
-
8. The Dating Game
-
8.1. The date-funcs Library
- 8.1.1. split_date—Divide a Date into Day, Month, and Year
- 8.1.2. is_leap_year—Is There an Extra Day This Year?
- 8.1.3. days_in_month—How Many Days Hath September?
- 8.1.4. date2julian—Calculate the Julian Day Number
- 8.1.5. julian2date—Convert Julian Back to Year, Month, and Day
- 8.1.6. dateshift—Add or Subtract a Number of Days
- 8.1.7. diffdate—Find the Number of Days Between Two Dates
- 8.1.8. day_of_week—Find the Day of the Week for Any Date
- 8.1.9. display_date—Show a Date in Text Format
- 8.1.10. parse_date—Decipher Various Forms of Date String
- 8.1.11. valid_date—Where Was I on November 31st?
- 8.2. Summary
-
8.1. The date-funcs Library
- 9. Good Housekeeping: Monitoring and Tidying Up File Systems
-
10. POP Goes the E-Mail
-
10.1.
- 10.1.1. popcheck—Display Number of Messages in POP3 Mailbox
- 10.1.2. pop3-funcs—Functions for Managing a POP3 Mailbox
- 10.1.3. pop3list—Examine a POP3 Mailbox
- 10.1.4. pop3filter—Delete and Retrieve Messages from a POP3 Server
- 10.1.5. pflog—Print a Summary of a Day's Activity
- 10.1.6. viewlog—Continuously View the pop3filter Log File
- 10.2. Summary
-
10.1.
- 11. PostScript: More Than an Afterthought
-
12. Screenplay: The screen—funcs Library
-
12.1.
- 12.1.1. screen-vars—Variables for Screen Manipulation
- 12.1.2. set_attr—Set Screen-Printing Attributes
- 12.1.3. set_fg, set_bg, set_fgbg-—Set Colors forPrinting to the Screen
- 12.1.4. cls—Clear the Screen
- 12.1.5. printat—Position Cursor by Row and Column
- 12.1.6. put_block_at—Print Lines in a Column Anywhere on the Screen
- 12.1.7. get_size—Set LINES and COLUMNS Variables
- 12.1.8. max_length—Find the Length of the Longest Argument
- 12.1.9. print_block_atxy—Print a Block of Lines Anywhere on the Screen
- 12.1.10. vbar, hbar—Print a Vertical or Horizontal Bar
- 12.1.11. center—Center a String on N Columns
- 12.1.12. flush_right—Align String with the Right Margin
- 12.1.13. ruler—Draw a Ruler Across the Width and Height of the Window
- 12.1.14. box_block, box_block_at—Print Text Surrounded by a Box
- 12.1.15. clear_area, clear_area_at—Clear an Area of the Screen
- 12.1.16. box_area, box_area_at—Draw a Box Around an Area
- 12.1.17. screen-demo—Saving and Redisplaying Areas of the Screen
- 12.2. Summary
-
12.1.
- 13. Backing Up the Drive
- 14. Aging, Archiving, and Deleting Files
-
15. Covering All Your Databases
- 15.1.
-
15.2. shdb-funcs: Shell Database Function Library
- 15.2.1. load_db—Import Database into Shell Array
- 15.2.2. split_record—Split a Record into Fields
- 15.2.3. csv_split—Extract Fields from CSV Records
- 15.2.4. put_record—Assemble a Database Record from an Array
- 15.2.5. put_csv—Assemble Fields into a CSV Record
- 15.2.6. db-demo—View and Edit a Password File
- 15.3. PhoneBase: A Simple Phone Number Database
- 15.4. Summary
- 16. Home on the Web
- 17. Taking Care of Business
- 18. Random Acts of Scripting
-
19. A Smorgasbord of Scripts
-
19.1.
- 19.1.1. topntail—Remove Top and Bottom Lines from a File
- 19.1.2. flocate—Locate Files by Filename Alone
- 19.1.3. sus—Display a POSIX Man Page
- 19.1.4. cwbw—Count Words Beginning With
- 19.1.5. cci—Configure, Compile, and Install from Tarball
- 19.1.6. ipaddr—Find a Computer's Network Address
- 19.1.7. ipaddr.cgi—Print the Remote Address of an HTTP Connection
- 19.1.8. iprev—Reverse the Order of Digits in an IP Address
- 19.1.9. intersperse—Insert a String Between Doubled Characters
- 19.1.10. ll—Use a Pager for a Directory Listing Only If Necessary
- 19.1.11. name-split—Divide a Person's Full Name into First, Last, and Middle Names
- 19.1.12. rot13—Encode or Decode Text
- 19.1.13. showfstab—Show Information from /etc/fstab
- 19.1.14. unique—Remove All Duplicate Lines from a File
- 19.2. Summary
-
19.1.
- 20. Script Development Management
- A. Internet Scripting Resources
Product information
- Title: Shell Scripting Recipes: A Problem-Solution Approach
- Author(s):
- Release date: May 2005
- Publisher(s): Apress
- ISBN: 9781590594711
You might also like
book
Shell Scripting Recipes: A Problem-Solution Approach, Second Edition
Shell Scripting Recipes is filled with over 150 much-needed and practical recipes that follow a problem-solution …
book
Wicked Cool Shell Scripts
Fun and functional Linux, Mac OS X and UNIX shell scripts The UNIX shell is the …
book
Robust Automatic Speech Recognition
Robust Automatic Speech Recognition: A Bridge to Practical Applications establishes a solid foundation for automatic speech …
book
Solaris™ 7 Reference
2004H-5 Thoroughly cross-referenced and packed with comprehensive examples for administrators and programmers Easy-to-understand explanations of UNIX …