Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [84]

By Root 974 0
shell's file descriptors:

exec 2> /tmp/$0.log Redirect shell's own standard error

exec 3< /some/file Open new file descriptor 3

...

read name rank serno <&3 Read from that file

* * *

Tip


The first example line that redirects the shell's standard error should be used only in a script. Interactive shells print their prompts on standard error; if you run this command interactively, you won't see a prompt! If you wish to be able to undo a redirection of standard error, save the file descriptor first by copying it to a new one. For example:

exec 5>&2 Save original standard error on fd 5

exec 2> /tmp/$0.log Redirect standard error

... Stuff here

exec 2>&5 Copy original back to fd 2

exec 5>&- Close fd 5, no longer needed

* * *

* * *

exec


Usage

exec [ program [ arguments ... ] ]

Purpose

To replace the shell with a new program, or to change the shell's own I/O settings.

Major options

None.

Behavior

With arguments, replace the shell with the named program, passing the arguments on to it. With just I/O redirections, change the shell's own file descriptors.

* * *

When used with arguments, exec serves a different purpose, which is to run the named program in place of the current shell. In other words, the shell starts the new program running in its current process. For example, suppose that you wish to do option processing using the shell, but that most of your task is accomplished by some other program. You can do it this way:

while [ $# -gt 1 ] Loop over arguments

do

case $1 in Process options

-f) # code for -f here

;;

-q) # code for -q here

;;

...

*) break ;; Nonoption, break loop

esac

shift Move next argument down

done

exec real-app -q "$qargs" -f "$fargs" "$@" Run the program

echo real-app failed, get help! 1>&2 Emergency message

When used this way, exec is a one-way operation. In other words, control never returns to the script. The only exception is if the new program can't be invoked. In that case, you may wish to have "emergency" code that at least prints a message and then does any other possible clean-up tasks.

* * *

[1] The make program is used for controlling recompilation of source files into object files. However, it has many uses. For more information, see Managing Projects with GNU make (O'Reilly).

The Full Story on printf

We introduced the printf command in Section 2.5.4. This section completes the description of that command.

* * *

printf


Usage

printf format [ string ... ]

Purpose

To produce output from shell scripts. Since printf's behavior is defined by the POSIX standard, scripts that use it can be more portable than those that use echo.

Major options

None.

Behavior

printf uses the format string to control the output. Plain characters in the string are printed. Escape sequences as described for echo are interpreted. Format specifiers consisting of % and a letter direct formatting of corresponding argument strings. See text for details.

* * *

As we saw earlier, the full syntax of the printf command has two parts:

printf format-string [arguments ...]

The first part is a string that describes the format specifications; this is best supplied as a string constant in quotes. The second part is an argument list, such as a list of strings or variable values, that correspond to the format specifications. The format string combines text to be output literally with specifications describing how to format subsequent arguments on the printf command line. Regular characters are printed verbatim. Escape sequences, similar to those of echo, are interpreted and then output as the corresponding character. Format specifiers, which begin with the character % and end with one of a defined set of letters, control the output of the following corresponding arguments. printf's escape sequences are described in Table 7-1.

Table 7-1. printf escape sequences

Sequence

Description

\a

Alert character, usually the ASCII BEL character.

\b

Backspace.

\c

Suppress any final newline

Return Main Page Previous Page Next Page

®Online Book Reader