Add log_all_output

This commit is contained in:
Mitchell Perilstein 2019-09-21 11:52:50 -04:00
parent 70dbe8feba
commit 22d69241d1
3 changed files with 63 additions and 1 deletions

View File

@ -1031,6 +1031,32 @@ echo -n >file
printf '' >file
```
## Use `exec` to change the shell's file descriptors.
Calling `exec` with redirection to a file can be used for logging all output.
**Example Function:**
```sh
log_all_output() {
# Usage: log_all_output log_filename
exec > "$1" 2>&1
}
```
**Example Usage:**
```shell
echo This is not logged
echo Nor is this error >&2
# all output will go to this file
log_all_output myoutput.log
echo normal output
echo this is an error >&2
```
## Extract lines between two markers
**Example Function:**

View File

@ -164,6 +164,32 @@ echo -n >file
printf '' >file
```
## Use `exec` to change the shell's file descriptors.
Calling `exec` with redirection to a file can be used for logging all output.
**Example Function:**
```sh
log_all_output() {
# Usage: log_all_output log_filename
exec > "$1" 2>&1
}
```
**Example Usage:**
```shell
echo This is not logged
echo Nor is this error >&2
# all output will go to this file
log_all_output myoutput.log
echo normal output
echo this is an error >&2
```
## Extract lines between two markers
**Example Function:**

12
test.sh
View File

@ -205,6 +205,16 @@ test_split() {
assert_equals "${result[*]}" "hello world my name is john"
}
test_log_all_output() {
(
log_all_output test.log
echo xx stdout
echo yy stderr >&2
)
contents="$(<"test.log")"
assert_equals "$contents" $'xx stdout\nyy stderr'
}
assert_equals() {
if [[ "$1" == "$2" ]]; then
((pass+=1))
@ -219,7 +229,7 @@ assert_equals() {
}
main() {
trap 'rm readme_code test_file' EXIT
trap 'rm readme_code test_file test.log' EXIT
# Extract code blocks from the README.
while IFS=$'\n' read -r line; do