Introduction

A shell is a program that takes commands typed by the user and calls the operating system to run those commands. For example, you may use the shell to enter a command to list the files in a directory, such as ls, or a command to copy a file, such as cp. A shell acts as a form of wrapper around the operating system, hence the term shell.

Unix and Linux systems support many different kinds of shells, each with its own command syntax. You can run a shell window to enter shell commands from a graphical desktop environment.

Shell scripts combine commands together and act similarly to batch files. With a shell and shell scripts, you can customize your system, automate tedious daily tasks, better perform your work, and get more out of your computers.

No matter how nice a graphical user interface your system sports, adding the power of a shell can dramatically increase the speed and efficiency of what you can do on your computer. Shells provide greater control over your system and allow you to do some things that simply cannot be done from the graphical environment. Shells often allow you to perform tasks remotely, which proves useful if you need to do something to a large number of computers, or computers located at another site.

Furthermore, Apple Computer chose Unix, an inherently scriptable operating system, as the basis for Mac OS X. In the Macintosh environment, you get the ability to create scripts along with a really neat desktop user interface.

As many small devices grow in functionality, you'll find shells even on low-end PDAs and handheld computers.

Many users fear the command line, which does seem kind of retro in this era of graphical user interfaces. But the immense power of shell scripts makes it worthwhile to learn about the shell and scripting.

This book covers shells, shell scripts, and a vast array of techniques for getting the most out of your systems with scripts.

Whom This Book Is For

This book is aimed at anyone who wants to get more out of their computer systems. This especially includes Mac OS X users who want to take advantage of the powerful underpinnings of Mac OS X, and Linux users who want to learn more about their systems.

Windows users can take advantage of shells and shell scripts to work around some of the brain-dead features of that operating system.

You do not need to have any prior experience writing programs or scripts. Wrox "Beginning" books start with the basics and lead you through the entire process. If you do happen to have programming experience, many of the topics will prove easier to pick up.

What Does This Book Cover?

This book covers shells and shell scripting with a special emphasis on the Bourne shell, the most commonly available shell. Special features of the C shell, bash, and Korn shells are also covered, but the emphasis is on scripts that are compatible with the Bourne shell. This will allow your scripts to work on the largest number of systems.

This book starts with the basics of shells and then covers how to make shell scripts. Special chapters cover interacting with the operating system and more complicated programming techniques. After reading this book, you should have a thorough grounding in how to create shell scripts, as well as the ability to find out more, an important task in today's ever-changing world of technology.

Throughout this book, you'll find a plethora of practical examples. We promise not to show how clever we are and instead focus on what works.

How This Book Is Structured

The first five chapters of the book cover the basics of shell scripting. Chapters 6 to 11 address more advanced scripting topics, including awk and sed, two commands that are used in a great many scripts. The final chapters of the book show how to apply scripting to status monitoring, systems administration, and the desktop.

