address review

This commit is contained in:
Mitchell Perilstein 2019-09-21 13:10:41 -04:00
parent 764ebd09ab
commit 4b34f7220f
3 changed files with 24 additions and 20 deletions

View File

@ -1031,30 +1031,32 @@ echo -n >file
printf '' >file
```
## Use `exec` to change the shell's file descriptors
## Use `exec` to redirect script output to different file descriptors
Use `exec` to redirection any output to the given file descriptors. In the example, stdout and stderr are both being redirected such that output of any commands afterwards will go to that file.
`exec` can be used to redirect script output to file descriptors. In the example below, `stdout` and `stderr` are redirected to `myoutput.log` (a file).
**Example Function:**
```sh
log_all_output() {
# Usage: log_all_output log_filename
exec > "$1" 2>&1
exec &>"$1"
}
```
**Example Usage:**
```shell
echo This is not logged
echo Nor is this error >&2
# These are *not* logged.
printf 'This is a message.\n'
printf 'This is an error message.\n' >&2
# all output will go to this file
# All script output will go to 'myoutput.log'.
log_all_output myoutput.log
echo normal output
echo this is an error >&2
# These are now logged.
printf 'This is a message.\n'
printf 'This is an error message.\n' >&2
```
## Extract lines between two markers

View File

@ -164,30 +164,32 @@ echo -n >file
printf '' >file
```
## Use `exec` to change the shell's file descriptors
## Use `exec` to redirect script output to different file descriptors
Use `exec` to redirection any output to the given file descriptors. In the example, stdout and stderr are both being redirected such that output of any commands afterwards will go to that file.
`exec` can be used to redirect script output to file descriptors. In the example below, `stdout` and `stderr` are redirected to `myoutput.log` (a file).
**Example Function:**
```sh
log_all_output() {
# Usage: log_all_output log_filename
exec > "$1" 2>&1
exec &>"$1"
}
```
**Example Usage:**
```shell
echo This is not logged
echo Nor is this error >&2
# These are *not* logged.
printf 'This is a message.\n'
printf 'This is an error message.\n' >&2
# all output will go to this file
# All script output will go to 'myoutput.log'.
log_all_output myoutput.log
echo normal output
echo this is an error >&2
# These are now logged.
printf 'This is a message.\n'
printf 'This is an error message.\n' >&2
```
## Extract lines between two markers

View File

@ -208,11 +208,11 @@ test_split() {
test_log_all_output() {
(
log_all_output test.log
echo xx stdout
echo yy stderr >&2
printf stdout
printf stderr >&2
)
contents="$(<"test.log")"
assert_equals "$contents" $'xx stdout\nyy stderr'
contents="$(<test.log)"
assert_equals "$contents" "stdoutstderr"
}
assert_equals() {