Chapter 8. Scripting and System Administration IN THIS CHAPTER Running External Programs Command-Line Options and Arguments The Shell Library Accessing Environment Variables Scripting in Microsoft Windows Working with Files, Directories, and Trees Miscellaneous Scripting Tasks Summary Thus spake the master programmer: "Though a program be but three lines long, someday it will have to be maintained." Geoffrey James, The Tao of Programming As programmers, we often need to glue programs together with little scripts that are able to talk to the operating system at a fairly high level and run external programs. This is especially true in the UNIX world, where shell scripts are relied on daily for countless tasks. Ruby isn't always a convenient glue language because it is more general-purpose than that. But in the long run, anything that can be done in ksh, bash, or the others can also be done in Ruby. In many cases, you might just as well use one of the more traditional languages for this purpose. The advantage that Ruby has, of course, is that it really is a general-purpose language, full-featured and truly object-oriented. On the theory that people might want to use Ruby to talk to the OS at this level, we present here a few tricks that might prove useful. We've found this chapter a little hard to organize because much of the functionality could logically be grouped in different ways. If you don't find what you are looking for in the expected place, scan the rest of the chapter also. In addition, much of what could be covered here is actually dealt with in other chapters entirely. Refer in particular to Chapter 4, "External Data Manipulation," which covers file I/O and attributes of files. These features are frequently used in the kind of scripts we will discuss in this chapter. |