我正在编写一个交互式shell脚本,需要在尽可能多的系统上运行。是否有一种与标准POSIX系统兼容的实现以下功能的替代方法?
#! /bin/bash
echo -n "Do you wish to continue? (Y/n) 5 seconds to respond... "
read -n 1 -t 5 answer # accepts a single character, 5 second timeout.
if [ "$answer" = "n" ] || [ "$answer" = "N" ] ; then
echo -e "\nExiting..."
exit
fi
echo -e "\nContinuing with script..."
# More code
read
上的超时对我来说是最重要的(read -t 5
)。读取的一个字符限制是可取的,但不是必需的(read -n 1
)。理想情况下,脚本可以在POSIX系统上工作,也可以在bash中工作,而不必启用特殊的POSIX兼容模式。
发布于 2015-08-28 11:35:36
根据迪基的回答,以下内容似乎奏效了,如果按下“n”或“N”以外的任何内容,则继续使用脚本。
据我所知,所有的标准设置都是posix。
#!/bin/sh
read_char() {
old=$(stty -g)
stty raw -echo min 0 time 50
eval "$1=\$(dd bs=1 count=1 2>/dev/null)"
stty $old
}
printf "Do you wish to continue? (Y/n) 5 seconds to respond..."
read_char answer
# answer=$(getch)
# answer=$(getche)
if [ "$answer" = "n" ] || [ "$answer" = "N" ] ; then
printf "\nExiting...\n"
exit
fi
printf "\nContinuing with script...\n"
替代"read_char":
这避免了使用eval (可能是不安全的)。
getch() {
old=$(stty -g)
stty raw -echo min 0 time 50
printf '%s' $(dd bs=1 count=1 2>/dev/null)
stty $old
}
这样就避免了eval并打印按下的键。
getche() {
old=$(stty -g)
stty raw min 0 time 50
printf '%s' $(dd bs=1 count=1 2>/dev/null)
stty $old
}
发布于 2015-08-25 23:56:31
stty
程序提供了这样做的方法。xterm
有几个脚本(在其源代码的“vttest”子目录中),它们保存、修改和恢复终端设置,以允许它读取终端的响应以转义序列。下面是dynamic2.sh
的一部分(开始设置printf
或echo
,以使用$CMD
变量来解决一些旧系统):
echo "reading current color settings"
exec </dev/tty
old=`stty -g`
stty raw -echo min 0 time 5
original=
for N in $FULL
do
$CMD $OPT "${ESC}]$N;?^G${SUF}" > /dev/tty
read reply
eval original$N='${reply}${SUF}'
original=${original}${reply}${SUF}
done
stty $old
if ( trap "echo exit" EXIT 2>/dev/null ) >/dev/null
then
trap '$CMD $OPT "$original" >/dev/tty; exit' EXIT HUP INT TRAP TERM
else
trap '$CMD $OPT "$original" >/dev/tty; exit' 0 1 2 5 15
fi
https://stackoverflow.com/questions/32213758
复制相似问题