Skip to content

Instantly share code, notes, and snippets.

@opamp
Created November 29, 2011 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opamp/1403337 to your computer and use it in GitHub Desktop.
Save opamp/1403337 to your computer and use it in GitHub Desktop.
昔書いたちょっとしたお休みタイマー ええ、単なるタイマーですよ
#!/usr/local/bin/zsh-4.3.11
#zshスクリプト あなたにおやすみタイマーを提供します。
#mplayerを呼び出すには /usr/local/bin /usr/bin /opt/local/binのいずれかにmplayerが入ってる必要がある
#Mplayerが有効な場合音を鳴らすなら以下の変数を書き換える-------------
SOUNDMUSIC="YES" #鳴らすならYES 値が不正なら無視されNO扱い
MFILE="" #鳴らす音のファイルPATH
LOG="YES" #カウント開始時間と終了時間のログを残すならYES
#---------------------------------------------------------------------
UNAME=`uname` #OS名を取得
SLEEPTIME=0 #スリープ時間を取得
SYS_MPLAYER="YES" #mplayerが使える状態ならYES (自動判定される)
if test $# -le 1 -a $# -ge 3;then #オプションは厳密に2でなければならない 練習にあえて2つの条件で書いた
echo "oyasumi -[s|c|r] time[s|m|h]"
echo "-s is shutdown"
echo "-c is call you with music(you must installed mplayer)"
echo "-r is reboot"
exit 0
fi
if test -f /usr/local/bin/mplayer -o -f /usr/bin/mplayer -o -f /opt/local/bin/mplayer;then
echo "Found out mplayer"
SYS_MPLAYER="YES"
else
echo "Not found mplayer"
SYS_MPLAYER="NO"
fi
SLEEPTIME=$2 #仮の代入
MAXSTR=${#SLEEPTIME} #SLEEPTIMEの文字数を代入
TIMEMOC=`echo "${SLEEPTIME}" | cut -c ${MAXSTR}`
case ${TIMEMOC} in #それぞれの語尾について時間を秒に換算する
'h')
BUF=`echo "${SLEEPTIME}" | cut -c 1-$((${MAXSTR} - 1))` #末尾の一文字を取り除く
BUF=$((BUF*60)) #zshの機能で掛け算をして
echo "MIN=${BUF}"
BUF=$((BUF*60))
echo "SEC=${BUF}"
SLEEPTIME=${BUF} #最終的に秒に換算
;; #分も同様
'm')
BUF=`echo "${SLEEPTIME}" | cut -c 1-$((${MAXSTR} - 1))`
BUF=$((BUF*60))
echo "SEC=${BUF}"
SLEEPTIME=${BUF}
;;
's')
SLEEPTIME=`echo "${SLEEPTIME}" | cut -c 1-$((${MAXSTR} - 1))`
;;
*)
echo "error. please input number with h or m or s"
exit 1
esac
#この時点でSLEEPTIMEにはスリープすべき数値が正しく代入されているハズである。
STIME=`date`
echo "sleep start"
echo "${STIME}"
sleep "${SLEEPTIME}" #スリープ
echo "COUNT END"
date
if test ${SYS_MPLAYER} = "YES" -a ${SOUNDMUSIC} = "YES";then
mplayer "${MFILE}"
fi
if test ${LOG} = "YES";then
echo "start " > a
echo "${STIME}" > b
cat a b > c
rm a b
echo "end " > d
date > e
cat d e > f
rm -f d e
cat c f > oyasumi_log.txt
rm -f c f
fi
if test $1 = "-s";then
if test "${UNAME}" = "Darwin";then
shutdown -h now
elif test "${UNAME}" = "Linux";then
poweroff
elif test "${UNAME}" = "FreeBSD";then
shutdown -p now
else
echo "Unknow OS"
echo "Missing Shutdown "
exit 1
fi
elif test $1 = "-r";then
reboot
elif test $1 = "-c";then
echo "COUND END.EXIT THIS SCRIPT"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment