LoginSignup
12
7

More than 5 years have passed since last update.

超シンプルなコマンドラインタイマーを作った

Last updated at Posted at 2015-05-28

ほぼtirだけど、もっとずっと簡単な、秒数計算して通知センターにテキスト出すだけのやつ。

iTunesを呼ばないで、とも言う。

シェルスクリプト

#!/bin/bash
# @(#)Commandline timer with notification center
# @(#)Usage: $0 [-h] <time h/m/s>
readonly SCRIPTNAME="$(basename "$0")"
export sec=0
for arg in "$@"; do
  if [[ "$arg" =~ ([0-9]+)([hms]) ]];then
    num=${BASH_REMATCH[1]}
    unit=${BASH_REMATCH[2]}
    case "$unit" in
      h)
        sec=$((sec += num * 3600))
        ;;
      m)
        sec=$((sec += num * 60))
        ;;
      s)
        sec=$((sec += num))
        ;;
    esac
  fi
done
echo "$SCRIPTNAME: After $sec sec"
sleep "$sec"
osascript -e "display notification \"$sec sec elapsed.\" with title \"$SCRIPTNAME\""

timer 1h 10m 30sのように使う。
15分の3回分を計算できないほど疲れ切っているときは、timer 15m 15m 15mで45分カウントできる。

常にバックグラウンドで実行するためのラッパースクリプト

  • 毎回timer 10m &とかやるのかったるいのでシェル関数化。
    • 別に全部関数で書いてもいいんだけどね。
  • 実行ファイルより関数が優先されるので、名前は同じでいい。
  • nohupで、ターミナルエミュレータのタブを閉じても動くようにした。
~/.bashrc
timer () { 
 command nohup timer "$*" & 
}

参考

12
7
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
12
7