Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [80]

By Root 886 0
(at least in the POSIX shell) have no provision for local variables.[3] Thus, all functions share variables with the parent script; this means you have to be careful not to change something that the parent script doesn't expect to be changed, such as PATH. It also means that other state is shared, such as the current directory and traps for signals. (Signals and traps are discussed in Section 13.3.2.)

* * *

[3] All of bash, ksh88, ksh93, and zsh do provide for local variables, but not necessarily using the same syntax.

Summary

Variables are necessary for any serious programming. Shell variables hold string values, and a large array of operators for use in ${ var...} lets you control the results of variable substitution.

The shell provides a number of special variables (those with nonalphanumeric names, such as $? and $!), that give you access to special information, such as command exit status. The shell also has a number of special variables with predefined meanings, such as PS1, the primary prompt string. The positional parameters and special variables $* and $@ give you access to the arguments used when a script (or function) was invoked. env, export, and readonly give you control over the environment.

Arithmetic expansion with $((...)) provides full arithmetic capabilities, using the same operators and precedence as in C.

A program's exit status is a small integer number that is made available to the invoker when the program is done. Shell scripts use the exit command for this, and shell functions use the return command. A shell script can get the exit status of the last command executed in the special variable $?.

The exit status is used for control-flow with the if, while, and until statements, and the !, && and || operators.

The test command, and its alias [...], test file attributes and string and numeric values, and are useful in if, while, and until statements.

The for loop provides a mechanism for looping over a supplied set of values, be they strings, filenames, or whatever else. while and until provide more conventional looping, with break and continue providing additional loop control. The case statement provides a multiway comparison facility, similar to the switch statement in C and C++.

getopts, shift, and $# provide the tools for processing the command line.

Finally, shell functions let you group related commands together and invoke them as a single unit. They act like a shell script, but the commands are stored in memory, making them more efficient, and they can affect the invoking script's variables and state (such as the current directory).

Chapter 7. Input and Output, Files, and Command Evaluation

This chapter completes the presentation of the shell language. We first look at files, both for I/O and for generating filenames in different ways. Next is command substitution, which lets you use the output of a command as arguments on a command line, and then we continue to focus on the command line by discussing the various kinds of quoting that the shell provides. Finally, we examine evaluation order and discuss those commands that are built into the shell.

Standard Input, Output, and Error

Standard I/O is perhaps the most fundamental concept in the Software Tools philosophy. The idea is that programs should have a data source, a data sink (where data goes), and a place to report problems. These are referred to by the names standard input, standard output, and standard error, respectively. A program should neither know, nor care, what kind of device lies behind its input and outputs: disk files, terminals, tape drives, network connections, or even another running program! A program can expect these standard places to be already open and ready to use when it starts up.

Many, if not most, Unix programs follow this design. By default, they read standard input, write standard output, and send error messages to standard error. As we saw in Chapter 5, such programs are called filters because they "filter" streams of data, each one performing some operation on the

Return Main Page Previous Page Next Page

®Online Book Reader