1

Here's what will happen, a message is displayed with a specified time waiting for keypress, if no keypress then it will resume.

Example

"Press ESC to exit, otherwise you will die.. 3..2..1"

"Press 'x' to procrastinate and check email, read some blogs, facebook, twitter.. otherwise you will resume work for 12 hours.. 3..2..1"

This should be a really handy function. How do I create this functionality in bash?

4

2 回答 2

6

在 bash 手册页上查找“read”命令并注意“-t timeout”选项。这样的事情应该让你开始

for i in 3 2 1 ; do 
    read -p $i... -n 1 -t 1 a && break
done
于 2009-12-22T02:01:34.533 回答
3

使用 read bash 内置命令的 -t 和 -n 选项,也不要忘记 -r 和 -s。(详见说明书)

#!/bin/bash

timeout=3

echo -n "Press ESC to exit, otherwise you will die..."
while [ $timeout -gt 0 ]; do
    echo -n " $timeout"
    if read -n1 -t1 -r -s x; then
        echo
        exit 0
    fi
    let timeout--
done
echo
于 2009-12-22T02:03:52.190 回答