13

Basic Shell Scripting

In addition to being an environment for interacting with the computer, the shell can be used for programming. Shell programs, often called scripts, are used for a variety of purposes, most notably system administration. Shell scripts are simply a series of system commands stored in a single file. These commands are executed automatically and in order each time the file is invoked.

Think for a moment about the possibilities. Any program on the machine can be run from the shell, with input and output automatically redirected according to your desires. Once you get the hang of shell scripting, you can automate a vast range of jobs with a few simple scripts. Even better, the jobs that work best in shell scripts are the ones that are most irritating to do by hand, whether because they're boring or because they're repetitive.

This chapter introduces the basic concepts of shell scripting. The scripts are simple and may seem trivial, but they clearly illustrate the underlying mechanics that you need to master before you move on to subsequent chapters and the more advanced methods they contain.

Commenting and Documenting Scripts

Good documenting is critical for good programming. It's easy, especially on systems with multiple administrators, to have a bunch of scripts lying around on the disk. Most of these scripts are probably variations on the same concept, and it's likely that nobody knows much about any of them. Even on a single-user system, you may write scripts ...

Get Beginning Unix now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.