カウントダウンタイマーの作成方法

カウントダウンタイマーの作成方法

20 secカウントダウンタイマーをから降順に表示する必要があります。00 sec

20秒以内にパスワードを入力できない場合は、メッセージで終了します。君の時間は終わった

出力:

you have 20 sec to enter password :
you have 15 sec to enter password :
you have 10 sec to enter password :
you have 00 sec to enter password :

メッセージを表示する作業コードの一部

read -t 20 -p 'Enter your password : ' 
status=$?   
if [ $status -eq 142 ]
then
    echo "your time is over"
fi

答え1

誰かがより良い解決策を見つけるまで:

#!/bin/bash

tmout=20

(
        while [[ $tmout -gt 0 ]]; do
                printf '\rPlease respond within %02d seconds: ' "$tmout" >&2
                sleep 1
                tmout=$(( tmout - 1 ))
        done
) & prompt_pid=$!

read -s -t "$tmout"
read_ok=$?
echo ''

if [[ $read_ok -eq 0 ]]; then
        kill "$prompt_pid"
        printf 'You responded with "%s"\n' "$REPLY"
else
        echo 'You did not respond in time'
fi

$tmoutこれにより、数秒間または終了するまで毎秒プロンプトを更新するバックグラウンドジョブが開始されます。プロンプトテキストの前にキャリッジリターン文字があります\r\rを使用して出力すると、カーソルはprintf行の先頭に戻ります。つまり、文字列の残りの部分が以前に出力されたテキストを上書きし、カチカチしているカウンターのように見えます。出力テキスト文字列がprintf常に同じ長さになるように、意図的にゼロで埋められた2桁の整数をカウンタとして使用します(少なくとも$tmout100未満の値の場合)。

その後、フォアグラウンド操作はタイムアウトを使用して$tmoutユーザー入力を数秒待ちます。readここではパスワードを読むために-s使用しreadます(これは入力した内容が表示されず、出力プロンプトによって複雑にならないという意味でもあります)。

返却時にreadプロンプ​​トループを終了し(まだ実行中の場合)、終了readした方法に従ってメッセージを印刷します。

答え2

以下はスニペットです。 00秒に達すると、5秒をさらに待ちます。これが望ましい動作であるかどうかは不明なので、条件を使用して編集できます。

#!/bin/bash
c1=5
c2=4
count=20
status=''
while [[ "$count" != 0 ]]   
    do
        count="$(expr $c1 \* $c2)"
        c2="$(expr $c2 \- 1)"
        read -t 5 -p  "You have $count sec to enter your password : "$'\n'
        status=$?
        if [ "$status" -ne 142 ] ; then
          break
        fi

done
if [ "$status" -eq 142 ]
then
    echo "Your time is over"
    
else 
   echo "Success"
fi 

###編集する###

出力1:

[root@host~]# ./lol.sh
You have 20 sec to enter your password :
You have 15 sec to enter your password :
You have 10 sec to enter your password :
You have 5 sec to enter your password :
You have 0 sec to enter your password :
Your time is over

出力2:

[root@host~]# ./lol.sh
You have 20 sec to enter your password :
LOL
Success
[root@host~]#

##コメントで提案されているように、2つのグローバルソリューションをさらに編集します。 ##:

これを行うには、カウンタを正確に20秒に変更できます。TMT&ちょっと待って便利な定数。

#!/bin/bash
##CALCULATTION :
##TIMEOUT=WAIT_SECONDS * NB_ITERATIONS 
##TIMEOUT modulo WAIT_SECONDS should  equal to 0 to get a fixed iterations number
# 20  =   5 * 4 
wait_sec=5
tmt=20
modulo_ok=1
fpr=1
count="$tmt"
# Modulo Test
if [[ "$(($tmt % $wait_sec))" -eq 0 ]]; then
    nb_iters="$(expr $tmt \/ $wait_sec)"
    modulo_ok=0
fi
if [[ "modulo_ok" -eq 0 ]]; then
    (while [[ "$count" != 0 ]] && [[ "$fpr" -gt 0 ]]
        do
            count="$(expr $wait_sec \* $nb_iters)"
            nb_iters="$(expr $nb_iters \- 1)"
            echo "You have $count sec to enter your password :" 
            ps -p "$pid" &> /dev/null 
            sleep $wait_sec
    done
    ) &  fpr=$?  
    bpr=$!
    read -t "$tmt" -s pass
    kill $bpr > /dev/null 2>&1 
    if [[ -z  "$pass" ]]
    then
        echo "Your time is over"
    else 
       echo "Success"
       echo "Your password is : $pass"
    fi 
else 
    echo 'Timeout modulo Wait seconds should be equal to 0'
fi

出力3: CASE - > tmt間隔中にパスワードが設定される

[root@host~]# ./lol2.sh
You have 20 sec to enter your password :
You have 15 sec to enter your password :
Success
Your password is : Reda

出力4:ケース->タイムアウト

[root@host~]# ./lol2.sh
You have 20 sec to enter your password :
You have 15 sec to enter your password :
You have 10 sec to enter your password :
You have 5 sec to enter your password :
Your time is over

出力5:CASE -> tmt%wait_secが0ではありません。

[root@host~]# ./lol2.sh
Timeout modulo Wait seconds should be equal to 0

関連情報