The following is a summary of the topics covered in each chapter:

  • Chapter 1, Introducing Shells: In this first chapter, you see what a shell is and how shells work. You discover the great variety of shells as well as how to find the shell on your system. You learn how to call up shell windows so that you can type in commands. You also learn about the default shells on Mac OS X, Windows (under Cygwin), Linux, QNX, and other systems.

  • Chapter 2, Introducing Shell Scripts: This chapter extends the discussion of shells to introduce shell scripts. You find out what a script is, how to create scripts, as well as how to run scripts. Because scripting requires a text editor, you'll find a review and recommendation of editors for Linux, Unix, Windows, and Mac OS X.

  • Chapter 3, Controlling How Scripts Run: This chapter introduces how scripts run, as well as how you can control which commands in your scripts get executed. You find out how to have your scripts make decisions and take alternate paths as needed.

  • Chapter 4, Interacting with the Environment: No shell is an island. This chapter covers how your shells can find out about the surrounding environment. This chapter delves into how to pass data to a shell script and how to modify the environment. Furthermore, you'll see how the magic #! line works and you'll learn how to create executable commands from your scripts.

  • Chapter 5, Scripting with Files: Virtually all modern operating systems allow you to store data in files. This is important so that most scripts interact with files of some sort. To help with this, Chapter 5 covers how to create, read, modify, and remove files.

  • Chapter 6, Processing Text with sed: Sed provides a stream, or batch-mode text editor. Using sed for text editing would be excruciatingly painful. But, using sed from scripts allows you to transform files with simple shell commands. This chapter covers how to call on sed's power from your scripts.

  • Chapter 7, Processing Text with awk: Awk is a special tool for working with text. Zillions of shell scripts use awk for sophisticated parsing and control over text. This chapter introduces awk and how you can call on awk from your scripts.

  • Chapter 8, Creating Command Pipelines: Shells and shell scripts come out of the ancient Unix traditions. These traditions include the quaint notion that each command should perform one task and one task only. The Unix philosophy includes the ability to combine these building-block commands into more complex commands. Chapter 8 covers how to create command pipelines, where the shell sends the output of one command to the input of another.

  • Chapter 9, Controlling Processes: Chapter 9 discusses the terms "program," "process," and "process IDs." It shows how you can query about processes, kill processes, and gather information about what is running on your system. This chapter also covers how to call on other processes from your scripts and then query how the processes ran, such as whether the processes succeeded or failed.

  • Chapter 10, Shell Scripting Functions: As your shell scripts grow in size and complexity, you can use functions to help simplify your scripts. Functions also make it easier to reuse parts of a script in other scripts you write. Chapter 10 covers functions as they pertain to shell scripts, how to create functions, and how to use them.

  • Chapter 11, Debugging Shell Scripts: Much as you'd like, no script is perfect. Software, all software, sports little problems called bugs. Debugging is the art of locating and destroying these problems. This chapter covers techniques for finding bugs as well as tips to reduce the occurrence of bugs before they happen.

  • Chapter 12, Graphing Data with MRTG: MRTG is a really clever package for showing complex data sets as graphs. MRTG works especially well for graphing aspects of your computing environment such as network throughput, memory resources, and disk usage. Chapter 12 shows how you can call MRTG from your scripts and use MRTG to help monitor your systems.

  • Chapter 13, Scripting for Administrators: Chapter 13 extends the discussion in Chapter 12 to cover more general techniques for administering systems using shell scripts.

  • Chapter 14, Scripting for the Desktop: Scripts are often associated with server systems only, but you'd be surprised at how well scripts can work on the desktop. This chapter covers how to call on your desktop applications from scripts, such as office suites, multimedia applications, and especially the Mac OS X desktop.

  • Appendixes: Appendix A contains the answers to chapter exercises. Appendix B covers the most useful commands available on Unix, Linux, Mac OS X, and Windows (under Cygwin).

What Do You Need to Use This Book?

This book covers shell scripting. To try out the examples, you will need the following.

  • A shell, such as the Bourne shell

  • Text-editing software

Chapter 1 covers the shell requirements in depth and describes how to find the shell on your system. For Windows users, you should download and install the Cygwin package, also described in Chapter 1.

Chapter 2 covers text-editing software as these packages relate to shells. You'll find a list of editors, as well as recommendations.

Conventions

To help you get the most from the text and keep track of what's happening, a number of conventions are used throughout the book:

Note

Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

  • Important words are italicized when first introduced.

  • Keyboard strokes appear like this: Ctrl-A.

  • Code within the text appears in a monospaced typeface, like this: persistence.properties.

  • Code examples are presented in the following way:

    Code examples appear in monofont with a gray background.
  • Within code blocks containing both input and output text, input appears in bold font.

Keyboards differ between systems. For example, Windows and Linux systems usually sport a keyboard with an Enter key. Mac OS X keyboards from Apple Computer label the key Return instead. This book uses Enter as this is the most common form.

Source Code

As you work through the examples in this book, you may choose either to type all the code manually or use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Once at the site, simply locate the book's title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book.

Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 0-7645-8320-4.

After you download the code, just decompress it with your favorite compression tool. Alternatively, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration; at the same time, you will be helping us provide even higher quality information.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book's details page, click the Book Errata link. On this page, you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book's errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot "your" error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to email you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.

  2. Read the terms of use and click Agree.

  3. Complete the required information to join as well as any optional information you wish to provide and click Submit.

  4. You will receive an email with information describing how to verify your account and complete the joining process.

    You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum emailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.128