Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
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
Comment thread
mnp marked this conversation as resolved.
Outdated

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.
Comment thread
mnp marked this conversation as resolved.
Outdated

**Example Function:**

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

**Example Usage:**

```shell
echo This is not logged
Comment thread
mnp marked this conversation as resolved.
Outdated
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
Comment thread
mnp marked this conversation as resolved.
Outdated
echo yy stderr >&2
)
contents="$(<"test.log")"
Comment thread
mnp marked this conversation as resolved.
Outdated
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