From 9bac51b1a691a6eef23fd856733dd600231b0199 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 28 Sep 2021 12:09:32 -0700 Subject: [PATCH] fix redirection inconsistencies across shells --- sh/confirm | 11 +++-------- sh/pause | 11 +++-------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/sh/confirm b/sh/confirm index 29a4107..d966bfc 100755 --- a/sh/confirm +++ b/sh/confirm @@ -27,11 +27,6 @@ ### 20 files changed, 406 insertions(+), 29 deletions(-) ### ``` -# known inconsistencies: -# since the fallbacks pipe stderr into stdin, -# this will behave differently between zsh and non-zsh shells: -# ( echo hi | confirm | wc -c ) 2>/dev/null - if [ -n "$ZSH_VERSION" ]; then confirm() { [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; } @@ -43,10 +38,10 @@ if [ -n "$ZSH_VERSION" ]; then elif [ -n "$BASH_VERSION" ]; then confirm() { [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; } - read -n1 -p "Continue? [y/N] " c + read -n1 -p "Continue? [y/N] " c 2>&1 echo [ "$c" = y ] || [ "$c" = Y ] - } <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe + } 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe else confirm() ( [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; } @@ -57,7 +52,7 @@ else c="$(dd ibs=1 count=1 2>/dev/null)" echo [ "$c" = y ] || [ "$c" = Y ] - ) <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe + ) 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe fi [ -n "${preload+-}" ] || confirm "$@" diff --git a/sh/pause b/sh/pause index 3f0c059..232bb4d 100755 --- a/sh/pause +++ b/sh/pause @@ -13,19 +13,14 @@ ### $ ### ``` -# known inconsistencies: -# since the fallbacks pipe stderr into stdin, -# this will behave differently between zsh and non-zsh shells: -# ( echo hi | pause | wc -c ) 2>/dev/null - if [ -n "$ZSH_VERSION" ]; then pause() { read -sk $'?Press any key to continue\n' } elif [ -n "$BASH_VERSION" ]; then pause() { - read -n1 -sp $'Press any key to continue\n' - } <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe + read -n1 -sp $'Press any key to continue\n' 2>&1 + } 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe else pause() ( old="$(stty -g)" @@ -33,7 +28,7 @@ else printf 'Press any key to continue\n' stty -icanon -echo dd ibs=1 count=1 2>/dev/null >/dev/null - ) <&2 >/dev/tty # try to ensure this is a terminal instead of a pipe + ) 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe fi [ -n "${preload+-}" ] || pause "$@"