我需要等待输入 20 秒,然后 myscript 应该继续执行。
我尝试过使用read -t20 var
,但这仅适用于 bash。我在 Solaris 10 上使用 ksh。
有人能帮助我吗?
编辑:20 秒只是一个例子。让我们假设它需要等待 1 小时。但是这个人可以或不能在PC前面写输入,他不需要等待1个小时来输入输入,但是如果他不在PC前面,那么shell应该在等待后继续执行一段时间。
谢谢!
我需要等待输入 20 秒,然后 myscript 应该继续执行。
我尝试过使用read -t20 var
,但这仅适用于 bash。我在 Solaris 10 上使用 ksh。
有人能帮助我吗?
编辑:20 秒只是一个例子。让我们假设它需要等待 1 小时。但是这个人可以或不能在PC前面写输入,他不需要等待1个小时来输入输入,但是如果他不在PC前面,那么shell应该在等待后继续执行一段时间。
谢谢!
来自man ksh
:
TMOUT
如果设置为大于零的值,如果在发出 PS1 提示后的规定秒数内未输入命令,则 shell 终止。可以使用此值的最大界限来编译 shell,该界限不能超过。
我不确定这是否适用read
于ksh
Solaris。它确实适用于 ksh93,但该版本也有read -t
.
该脚本包括这种方法:
# Start the (potentially blocking) read process in the background
(read -p && print "$REPLY" > "$Tmp") & readpid=$!
# Now start a "watchdog" process that will kill the reader after
# some time:
(
sleep 2; kill $readpid >/dev/null 2>&1 ||
{ sleep 1; kill -1 $readpid >/dev/null 2>&1; } ||
{ sleep 1; kill -9 $readpid; }
) & watchdogpid=$!
# Now wait for the reading process to terminate. It will terminate
# reliably, either because the read terminated, or because the
# "watchdog" process made it terminate.
wait $readpid
# Now stop the watchdog:
kill -9 $watchdogpid >/dev/null 2>&1
REPLY=TERMINATED # Assume the worst
[[ -s $Tmp ]] && read < "$Tmp"
看看这个论坛帖子它在第三个帖子中有答案。