From d38d18ec25736bfdf4c8739c7b40ec96c10fd0f3 Mon Sep 17 00:00:00 2001 From: Hongli Lai Date: Wed, 23 Oct 2019 08:19:16 +0200 Subject: [PATCH 1/2] rbenv help: fix 'type: write error: Broken pipe' Sometimes the command fails with a 'type: write error: Broken pipe'. This is because 'head -1' only reads the first line, then exits. If 'type' writes the second line after 'head -1' has already exited, then the aforementioned error is triggered. We fix this by buffering the entire output of 'type' before invoking 'head -1'. --- libexec/rbenv-help | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libexec/rbenv-help b/libexec/rbenv-help index 0b7e926a..d1296d62 100755 --- a/libexec/rbenv-help +++ b/libexec/rbenv-help @@ -42,8 +42,13 @@ extract_initial_comment_block() { } collect_documentation() { + local all_awks + local first_awk + all_awks=$(type -p gawk awk) + first_awk=$(head -1 <<<"$all_awks") + # shellcheck disable=SC2016 - $(type -p gawk awk | head -1) ' + "$first_awk" ' /^Summary:/ { summary = substr($0, 10) next From af454a32dc943a92125021690309086a17bd6802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 23 Oct 2019 12:12:38 +0200 Subject: [PATCH 2/2] Silence errors when piping `type | head -1` --- libexec/rbenv | 2 +- libexec/rbenv-help | 12 +++++++----- libexec/rbenv-hooks | 2 +- libexec/rbenv-versions | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libexec/rbenv b/libexec/rbenv index 4880df38..a1b9d69f 100755 --- a/libexec/rbenv +++ b/libexec/rbenv @@ -28,7 +28,7 @@ if enable -f "${BASH_SOURCE%/*}"/../libexec/rbenv-realpath.dylib realpath 2>/dev else [ -z "$RBENV_NATIVE_EXT" ] || abort "failed to load \`realpath' builtin" - READLINK=$(type -p greadlink readlink | head -1) + READLINK=$(type -p greadlink readlink 2>/dev/null | head -1) [ -n "$READLINK" ] || abort "cannot find readlink - are you missing GNU coreutils?" resolve_link() { diff --git a/libexec/rbenv-help b/libexec/rbenv-help index d1296d62..5b3af232 100755 --- a/libexec/rbenv-help +++ b/libexec/rbenv-help @@ -42,13 +42,15 @@ extract_initial_comment_block() { } collect_documentation() { - local all_awks - local first_awk - all_awks=$(type -p gawk awk) - first_awk=$(head -1 <<<"$all_awks") + local awk + awk="$(type -p gawk awk 2>/dev/null | head -1)" + if [ -z "$awk" ]; then + echo "rbenv: cannot find awk" >&2 + return 1 + fi # shellcheck disable=SC2016 - "$first_awk" ' + "$awk" ' /^Summary:/ { summary = substr($0, 10) next diff --git a/libexec/rbenv-hooks b/libexec/rbenv-hooks index 010c7058..64334cc7 100755 --- a/libexec/rbenv-hooks +++ b/libexec/rbenv-hooks @@ -26,7 +26,7 @@ if ! enable -f "${BASH_SOURCE%/*}"/rbenv-realpath.dylib realpath 2>/dev/null; th echo "rbenv: failed to load \`realpath' builtin" >&2 exit 1 fi -READLINK=$(type -p greadlink readlink | head -1) +READLINK=$(type -p greadlink readlink 2>/dev/null | head -1) if [ -z "$READLINK" ]; then echo "rbenv: cannot find readlink - are you missing GNU coreutils?" >&2 exit 1 diff --git a/libexec/rbenv-versions b/libexec/rbenv-versions index d1a110ce..21a9bdf2 100755 --- a/libexec/rbenv-versions +++ b/libexec/rbenv-versions @@ -33,7 +33,7 @@ if ! enable -f "${BASH_SOURCE%/*}"/rbenv-realpath.dylib realpath 2>/dev/null; th exit 1 fi - READLINK=$(type -p greadlink readlink | head -1) + READLINK=$(type -p greadlink readlink 2>/dev/null | head -1) if [ -z "$READLINK" ]; then echo "rbenv: cannot find readlink - are you missing GNU coreutils?" >&2 exit 1