From 22d69241d14b50450164a4cc3d1351a0342b6f89 Mon Sep 17 00:00:00 2001 From: Mitchell Perilstein Date: Sat, 21 Sep 2019 11:52:50 -0400 Subject: [PATCH 1/6] Add log_all_output --- README.md | 26 ++++++++++++++++++++++++++ manuscript/chapter4.txt | 26 ++++++++++++++++++++++++++ test.sh | 12 +++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1f284a..f9f3f7d 100644 --- a/README.md +++ b/README.md @@ -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:** diff --git a/manuscript/chapter4.txt b/manuscript/chapter4.txt index 902fec6..22f01b3 100644 --- a/manuscript/chapter4.txt +++ b/manuscript/chapter4.txt @@ -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:** diff --git a/test.sh b/test.sh index 950dfb6..49317db 100755 --- a/test.sh +++ b/test.sh @@ -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 From 764ebd09ab7dc6941693298c85ab7b42b22e61a4 Mon Sep 17 00:00:00 2001 From: Mitchell Perilstein Date: Sat, 21 Sep 2019 11:58:56 -0400 Subject: [PATCH 2/6] cleanup --- README.md | 4 ++-- manuscript/chapter4.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f9f3f7d..eaa1ed1 100644 --- a/README.md +++ b/README.md @@ -1031,9 +1031,9 @@ echo -n >file printf '' >file ``` -## Use `exec` to change the shell's file descriptors. +## Use `exec` to change the shell's file descriptors -Calling `exec` with redirection to a file can be used for logging all output. +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:** diff --git a/manuscript/chapter4.txt b/manuscript/chapter4.txt index 22f01b3..a6e7ac3 100644 --- a/manuscript/chapter4.txt +++ b/manuscript/chapter4.txt @@ -164,9 +164,9 @@ echo -n >file printf '' >file ``` -## Use `exec` to change the shell's file descriptors. +## Use `exec` to change the shell's file descriptors -Calling `exec` with redirection to a file can be used for logging all output. +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:** From 4b34f7220ff6041f740ca0f3d7766ba2390ac57b Mon Sep 17 00:00:00 2001 From: Mitchell Perilstein Date: Sat, 21 Sep 2019 13:10:41 -0400 Subject: [PATCH 3/6] address review --- README.md | 18 ++++++++++-------- manuscript/chapter4.txt | 18 ++++++++++-------- test.sh | 8 ++++---- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index eaa1ed1..f26c5de 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/manuscript/chapter4.txt b/manuscript/chapter4.txt index a6e7ac3..d06288e 100644 --- a/manuscript/chapter4.txt +++ b/manuscript/chapter4.txt @@ -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 diff --git a/test.sh b/test.sh index 49317db..e8a2758 100755 --- a/test.sh +++ b/test.sh @@ -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="$( Date: Sun, 22 Sep 2019 11:42:49 -0400 Subject: [PATCH 4/6] add stop_logging_output function and test --- README.md | 22 ++++++++++++++++++---- manuscript/chapter4.txt | 22 ++++++++++++++++++---- test.sh | 5 ++++- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f26c5de..17491ce 100644 --- a/README.md +++ b/README.md @@ -1040,23 +1040,37 @@ printf '' >file ```sh log_all_output() { # Usage: log_all_output log_filename + exec 6>&1 # save stdout + exec 7>&2 # save stderr exec &>"$1" } + +stop_logging_output() { + exec 1>&6 6>&- # Restore stdout + exec 2>&7 7>&- # Restore stderr +} ``` **Example Usage:** ```shell # These are *not* logged. -printf 'This is a message.\n' -printf 'This is an error message.\n' >&2 +printf 'This is a normal message.\n' +printf 'This is a normal error message.\n' >&2 # All script output will go to 'myoutput.log'. log_all_output myoutput.log # These are now logged. -printf 'This is a message.\n' -printf 'This is an error message.\n' >&2 +printf 'This is a logged message.\n' +printf 'This is a logged error message.\n' >&2 + +# Return to normal. +stop_logging_output + +# These are not logged. +printf 'This is a normal message.\n' +printf 'This is a normal error message.\n' >&2 ``` ## Extract lines between two markers diff --git a/manuscript/chapter4.txt b/manuscript/chapter4.txt index d06288e..5bde5af 100644 --- a/manuscript/chapter4.txt +++ b/manuscript/chapter4.txt @@ -173,23 +173,37 @@ printf '' >file ```sh log_all_output() { # Usage: log_all_output log_filename + exec 6>&1 # save stdout + exec 7>&2 # save stderr exec &>"$1" } + +stop_logging_output() { + exec 1>&6 6>&- # Restore stdout + exec 2>&7 7>&- # Restore stderr +} ``` **Example Usage:** ```shell # These are *not* logged. -printf 'This is a message.\n' -printf 'This is an error message.\n' >&2 +printf 'This is a normal message.\n' +printf 'This is a normal error message.\n' >&2 # All script output will go to 'myoutput.log'. log_all_output myoutput.log # These are now logged. -printf 'This is a message.\n' -printf 'This is an error message.\n' >&2 +printf 'This is a logged message.\n' +printf 'This is a logged error message.\n' >&2 + +# Return to normal. +stop_logging_output + +# These are not logged. +printf 'This is a normal message.\n' +printf 'This is an normal error message.\n' >&2 ``` ## Extract lines between two markers diff --git a/test.sh b/test.sh index e8a2758..10d0449 100755 --- a/test.sh +++ b/test.sh @@ -210,7 +210,10 @@ test_log_all_output() { log_all_output test.log printf stdout printf stderr >&2 - ) + stop_logging_output + printf stdout + printf stderr >&2 + ) &> /dev/null contents="$( Date: Sun, 22 Sep 2019 11:45:06 -0400 Subject: [PATCH 5/6] untabify --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 17491ce..7b48d6a 100644 --- a/README.md +++ b/README.md @@ -1040,14 +1040,14 @@ printf '' >file ```sh log_all_output() { # Usage: log_all_output log_filename - exec 6>&1 # save stdout - exec 7>&2 # save stderr + exec 6>&1 # save stdout + exec 7>&2 # save stderr exec &>"$1" } stop_logging_output() { - exec 1>&6 6>&- # Restore stdout - exec 2>&7 7>&- # Restore stderr + exec 1>&6 6>&- # Restore stdout + exec 2>&7 7>&- # Restore stderr } ``` From d07ed2d3257ad31a6896fe57b861a2e0387f5d9f Mon Sep 17 00:00:00 2001 From: Mitchell Perilstein Date: Sun, 22 Sep 2019 23:39:49 -0400 Subject: [PATCH 6/6] Put back the printf. I felt they were more clear the other way. --- README.md | 12 ++++++------ manuscript/chapter4.txt | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7b48d6a..ac373eb 100644 --- a/README.md +++ b/README.md @@ -1055,22 +1055,22 @@ stop_logging_output() { ```shell # These are *not* logged. -printf 'This is a normal message.\n' -printf 'This is a normal error message.\n' >&2 +printf 'This is a message.\n' +printf 'This is an error message.\n' >&2 # All script output will go to 'myoutput.log'. log_all_output myoutput.log # These are now logged. -printf 'This is a logged message.\n' -printf 'This is a logged error message.\n' >&2 +printf 'This is a message.\n' +printf 'This is an error message.\n' >&2 # Return to normal. stop_logging_output # These are not logged. -printf 'This is a normal message.\n' -printf 'This is a normal error message.\n' >&2 +printf 'This is a message.\n' +printf 'This is an error message.\n' >&2 ``` ## Extract lines between two markers diff --git a/manuscript/chapter4.txt b/manuscript/chapter4.txt index 5bde5af..ab49d89 100644 --- a/manuscript/chapter4.txt +++ b/manuscript/chapter4.txt @@ -173,14 +173,14 @@ printf '' >file ```sh log_all_output() { # Usage: log_all_output log_filename - exec 6>&1 # save stdout - exec 7>&2 # save stderr + exec 6>&1 # save stdout + exec 7>&2 # save stderr exec &>"$1" } stop_logging_output() { - exec 1>&6 6>&- # Restore stdout - exec 2>&7 7>&- # Restore stderr + exec 1>&6 6>&- # Restore stdout + exec 2>&7 7>&- # Restore stderr } ``` @@ -188,22 +188,22 @@ stop_logging_output() { ```shell # These are *not* logged. -printf 'This is a normal message.\n' -printf 'This is a normal error message.\n' >&2 +printf 'This is a message.\n' +printf 'This is an error message.\n' >&2 # All script output will go to 'myoutput.log'. log_all_output myoutput.log # These are now logged. -printf 'This is a logged message.\n' -printf 'This is a logged error message.\n' >&2 +printf 'This is a message.\n' +printf 'This is an error message.\n' >&2 # Return to normal. stop_logging_output # These are not logged. -printf 'This is a normal message.\n' -printf 'This is an normal error message.\n' >&2 +printf 'This is a message.\n' +printf 'This is an error message.\n' >&2 ``` ## Extract lines between two markers