mirror of
https://github.com/notwa/rc
synced 2024-11-05 00:29:02 -08:00
fix leaky reads and add argc checking
This commit is contained in:
parent
9bac51b1a6
commit
f15ff1c965
2 changed files with 13 additions and 6 deletions
10
sh/confirm
10
sh/confirm
|
@ -29,22 +29,24 @@
|
|||
|
||||
if [ -n "$ZSH_VERSION" ]; then
|
||||
confirm() {
|
||||
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
read -q '?Continue? [y/N] '
|
||||
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
local c=
|
||||
read -q '?Continue? [y/N] ' c
|
||||
ret=$?
|
||||
echo >/dev/tty
|
||||
return $ret
|
||||
}
|
||||
elif [ -n "$BASH_VERSION" ]; then
|
||||
confirm() {
|
||||
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
local c=
|
||||
read -n1 -p "Continue? [y/N] " c 2>&1
|
||||
echo
|
||||
[ "$c" = y ] || [ "$c" = Y ]
|
||||
} 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; }
|
||||
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
old="$(stty -g)"
|
||||
trap 'stty "$old"' INT EXIT
|
||||
printf 'Continue? [y/N] '
|
||||
|
|
9
sh/pause
9
sh/pause
|
@ -15,14 +15,19 @@
|
|||
|
||||
if [ -n "$ZSH_VERSION" ]; then
|
||||
pause() {
|
||||
read -sk $'?Press any key to continue\n'
|
||||
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
local c=
|
||||
read -sk $'?Press any key to continue\n' c
|
||||
}
|
||||
elif [ -n "$BASH_VERSION" ]; then
|
||||
pause() {
|
||||
read -n1 -sp $'Press any key to continue\n' 2>&1
|
||||
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
local c=
|
||||
read -n1 -sp $'Press any key to continue\n' c 2>&1
|
||||
} 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
|
||||
else
|
||||
pause() (
|
||||
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
|
||||
old="$(stty -g)"
|
||||
trap 'stty "$old"' INT EXIT
|
||||
printf 'Press any key to continue\n'
|
||||
|
|
Loading…
Reference in a new issue