Why Shell Programming Matters to You


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:

  • It's easy to learn. If you work with the FreeBSD command line on a regular basis, you probably are already familiar with many of the commands you can use in your shell programs.

  • For many tasks, you can develop a shell program in 5 or 10 minutes that would take hours or even days to develop in C or some other programming language.

  • It can save you hours of tedious work. For example, why go through 100 text files manually to make a single change when you can write a shell program with a for loop that will do it automatically?

  • It will teach you many new and more efficient ways to work with FreeBSD. You will discover useful commands you never knew existed as well as ways to do things you wouldn't ever otherwise have discovered. You might even learn to like the FreeBSD command line better than the X11 GUI or even Windows. Why spend money to buy a new program to perform a certain task when you can string a few FreeBSD commands together in a pipeline to accomplish the same thing?

  • Don't like the way an existing command works? You can write a new one in a few minutes with shell programming. One of the things that makes FreeBSD so powerful is the "do it your own way" philosophy. There are many ways to achieve the same end, and no one way is necessarily the best way.

  • It is perhaps the ideal first programming language because it allows you to concentrate on learning programming logic and other key techniques while working with commands you may already be familiar with and that provide immediate useful results.

  • Shell programming is a skill that's applicable throughout the UNIX world, so it will benefit you whether you're using FreeBSD, Linux, Solaris, Mac OS X, or any other UNIX variant.

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.





FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net