Shell programming is sometimes shunned in favor of scripting in Perl and other interpreted languages, and thought of as simply a glorified version of DOS batch programmingbut as a UNIX administrator, you'll find that being able to write and interpret shell scripts is a central skill. True, in its simplest form, shell programming is like DOS batch programming in that you can use it to execute a list of commands stored in a file that could also be executed from the command line. However, the shell programs you might write can have far greater flexibility and sophistication than a simple DOS batch program, and shell scripting has certain unique advantages even over languages such as Perl or C. Here are the top seven reasons why every FreeBSD user should learn shell programming:
Furthermore, some of the most crucial components of FreeBSD are actually shell scripts, including (but not limited to) the resource configuration scripts in /etc and the start/stop shell scripts that control third-party daemon programs, which live in /usr/local/etc/rc.d (as you will see in Chapter 14, "System Configuration and Startup Scripts"). Having a thorough grounding in shell scripting will make it much easier for you to understand how those scripts work, and therefore to come up with clever solutions to problems that might arise with your configuration. Shell scripting is like having a jack in your trunk: Most of the time, it's just there taking up space. But when you suddenly get a flat tire, you find yourself very glad you took the time to stow it there. It's to your advantage to take the time to understand the shell, how it interprets commands, how to send the output of one command down a pipeline to use as the input for another command, and the hundreds of specialized FreeBSD commands you'll use to develop the commands that make up your shell scripts. You can start that learning process by working with a simple shell program, as you do in the section that follows. Note The first part of this chapter covers only commands that are available in all Bourne-type shells. The second part covers the enhanced features of the Korn and POSIX shells, such as FreeBSD uses. If you need to write shell programs that will be run on other systems and you cannot say for sure what shells will be available there, it is best to stick with the commands available in all Bourne shells. If you don't, your programs might not work on other systems that have an older Bourne shell. Note This chapter does not cover C shell scripting at all. Although it is possible to write shell scripts using the C shell, its programming syntax is a lot more limited than the incompatible one found in Bourne-style shells, and lacks a lot of very useful shell-scripting features (including functions and swapping STDOUT and STDERR). If you're using a C shell (csh or tcsh) for your login sessions and you plan to do a lot of shell programming, you might want to consider switching to bash (the modern iteration of the Bourne shell) for better session compatibility and ease of testing. Tip There is a lot more to shell scripting than a single chapter can cover. If you are interested in learning more about shell scripting, refer to Teach Yourself Shell Programming in 24 Hours, available from Sams Publishing, for more information. |