#!/bin/sh COMMAND=$1 case "$COMMAND" in "start") if [ "$#" -ne 2 ]; then echo "Usage: listimer start "; exit 1; fi DURATION_MIN=$2 CURRENT_TIME=$(date +%s) echo "$CURRENT_TIME\n\n$DURATION_MIN" > /tmp/listimer ;; "restart") if [ "$#" -ne 1 ]; then echo "Usage: listimer restart"; exit 1; fi CURRENT_TIME=$(date +%s) sed -i "1s/.*/$CURRENT_TIME/" /tmp/listimer ;; "stop") if [ "$#" -ne 1 ]; then echo "Usage: listimer stop"; exit 1; fi CURRENT_TIME=$(date +%s) rm /tmp/listimer ;; "query") if [ ! -f /tmp/listimer ]; then echo "No timer" exit 0; fi START_TIME=$(sed -n '1p' < /tmp/listimer) DURATION_MIN=$(sed -n '3p' < /tmp/listimer) DURATION_SEC=$DURATION_MIN*60 CURRENT_TIME=$(date +%s) ELAPSED_TIME_SEC=$(($CURRENT_TIME-$START_TIME)) ELAPSED_TIME_MIN=$(($ELAPSED_TIME_SEC/60)) REMAINING_TIME_MIN=$(($DURATION_MIN-$ELAPSED_TIME_MIN)) if [ "$REMAINING_TIME_MIN" -lt "$(($DURATION_MIN / 2))" ]; then echo "$REMAINING_TIME_MIN minutes" elif [ "$REMAINING_TIME_MIN" -lt "0" ]; then echo "TIME OUT!" else echo "$REMAINING_TIME_MIN minutes" fi ;; *) echo "Unexpected command." esac