Chapter 10. Shell Programming


IN THIS CHAPTER

  • Why Shell Programming Matters to You

  • A Simple Shell Program

  • Variables

  • Interacting with the User

  • Arithmetic in Shell Programs

  • Loops

  • Conditional Statements

  • Exit Status

  • Functions

  • Filehandles

  • Debugging Shell Scripts

  • Advanced Features of Korn Shell Scripting

In Chapter 8, "Working with the Shell," you saw how to operate the shell interactively at the command line. To the casual user, the shell looks like nothing more than a prompt where you can type commands and launch bigger, more useful programs. What you may not have realized is that a powerful programming language is built right into the shell, and when you're at the shell prompt you're actually in the middle of a "program" that you're writing line by line with every command you type. More usefully, you can write a shell script (which you might know as a "batch file," though shell scripts are far more powerful) full of commands that you would otherwise have to issue manually, creating a program you can then run at your discretion. You can use shell programming to do everything from automating a repetitive list of commands to writing sophisticated interactive programs that process text data to storing and retrieving information in simple databases.

The modular design of the FreeBSD operating system allows you to call any FreeBSD command from within a shell program. And if you can't find a FreeBSD command to do what you want, you can string commands together into pipelines to create new commands. As you learn in this chapter, pipelines take the output from one command and use it as the input for another command. FreeBSD has hundreds of these modular commands that each serve a small but highly specialized purpose. In this chapter, you learn how to write a simple shell program and how to use variables, arithmetic, and loops in shell programming. You also learn how to use conditional statements, file descriptors, and many of the advanced techniques of shell programming to perform a number of important tasks in FreeBSD.

Shell programs offer commands for searching text files, combining text from two files into a single file, formatting text in columns, cutting only certain fields from text files, counting characters, words, and lines in a file, performing math operations, doing file backups, compressing and uncompressing files, and much more. If you can't find a command or pipeline that does what you want, chances are you can find a free program available on the Internet that you can use in your shell program. You'll find all the important techniques you'll need for taking full advantage of the flexibility of shell programming in this chapter.




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