Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [99]

By Root 970 0
Intended for interactive use.

-e

errexit

Exit the shell when a command exits with nonzero status.

-f

noglob

Disable wildcard expansion.

-h

Locate and remember the location of commands called from function bodies when the function is defined, instead of when the function is executed (XSI).

-m

monitor

Enable job control (on by default). Intended for interactive use.

-n

noexec

Read commands and check for syntax errors, but don't execute them. Interactive shells are allowed to ignore this option.

-u

nounset

Treat undefined variables as errors, not as null.

-v

verbose

Print commands (verbatim) before running them.

-x

xtrace

Print commands (after expansions) before running them.

ignoreeof

Disallow Ctrl-D to exit the shell.

nolog

Disable command history for function definitions.

vi

Use vi-style command-line editing. Intended for interactive use.

Perhaps surprisingly, set does not set shell variables (unlike the same command in the BSD C shell). That task is accomplished with simple variable = value assignments.

* * *

Tip


Although not part of POSIX, the command set -o emacs is widely implemented (ksh88, ksh93, bash, zsh). If you're already comfortable with emacs, using this command gives you a one-line mini-screen editor that accepts emacs commands for working with your shell history.

* * *

The special variable $- is a string representing the currently enabled shell options. Each option's short option letter appears in the string if that option is enabled. This can be used to test option settings, like so:

case $- in

*C*) ... The noclobber option is enabled

;;

esac

* * *

Warning


Interestingly enough, while the POSIX standard goes to some lengths to make it possible to save and restore the state of shell variables and traps, there is no defined way to save a list of function definitions for later reuse. This appears to be a simple oversight in the standard. We will show how to do this in Section 14.1.

* * *

* * *

[7] It thus violates the "do one thing well" Software Tools principle. The reason for this is that Steven Bourne wished to avoid having lots of reserved commands built into the shell.

Summary

The read command reads lines and splits the data into fields, for assigning to named shell variables. The -r option provides some control over how data is read.

I/O redirection allows you to change the source or destination of one program, or multiple programs running together in a subshell or code block. Besides redirecting to or from files, pipelines let you hook multiple programs together. Here documents provide inline input.

File descriptor manipulation, particularly of file descriptors 1 and 2, is a fundamental operation, used repeatedly in everyday scripting.

printf is a flexible, albeit somewhat complicated, command for generating output. Most of the time, it can be used in a simple manner, but its power is occasionally needed and valuable.

The shell performs a number of expansions (or substitutions) on the text of each command line: tilde expansion (if supported) and wildcards; variable expansion; arithmetic expansion; and command substitution. Wildcarding now includes POSIX character classes for locale-dependent matching of characters in filenames. By convention, "dot files" are not included in wildcard expansions. Variable and arithmetic expansion were described in Chapter 6. Command substitution has two forms: `...` is the original form, and $(...) is the newer, easier-to-write form.

Quoting protects different source-code elements from special treatment by the shell. Individual characters may be quoted by preceding them with a backslash. Single quotes protect all enclosed characters; no processing is done on the quoted text, and it's impossible to embed a single quote into single-quoted text. Double quotes group the enclosed items into a single word or argument, but variable, arithmetic, and command substitutions are still applied to the contents.

Return Main Page Previous Page Next Page

®Online Book Reader