Comment detail

制限時間内のキー入力検査 (Nested Flatten)

pure bashで。最大1秒未満の誤差。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash

function InputChecker(){
    local timeout=$1
    local str=$2

    echo -n "input($str) =>"
    read -n1 c
    t1=$SECONDS
    read s
    t=$(( $SECONDS - $t1 ))

    echo -n 'result => '
    if (( $t >= $timeout )) ; then
        echo 'TIME OUT'
    elif [ "$c$s" == "$str" ] ; then
        echo 'OK'
    else
        echo 'NG'
    fi
}

InputChecker $1 $2
InputChecker $1 $2
InputChecker $1 $2

Index

Feed

Other

Link

Pathtraq

loading...