You are previewing Shell Programming in Unix, Linux and OS X, Fourth Edition.
O'Reilly logo
Shell Programming in Unix, Linux and OS X, Fourth Edition

Book Description

Shell Programming in Unix, Linux and OS X is a thoroughly updated revision of Kochan and Wood’s classic Unix Shell Programming tutorial. Following the methodology of the original text, the book focuses on the POSIX standard shell, and teaches you how to develop programs in this useful programming environment, taking full advantage of the underlying power of Unix and Unix-like operating systems.

After a quick review of Unix utilities, the book’s authors take you step-by-step through the process of building shell scripts, debugging them, and understanding how they work within the shell’s environment. All major features of the shell are covered, and the large number of practical examples make it easy for you to build shell scripts for your particular applications. The book also describes the major features of the Korn and Bash shells.

Learn how to…

  • Take advantage of the many utilities provided in the Unix system

  • Write powerful shell scripts

  • Use the shell’s built-in decision-making and looping constructs

  • Use the shell’s powerful quoting mechanisms

  • Make the most of the shell’s built-in history and command editing capabilities

  • Use regular expressions with Unix commands

  • Take advantage of the special features of the Korn and Bash shells

  • Identify the major differences between versions of the shell language

  • Customize the way your Unix system responds to you

  • Set up your shell environment

  • Make use of functions

  • Debug scripts

  • Contents at a Glance

    1  A Quick Review of the Basics

    2  What Is the Shell?

    3  Tools of the Trade

    4  And Away We Go

    5  Can I Quote You on That?

    6  Passing Arguments

    7  Decisions, Decisions

    8  ‘Round and ‘Round She Goes

    9  Reading and Printing Data

    10 Your Environment

    11 More on Parameters

    12 Loose Ends

    13 Rolo Revisited

    14 Interactive and Nonstandard Shell Features

    A Shell Summary

    B For More Information

    Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Developer’s Library
    5. Contents at a Glance
    6. Table of Contents
    7. About the Authors
    8. We Want to Hear from You!
    9. Reader Services
    10. Introduction
      1. How This Book Is Organized
      2. Accessing the Free Web Edition
    11. 1. A Quick Review of the Basics
      1. Some Basic Commands
        1. Displaying the Date and Time: The date Command
        2. Finding Out Who’s Logged In: The who Command
        3. Echoing Characters: The echo Command
      2. Working with Files
        1. Listing Files: The ls Command
        2. Displaying the Contents of a File: The cat Command
        3. Counting the Number of Words in a File: The wc Command
        4. Command Options
        5. Making a Copy of a File: The cp Command
        6. Renaming a File: The mv Command
        7. Removing a File: The rm Command
      3. Working with Directories
        1. The Home Directory and Pathnames
        2. Displaying Your Working Directory: The pwd Command
        3. Changing Directories: The cd Command
        4. More on the ls Command
        5. Creating a Directory: The mkdir Command
        6. Copying a File from One Directory to Another
        7. Moving Files Between Directories
        8. Linking Files: The ln Command
        9. Removing a Directory: The rmdir Command
      4. Filename Substitution
        1. The Asterisk
        2. Matching Single Characters
      5. Filename Nuances
        1. Spaces in Filenames
        2. Other Weird Characters
      6. Standard Input/Output, and I/O Redirection
        1. Standard Input and Standard Output
        2. Output Redirection
        3. Input Redirection
      7. Pipes
        1. Filters
      8. Standard Error
      9. More on Commands
        1. Typing More Than One Command on a Line
        2. Sending a Command to the Background
        3. The ps Command
      10. Command Summary
    12. 2. What Is the Shell?
      1. The Kernel and the Utilities
      2. The Login Shell
      3. Typing Commands to the Shell
      4. The Shell’s Responsibilities
        1. Program Execution
        2. Variable and Filename Substitution
        3. I/O Redirection
        4. Hooking up a Pipeline
        5. Environment Control
        6. Interpreted Programming Language
    13. 3. Tools of the Trade
      1. Regular Expressions
        1. Matching Any Character: The Period (.)
        2. Matching the Beginning of the Line: The Caret (^)
        3. Matching the End of the Line: The Dollar Sign $
        4. Matching a Character Set: The [...] Construct
        5. Matching Zero or More Characters: The Asterisk (*)
        6. Matching a Precise Number of Subpatterns: \{...\}
        7. Saving Matched Characters: \(...\)
      2. cut
        1. The -d and -f Options
      3. paste
        1. The -d Option
        2. The -s Option
      4. sed
        1. The -n Option
        2. Deleting Lines
      5. tr
        1. The -s Option
        2. The –d Option
      6. grep
        1. Regular Expressions and grep
        2. The -v Option
        3. The -l Option
        4. The -n Option
      7. sort
        1. The -u Option
        2. The -r Option
        3. The -o Option
        4. The -n Option
        5. Skipping Fields
        6. The -t Option
        7. Other Options
      8. uniq
        1. The -d Option
        2. Other Options
    14. 4. And Away We Go
      1. Command Files
        1. Comments
      2. Variables
        1. Displaying the Values of Variables
        2. Undefined Variables Have the Null Value
        3. Filename Substitution and Variables
        4. The ${variable} Construct
      3. Built-in Integer Arithmetic
    15. 5. Can I Quote You on That?
      1. The Single Quote
      2. The Double Quote
      3. The Backslash
        1. Using the Backslash for Continuing Lines
        2. The Backslash Inside Double Quotes
      4. Command Substitution
        1. The Back Quote
        2. The $(...) Construct
        3. The expr Command
    16. 6. Passing Arguments
      1. The $# Variable
      2. The $* Variable
      3. A Program to Look Up Someone in the Phone Book
      4. A Program to Add Someone to the Phone Book
      5. A Program to Remove Someone from the Phone Book
        1. ${n}
      6. The shift Command
    17. 7. Decisions, Decisions
      1. Exit Status
        1. The $? Variable
      2. The test Command
        1. String Operators
        2. An Alternative Format for test
        3. Integer Operators
        4. File Operators
        5. The Logical Negation Operator !
        6. The Logical AND Operator –a
        7. Parentheses
        8. The Logical OR Operator –o
      3. The else Construct
      4. The exit Command
        1. A Second Look at the rem Program
      5. The elif Construct
        1. Yet Another Version of rem
      6. The case Command
        1. Special Pattern-Matching Characters
        2. The -x Option for Debugging Programs
        3. Back to the case
      7. The Null Command :
      8. The && and || Constructs
    18. 8. 'Round and 'Round She Goes
      1. The for Command
        1. The $@ Variable
        2. The for Without the List
      2. The while Command
      3. The until Command
      4. More on Loops
        1. Breaking Out of a Loop
        2. Skipping the Remaining Commands in a Loop
        3. Executing a Loop in the Background
        4. I/O Redirection on a Loop
        5. Piping Data into and out of a Loop
        6. Typing a Loop on One Line
      5. The getopts Command
    19. 9. Reading and Printing Data
      1. The read Command
        1. A Program to Copy Files
        2. Special echo Escape Characters
        3. An Improved Version of mycp
        4. A Final Version of mycp
        5. A Menu-Driven Phone Program
        6. The $$ Variable and Temporary Files
        7. The Exit Status from read
      2. The printf Command
    20. 10. Your Environment
      1. Local Variables
        1. Subshells
      2. Exported Variables
        1. export -p
      3. PS1 and PS2
      4. HOME
      5. PATH
      6. Your Current Directory
        1. CDPATH
      7. More on Subshells
        1. The .Command
        2. The exec Command
        3. The (...) and { ...; } Constructs
        4. Another Way to Pass Variables to a Subshell
      8. Your .profile File
      9. The TERM Variable
      10. The TZ Variable
    21. 11. More on Parameters
      1. Parameter Substitution
        1. ${parameter}
        2. ${parameter:-value}
        3. ${parameter:=value}
        4. ${parameter:?value}
        5. ${parameter:+value}
        6. Pattern Matching Constructs
        7. ${#variable}
      2. The $0 Variable
      3. The set Command
        1. The -x Option
        2. set with No Arguments
        3. Using set to Reassign Positional Parameters
        4. The -- Option
        5. Other Options to set
      4. The IFS Variable
      5. The readonly Command
      6. The unset Command
    22. 12. Loose Ends
      1. The eval Command
      2. The wait Command
        1. The $! Variable
      3. The trap Command
        1. trap with No Arguments
        2. Ignoring Signals
        3. Resetting Traps
      4. More on I/O
        1. <&- and >&-
        2. In-line Input Redirection
        3. Shell Archives
      5. Functions
        1. Removing a Function Definition
        2. The return Command
      6. The type Command
    23. 13. Rolo Revisited
      1. Data Formatting Considerations
      2. rolo
      3. add
      4. lu
      5. display
      6. rem
      7. change
      8. listall
      9. Sample Output
    24. 14. Interactive and Nonstandard Shell Features
      1. Getting the Right Shell
      2. The ENV File
      3. Command-Line Editing
      4. Command History
      5. The vi Line Edit Mode
        1. Accessing Commands from Your History
      6. The emacs Line Edit Mode
        1. Accessing Commands from Your History
      7. Other Ways to Access Your History
        1. The history Command
        2. The fc Command
        3. The r Command
      8. Functions
        1. Local Variables
        2. Automatically Loaded Functions
      9. Integer Arithmetic
        1. Integer Types
        2. Numbers in Different Bases
      10. The alias Command
        1. Removing Aliases
      11. Arrays
      12. Job Control
        1. Stopped Jobs and the fg and bg Commands
      13. Miscellaneous Features
        1. Other Features of the cd Command
        2. Tilde Substitution
        3. Order of Search
      14. Compatibility Summary
    25. A. Shell Summary
      1. Startup
      2. Commands
      3. Comments
      4. Parameters and Variables
        1. Shell Variables
        2. Positional Parameters
        3. Special Parameters
        4. Parameter Substitution
      5. Command Re-entry
        1. The fc Command
        2. vi Line Edit Mode
      6. Quoting
        1. Tilde Substitution
        2. Arithmetic Expressions
      7. Filename Substitution
      8. I/O Redirection
      9. Exported Variables and Subshell Execution
        1. The (...) Construct
        2. The { ...; } Construct
        3. More on Shell Variables
      10. Functions
      11. Job Control
        1. Shell Jobs
        2. Stopping Jobs
      12. Command Summary
        1. The : Command
        2. The . Command
        3. The alias Command
        4. The bg Command
        5. The break Command
        6. The case Command
        7. The cd Command
        8. The continue Command
        9. The echo Command
        10. The eval Command
        11. The exec Command
        12. The exit Command
        13. The export Command
        14. The false Command
        15. The fc Command
        16. The fg Command
        17. The for Command
        18. The getopts Command
        19. The hash Command
        20. The if Command
        21. The jobs Command
        22. The kill Command
        23. The newgrp Command
        24. The pwd Command
        25. The read Command
        26. The readonly Command
        27. The return Command
        28. The set Command
        29. The shift Command
        30. The test Command
        31. The times Command
        32. The trap Command
        33. The true Command
        34. The type Command
        35. The umask Command
        36. The unalias Command
        37. The unset Command
        38. The until Command
        39. The wait Command
        40. The while Command
    26. B. For More Information
      1. Online Documentation
      2. Documentation on the Web
      3. Books
        1. O’Reilly & Associates
        2. Pearson
    27. Index
    28. Inside Front Cover
    29. Inside Back Cover
    30. Code Snippets