#!/bin/sh
# Timer2 - 07.12.2009
DEPENDENCIES="grep sed mplayer"
SOUND="/mnt/data/Sons/alarme.wav"
SCRIPT=`basename "$0"`
E_BADSWITCH="10 You need to use either \"-a\" or \"-c\" switch as first argument"
E_MISSINGDEPENDENCY="11 Missing dependency: "
E_NOTIME="12 No time specified"
E_NODATEIFCOUNTDOWN="13 Date must not be specified in countdown mode"
E_NOSOUND="14 No $SOUND sound file found"
E_NEGATIVETIMEOUT="15 Timeout is anterior to current time with specified date and/or time"
help()
{
echo -e "\033[1mUsage :\033[0m $SCRIPT {-a|c} [YYYY-MM-DD] HH:MM[:SS]"
echo
echo -e "\033[1mSwitches :\033[0m -a : alarm mode"
echo " -c : countdown mode"
echo "You have one and only one mandatory switch to use between these two."
echo
echo -e "\033[1mParameters :\033[0m YYYY : year (4 numbers; i.e.: 2001)"
echo " MM : month (2 numbers, 01=January, 12=December)"
echo " DD : day of the month (2 numbers; beginning with 01)"
echo " HH : hours (2 numbers; i.e.: 00=midnight)"
echo " MM : minutes (2 numbers)"
echo " SS : seconds (2 numbers)"
echo "The whole group \"YYYY-MM-DD\" and the other symbol \"SS\" are"
echo "both facultative. The \"YYYY-MM-DD\" group must not be used in"
echo "countdown mode (only hours, minutes & secondes)."
}
echo_error()
{
echo -n "Error ! " >&2
echo `echo "$*" | sed 's/^[0-9][0-9] //'` >&2
echo "Type \"$SCRIPT -h\" or \"$SCRIPT --help\" for help screen"
exit `echo "$*" | sed 's/^\([0-9][0-9]\).*$/\1/'`
}
[ "$1" = "" -o "$1" = "-h" -o "$1" = "--help" ] && help && exit 0
for dep in $DEPENDENCIES; do
which "$dep" 1>/dev/null 2>&1; [ $? -ne 0 ] && echo_error "$E_MISSINGDEPENDENCY $dep"
done
[ ! -f "$SOUND" ] && echo_error "$E_NOSOUND"
[ "$1" != "-a" -a "$1" != "-c" ] && echo_error "$E_BADSWITCH"
[ "$2" = "" ] && echo_error "$E_NOTIME"
if [ "$1" = "-c" ]; then
shift
echo "$*" | grep "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" 1>/dev/null 2>&1; [ $? -eq 0 ] && echo_error "$E_NODATEIFCOUNTDOWN"
echo "$*" | grep -E "^[0-9][0-9]:[0-9][0-9](:[0-9][0-9])?$" 1>/dev/null 2>&1; [ $? -ne 0 ] && echo_error "$E_NOTIME"
countdown_time=`echo "$*" | sed 's/0\([1-9]\)/\1/g'`
hours=`echo "$countdown_time" | cut -d ":" -f 1`
minutes=`echo "$countdown_time" | cut -d ":" -f 2`
seconds=`echo "$countdown_time" | cut -d ":" -f 3`
[ "$seconds" = "" ] && seconds=0
echo -n "Countdown"
delay_seconds=$(( ($hours * 60 + $minutes) * 60 + $seconds ))
elif [ "$1" = "-a" ]; then
shift
alarm_seconds=`date -d "$*" "+%s"`
now_seconds=`date "+%s"`
seconds=$(( ($alarm_seconds - $now_seconds) ))
minutes=0
hours=0
[ $seconds -lt 1 ] && echo_error "$E_NEGATIVETIMEOUT"
if [ $seconds -ge 60 ]; then
minutes=$(( $seconds / 60 ))
seconds=$(( $seconds % 60 ))
if [ $minutes -ge 60 ]; then
hours=$(( $minutes / 60 ))
minutes=$(( ($hours % 60) + $minutes ))
fi
fi
echo -n "Alarm"
delay_seconds=$(( ((($hours * 60) + $minutes) * 60) + $seconds ))
fi
echo " starting: timeout in $hours hours $minutes minutes $seconds seconds"
sleep $delay_seconds && mplayer "$SOUND"
exit 0
#!/bin/sh
#scalars
SCRIPT=`basename "$0"`
HOURS=0
MINUTES=0
SECONDS=0
BEEP_LAST='false'
BEEP_DURATION=5 #seconds
BOX='false'
SOUND="/media/Elements/data/Sons/alarme.mp3"
#functions
help() {
cat <<EOT<br />Usage : $SCRIPT [--beep] hours minutes seconds
$SCRIPT --box
EOT
}
leadzero() {
[ $1 -lt 10 ] && echo '0'$1 || echo $1
}
#check script parameters
[ "$1" = "" -o "$1" = "-h" -o "$1" = "--help" ] && help && exit 0
while [ $# -ne 0 ]; do
case $1 in
--beep )
BEEP_LAST="true"
shift
;;
--box )
BOX="true"
shift
;;
*)
break
;;
esac
done
if [ "$BOX" = "true" ]; then
TMP=$(mktemp)
dialog --inputbox 'Hours' 8 70 2>$TMP
[ $? -eq 0 ] && HOURS=$(cat $TMP)
dialog --inputbox 'Minutes' 8 70 2>$TMP
[ $? -eq 0 ] && MINUTES=$(cat $TMP)
dialog --inputbox 'Seconds' 8 70 2>$TMP
[ $? -eq 0 ] && SECONDS=$(cat $TMP)
[ -f $TMP ] && rm -f $TMP
else
HOURS=$1
MINUTES=$2
SECONDS=$3
fi
echo "$HOURS$MINUTES$SECONDS" | grep -q -E '^[0-9][0-9]*$'
[ $? -ne 0 ] && echo 'Error: parameters are not numbers' >&2 && exit 1
TOTAL_SECONDS=$(( HOURS * 3600 + MINUTES * 60 + SECONDS ))
[ $TOTAL_SECONDS -eq 0 ] && echo 'Error: total timer should be greater than zero' >&2 && exit 1
if [ "$BOX" = "true" ]; then
dialog --pause "Time remaining from $(leadzero $HOURS):$(leadzero $MINUTES):$(leadzero $SECONDS)" 8 70 "$TOTAL_SECONDS"
RET_VAL=$?
else
echo -en "\nRemaining :\n\n\t$(leadzero $HOURS):$(leadzero $MINUTES):$(leadzero $SECONDS)"
while [ $TOTAL_SECONDS -gt 0 ]; do
sleep 1
TOTAL_SECONDS=$((TOTAL_SECONDS - 1))
S=$(( TOTAL_SECONDS % 60 ))
TM=$(( (TOTAL_SECONDS - S) / 60 ))
M=$(( TM % 60 ))
H=$(( (TM - M) / 60 ))
COUNTDOWN="$(leadzero $H):$(leadzero $M):$(leadzero $S)"
echo -en "\r\t$COUNTDOWN"
[ "$BEEP_LAST" = "true" -a $TOTAL_SECONDS -le $BEEP_DURATION ] && echo -en "\a"
done
sleep 1
echo -e "\r\tTerminé !\n"
RET_VAL=0
fi
[ $RET_VAL -eq 0 ] && mplayer "$SOUND" 1>/dev/null 2>&1
exit 0
#!/bin/sh
xterm -title "Timer" -T "Timer" -geometry "70x8" -e "timer --box"
#!/bin/sh
SCRIPT=$(basename "$0")
TOMORROW="false"
BOX="false"
SOUND_COMMAND="mplayer /media/Elements/data/Sons/alarme.mp3 >/dev/null 2>&1"
help() {
cat <<EOT<br />Usage : $SCRIPT [options] HH MM SS
Options :
--tomorrow | -t : specify that the given time concerns the
next day; useful when you are late in the
day and wanting to set an alarm in a few
hours, past midnight
Parameters :
'HH', 'MM' and 'SS' *must* be filled with their respective
values ; add a leading zero if the number is lesser than 10
(HH, MM and SS must have 2 numbers)
As an example : '$SCRIPT 14 05 03'
for an alarm at 14:05:03 in same day
EOT
}
short_help() {
echo "Usage : $SCRIPT [-t] hours minutes seconds"
echo "Type \"$SCRIPT -h\" or \"$SCRIPT --help\" for complete help screen"
}
err_exit() {
echo "Error: $* !" >&2
short_help
exit 1
}
[ $# -eq 0 ] && short_help && exit 0
while [ $# -ne 0 ]; do
case $1 in
-h|--help)
help
exit 0
;;
-t|--tomorrow)
TOMORROW="true"
shift
;;
--box)
BOX="true"
TMP=$(mktemp)
dialog --max-input 8 --inputbox "Alarm time (input format: HH MM SS)\n(current time is : $(date +%H:%M:%S)" 8 40 2>$TMP
A_TIMESTRING=$(cat $TMP)
[ -f $TMP ] && rm -f $TMP
echo "$A_TIMESTRING" | grep -q -E '^[0-9][0-9] [0-9][0-9] [0-9][0-9]$'
[ $? -ne 0 ] && dialog --msgbox "Error: \"$A_TIMESTRING\" is a wrong time format !" 8 70 && exit 1
A_HOURS=$(echo $A_TIMESTRING | cut -d " " -f 1)
A_MIN=$(echo $A_TIMESTRING | cut -d " " -f 2)
A_SEC=$(echo $A_TIMESTRING | cut -d " " -f 3)
break
;;
*)
[ $# -ne 3 ] && err_exit "3 parameters needed"
echo "$1$2$3" | grep -q -E '^[0-9][0-9][0-9][0-9][0-9][0-9]$'
[ $? -ne 0 ] && err_exit "all time parameters are not 2-digit numbers"
A_HOURS=$1
A_MIN=$2
A_SEC=$3
break
;;
esac
done
A_TIME=$(date -d $A_HOURS:$A_MIN:$A_SEC +%s)
[ "$TOMORROW" = "true" ] && A_TIME=$(( A_TIME + 24 * 3600 ))
N_TIME=$(date +%s)
[ $A_TIME -le $N_TIME ] && err_exit "Alarm time cannot be anterior or same as current time"
D_TIME=$(( A_TIME - N_TIME ))
#D_SEC=$(( D_TIME % 60 ))
#D_MIN=$(( ((D_TIME - D_SEC) / 60) % 60 ))
#D_HOURS=$(( (D_TIME - D_MIN - D_SEC) / 3600 ))
if [ "$BOX" = "false" ]; then
echo -e "\n\t\033[1mAlarm time\033[0m $A_HOURS:$A_MIN:$A_SEC"
while [ $D_TIME -ne 0 ]; do
echo -en "\t\033[1mCurrent time\033[0m $(date +%H:%M:%S)"
sleep 1
D_TIME=$(( D_TIME - 1 ))
echo -en "\b\r"
done
echo -e "\t\033[1;33mTerminé !\033[0;37m \n"
eval "$SOUND_COMMAND"
else
while [ $D_TIME -ne 0 ]; do
dialog --clear --timeout 1 --backtitle "Alarm" --msgbox "Alarm set at : $A_HOURS:$A_MIN:$A_SEC\nCurrent time is : $(date +%H:%M:%S)" 8 40
D_TIME=$(( D_TIME - 1 ))
done
eval "$SOUND_COMMAND"
dialog --clear --msgbox "Time is out ! Alarm has finished" 8 40
fi
#!/bin/sh
xterm -title "Alarm" -T "Alarm" -geometry "40x8" -e "alarm --box"
It looks like you're new here. If you want to get involved, click one of these buttons!