Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add log_all_output #84

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,32 @@ echo -n >file
printf '' >file
```

## Use `exec` to change the shell's file descriptors
mnp marked this conversation as resolved.
Show resolved Hide resolved

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.
mnp marked this conversation as resolved.
Show resolved Hide resolved

**Example Function:**

```sh
log_all_output() {
# Usage: log_all_output log_filename
exec > "$1" 2>&1
mnp marked this conversation as resolved.
Show resolved Hide resolved
}
```

**Example Usage:**

```shell
echo This is not logged
mnp marked this conversation as resolved.
Show resolved Hide resolved
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:**
Expand Down
26 changes: 26 additions & 0 deletions manuscript/chapter4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,32 @@ echo -n >file
printf '' >file
```

## Use `exec` to change the shell's 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.

**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:**
Expand Down
12 changes: 11 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
mnp marked this conversation as resolved.
Show resolved Hide resolved
echo yy stderr >&2
)
contents="$(<"test.log")"
mnp marked this conversation as resolved.
Show resolved Hide resolved
assert_equals "$contents" $'xx stdout\nyy stderr'
}

assert_equals() {
if [[ "$1" == "$2" ]]; then
((pass+=1))
Expand All @@ -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
Expand Down