Title graphic

Here is where the fun begins

With the thousands of commands available for the command line user, how can you remember them all? The answer is, you don't. The real power of the computer is its ability to do the work for you. To get it to do that, we use the power of the shell to automate things. We write scripts.

Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard. In addition to the things you have learned so far, the shell also provides a variety of useful programming features to make your scripts truly powerful.

What are scripts good for? A wide range of tasks can be automated. Here are some of the things I automate with scripts:

  • A script gathers up all the files (over 2200) in this site on my computer and transmits them to my web server.

  • The SuperMan pages are built entirely by a script.

  • Every Friday night, all my computers copy their files to a "backup server" on my network. This is performed by a script.

  • A script automatically gets the current updates from my Linux vendor and maintains a repository of vital updates. It sends me an email message with a report of tasks that need to be done.

As you can see, scripts unlock the power of your Linux machine. So let's have some fun!

Contents

  1. Writing your first script and getting it to work
    1. Writing a script
    2. Setting permissions
    3. Putting it in your path
  2. Editing the scripts you already have
    1. Commands, commands, everywhere
    2. Aliases
    3. Shell functions
    4. type
    5. .bashrc
  3. Here Scripts
    1. Writing an HTML file with a script
  4. Substitutions - Part 1
    1. Variables
    2. How to create a variable
    3. Where does the variable's name come from?
    4. How does this increase our laziness?
    5. Environment Variables
  5. Substitutions - Part 2
    1. --help and other tricks
    2. Assigning a command's result to a variable
    3. Constants
  6. Quoting
    1. Single and double quotes
    2. Quoting a single character
    3. Other backslash tricks
  7. Shell Functions
    1. Keep your scripts working
  8. Some Real Work
    1. show_uptime
    2. drive_space
    3. home_space
    4. system_info
  9. Flow Control - Part 1
    1. if
    2. What is a "condition"?
    3. Exit status
    4. test
    5. exit
    6. Testing for root
  10. Stay Out of Trouble
    1. Empty variables
    2. Missing quotes
    3. Isolating problems
    4. Watching your script run
  11. Keyboard Input and Arithmetic
    1. read
    2. Arithmetic
  12. Flow Control - Part 2
    1. More branching
    2. Loops
    3. Building a menu
  13. Positional Parameters
    1. Detecting command line arguments
    2. Command line options
    3. Getting an option's argument
    4. Integrating the command line processor into the script
    5. Adding interactive mode
  14. Flow Control - Part 3
  15. Errors and Signals and Traps (Oh My!) - Part 1
    1. Exit status
    2. Checking the exit status
    3. An error exit function
    4. AND and OR lists
    5. Improving the error exit function
  16. Errors and Signals and Traps (Oh My!) - Part 2
    1. Cleaning up after yourself
    2. trap
    3. Signal 9 From Outer Space
    4. A clean_up function
    5. Creating safe temporary files

© 2000-2014, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.

Linux® is a registered trademark of Linus Torvalds.