You are previewing Unix Shell Programming, Third Edition.
O'Reilly logo
Unix Shell Programming, Third Edition

Book Description

Unix Shell Programming is a tutorial aimed at helping Unix and Linux users get optimal performance out of their operating out of their operating system. It shows them how to take control of their systems and work efficiently by harnessing the power of the shell to solve common problems. The reader learns everything he or she needs to know to customize the way a Unix system responds.

The vast majority of Unix users utilize the Korn shell or some variant of the Bourne shell, such as bash. Three are covered in the third edition of Unix Shell Programming. It begins with a generalized tutorial of Unix and tools and then moves into detailed coverage of shell programming.

Topics covered include: regular expressions, the kernel and the utilities, command files, parameters, manipulating text filters, understanding and debugging shell scripts, creating and utilizing variables, tools, processes, and customizing the shell.

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Authors
  3. We Want to Hear from You!
    1. Reader Services
  4. 1. Introduction
  5. 2. 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. Standard Input/Output and I/O Redirection
      1. Standard Input and Standard Output
      2. Output Redirection
      3. Input Redirection
    6. Pipes
      1. Filters
    7. Standard Error
    8. More on Commands
      1. Typing More Than One Command on a Line
      2. Sending a Command to the Background
      3. The ps Command
    9. Command Summary
    10. Exercises
  6. 3. 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. Pipeline Hookup
      5. Environment Control
      6. Interpreted Programming Language
  7. 4. 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 Choice of Characters: The [...] Construct
      5. Matching Zero or More Characters: The Asterisk (*)
      6. Matching a Precise Number of Characters: \{...\}
      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
    9. Exercises
  8. 5. And Away We Go
    1. Command Files
      1. Comments
    2. Variables
      1. Displaying the Values of Variables
      2. The Null Value
      3. Filename Substitution and Variables
      4. The ${variable} Construct
    3. Built-in Integer Arithmetic
    4. Exercises
  9. 6. 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
    5. Exercises
  10. 7. 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
    6. ${n}
    7. The shift Command
    8. Exercises
  11. 8. 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
    9. Exercises
  12. 9. 'Round and 'Round She Goes
    1. The for Command
      1. The $@ Variable
      2. The for Without the List
      3. The while Command
    2. The until Command
    3. 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
    4. The getopts Command
    5. Exercises
  13. 10. 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
    3. Exercises
  14. 11. Your Environment
    1. Local Variables
      1. Subshells
    2. Exported Variables
      1. export -p
    3. PS1 and PS2
    4. HOME, James
    5. Your 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
    11. Exercises
  15. 12. 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
    7. Exercises
  16. 13. 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. Inline Input Redirection
      3. Shell Archives
    5. Functions
      1. Removing a Function Definition
      2. The return Command
    6. The type Command
    7. Exercises
  17. 14. Rolo Revisited
    1. Design Considerations
    2. rolo
    3. add
    4. lu
    5. display
    6. rem
    7. change
    8. listall
    9. Sample Output
    10. Exercises
  18. 15. 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 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. The Restricted Shell rsh
    14. Miscellaneous Features
      1. Other Features of the cd Command
      2. Tilde Substitution
      3. Order of Search
    15. Compatibility Summary
    16. Exercises
  19. 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
  20. B. For More Information
    1. Online Documentation
    2. Documentation on the Web
    3. Books
      1. O'Reilly & Associates
      2. Sams and Que
      3. Other Publishers