LoginSignup
2
0

More than 3 years have passed since last update.

[macOS]シェルスクリプトで簡易的なカウントダウンタイマーを作成する

Posted at

概要

本書はシェルスクリプトを使用して簡易的なカウントダウンタイマーを作成します。

前提条件

  • Macから音が出るようにボリュームを調整してください。
  • 本書ではMacBookAir2019(macOS Catalina)を使用しました。

スクリプト内容

#!/bin/sh

<< COMMENTOUT
countdown_timer(macOS)
Copyright (c) 2020 yuichi1992_west
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
COMMENTOUT

# A variable that checks whether only a numerical value (seconds) is entered using a regular expression.
timer_count='[0-9]+'

# Prompt for the time to play the sound (specify the number of seconds).
echo "Prompt for the time to play the sound (specify the number of seconds)."
echo "For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day."
echo "After entering, press Enter.  "
read timer_seconds

# Compare the entered value with the regular expression.
if [[ $timer_seconds =~ $timer_count ]] ;
then

    # If the regular expression is correct, start the countdown.
    echo ""
    echo "Start the countdown."

    # Increments by 1 with a for statement from 0 to the entered value.
    for ((i=0;i<$timer_seconds;i++))
    do

        # Output the remaining time.
        echo ""
        echo "Remaining time (seconds)"
        expr $timer_seconds - $i
        echo ""

        # Wait 1 second.
        sleep 1

    done

    # Sound the alarm (standard audio of macOS) 10 times.
    echo "It's time! Play the alarm!"
    for ((k=0;k<=10;k++))
    do
        afplay /System/Library/Sounds/Ping.aiff
    done

    read -p "Press [Enter] key to resume."

else

    # If the entered value is not a number, a warning will be prompted.
    echo ""
    echo "Important!! Characters other than numbers are included. Enter only half-width numbers from 0 to 9."
    echo "For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day."
    echo ""

    read -p "Press [Enter] key to resume."

fi

exit 0      

動作結果

成功の場合

% bash countdown_timer.sh
Prompt for the time to play the sound (specify the number of seconds).
For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day.
After entering, press Enter.
5

Start the countdown.

Remaining time (seconds)
5


Remaining time (seconds)
4


Remaining time (seconds)
3


Remaining time (seconds)
2


Remaining time (seconds)
1

It's time! Play the alarm!

(システムで用意されている音声を10回鳴らします)

Press [Enter] key to resume.
  • 途中でアラームを止める場合はcontrol + Cを押してください。

失敗の場合

% bash countdown_timer.sh
Prompt for the time to play the sound (specify the number of seconds).
For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day.
After entering, press Enter.
aaa

Important!! Characters other than numbers are included. Enter only half-width numbers from 0 to 9.
For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day.

Press [Enter] key to resume.

動作について

  • 正規表現(数値のみ)の変数を宣言する。
timer_count='[0-9]+'
  • 音声を鳴らす時間をユーザーに入力させる。
read timer_seconds
  • 入力した値が全て数値(0-9)であるか判定する。
if [[ $timer_seconds =~ $timer_count ]] ;
  • for文を利用してカウントダウンを始める。1秒ごとにiの値を1増やし、入力した時間になったらfor文から抜ける。
for ((i=0;i<$timer_seconds;i++))
  • 残り時間(入力した時間 - iの値)を計算する。
expr $timer_seconds - $i
  • 1秒待機する。
sleep 1
  • iの値が入力した時間に達したら音声を10回鳴らす。
for ((k=0;k<=10;k++))
  • システムで用意された音声を鳴らす。
afplay /System/Library/Sounds/Ping.aiff
  • 10回鳴らした後、ユーザーにEnterキーを押させてスクリプトを終了する。
read -p "Press [Enter] key to resume."

最後に

ターミナルを立ち上げて本書のシェルスクリプトを実行することでカウントダウンタイマーが利用できます。またfor ((k=0;k<=10;k++))からkの値を変更してアラーム回数を増減させたり、afplay /System/Library/Sounds/Ping.aiffから別のシステム音声や好きな曲に変更する、というカスタマイズもできます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0