readline-sync/lib/read.sh

30 lines
986 B
Bash
Raw Normal View History

2015-03-01 18:07:28 +01:00
silentRead() {
# `typeset` doesn't work in `func()` of ksh.
# `function fnc` for local-var of ksh is not compatible.
# Therefore, `_input` is not local-var on ksh.
local _input="" 2>/dev/null || typeset _input=""
2015-03-01 18:00:20 +01:00
stty --file=/dev/tty -echo echonl 2>/dev/null || \
stty -F /dev/tty -echo echonl 2>/dev/null || \
stty -f /dev/tty -echo echonl 2>/dev/null || \
exit 1
2015-03-01 18:07:28 +01:00
IFS= read -r _input </dev/tty
2015-03-01 18:00:20 +01:00
stty --file=/dev/tty echo -echonl 2>/dev/null || \
stty -F /dev/tty echo -echonl 2>/dev/null || \
stty -f /dev/tty echo -echonl 2>/dev/null
2015-03-01 18:07:28 +01:00
printf '%s' "$_input"
}
if [ "$1" = "noechoback" ]; then
# Try `-s` option. *ksh have it that not `--silent`. Therefore, don't try it.
if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then
IFS= read -rs INPUT </dev/tty 2>/dev/null || INPUT=`silentRead` || exit 1
printf '\n' >/dev/tty
else
INPUT=`silentRead` || exit 1
fi
2014-07-12 00:37:25 +02:00
else
2015-03-01 07:06:53 +01:00
IFS= read -r INPUT </dev/tty
2014-07-12 00:37:25 +02:00
fi
2015-03-01 07:06:53 +01:00
printf '%s' "'$INPUT'"
2014-06-27 03:08:05 +02:00
exit 0