UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: VIII. Shell Programming Chapter 44Next: 44.2 Writing a Simple Shell Program

44. Shell Programming for the Uninitiated

Everyone Should Learn Some Shell Programming
Writing a Simple Shell Program
What's a Shell, Anyway?
Testing How Your System Executes Files
Test String Values with Bourne Shell case
Pattern Matching in case Statements
Exit Status of UNIX Processes
Test Exit Status with the if Statement
Testing Your Success
Loops That Test Exit Status
Set Exit Status of a Shell (Script)
Trapping Exits Caused by Interrupts
read: Reading from the Keyboard
Putting awk, sed, etc., Inside Shell Scripts
Handling Command-Line Arguments in Shell Scripts
Handling Command-Line Arguments with a for Loop
Handling Arguments with while and shift
Standard Command-Line Parsing
The Bourne Shell set Command
test: Testing Files and Strings
Picking a Name for a New Command
Finding a Program Name; Multiple Program Names
Reading Files with the . and source Commands

44.1 Everyone Should Learn Some Shell Programming

One of the great things about UNIX is that it's made up of individual utilities, "building blocks" like cat and grep, that you run from a shell prompt. Using pipes, redirection, filters, and so on, you can combine those utilities to do an incredible number of things. Shell programming lets you take the same commands you'd type at a shell prompt - and put them into a file you can run by just typing its name. You can make new programs that combine UNIX programs (and other shell scripts) in your own way to do exactly what you need. If you don't like the way a program works, you can write a shell script to do just what you want.

Because many UNIX users use the shell every day, they don't need to learn a whole new language for programming... just some tips and techniques. In fact, this chapter covers a lot of programming techniques that you'll want to use even when you aren't programming. For example, loops and tests are handy on the command line.

(This series of articles does assume that you've written programs in some language before, or are generally familiar with programming concepts. If you haven't, you might start with a more comprehensive shell programming book.)

Some of the topics you need to learn about as a beginning shell programmer have already been covered in other chapters. Here are the articles you'll probably want to read - in an order that makes sense if you're looking for something of a tutorial:

This chapter discusses only Bourne shell programming. In most cases, the C shell isn't great for shell programming (47.2).

A note about command versions: unfortunately, the same commands on different versions of UNIX can have different options. Some Bourne shells are a little different from others. For instance, some test (44.20) commands have a -x option to test for an executable file; others don't. As article 46.10 explains, some echo commands use a -n option to mean "no newline at the end of this string"; others have you put \c at the end of the string. And so on. Where there are differences, these articles generally use the commands in Berkeley UNIX. If a command doesn't seem to work on your system, check its online manual page or the sh manual page.

- JP

Previous: VIII. Shell Programming UNIX Power ToolsNext: 44.2 Writing a Simple Shell Program
VIII. Shell Programming Book Index44.2 Writing a Simple Shell Program

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System