Count Down

[root@station78 ~]# cat counting_down.sh
#!/bin/sh
#
#@@@author: Majesty
#
#set -x
#a shell for count down
#modify the values of time arbitrarily
time="01:00:10"

#spilt minute and second , and convert them.
echo $time > /tmp/tmptime.txt
htime=`cat /tmp/tmptime.txt | awk -F: '{print $1}'`
case $htime in
00) htime=0;;
01) htime=1;;
02) htime=2;;
03) htime=3;;
04) htime=4;;
05) htime=5;;
06) htime=6;;
07) htime=7;;
08) htime=8;;
09) htime=9;;
*);;
esac

mtime=`cat /tmp/tmptime.txt | awk -F: '{print $2}'`
case $mtime in
00) mtime=0;;
01) mtime=1;;
02) mtime=2;;
03) mtime=3;;
04) mtime=4;;
05) mtime=5;;
06) mtime=6;;
07) mtime=7;;
08) mtime=8;;
09) mtime=9;;
*);;
esac

stime=`cat /tmp/tmptime.txt | awk -F: '{print $3}'`
case $stime in
00) stime=0;;
01) stime=1;;
02) stime=2;;
03) stime=3;;
04) stime=4;;
05) stime=5;;
06) stime=6;;
07) stime=7;;
08) stime=8;;
09) stime=9;;
*);;
esac


#set -x
#change all time to seconds
ctime=`echo " $htime*3600 + $mtime*60 + $stime " | bc`

#begin counting down
while [ $ctime -gt 0 ]
do
 sleep 1
 ctime=$((ctime-1))

clear  #clear screen.

ohtime=`expr $ctime / 3600`  #calcuate the hours
case $ohtime in
0) ohtime=00;;
1) ohtime=01;;
2) ohtime=02;;
3) ohtime=03;;
4) ohtime=04;;
5) ohtime=05;;
6) ohtime=06;;
7) ohtime=07;;
8) ohtime=08;;
9) ohtime=09;;
*);;
esac

#set -x
omtimetmp=`echo "$ctime - $ohtime * 3600" | bc` #calculate the minutes
omtime=`expr $omtimetmp / 60`
case $omtime in
0) omtime=00;;
1) omtime=01;;
2) omtime=02;;
3) omtime=03;;
4) omtime=04;;
5) omtime=05;;
6) omtime=06;;
7) omtime=07;;
8) omtime=08;;
9) omtime=09;;
*);;
esac

ostime=`expr $ctime % 60` #cut the remainder. calculate the seconds
case $ostime in
0) ostime=00;;
1) ostime=01;;
2) ostime=02;;
3) ostime=03;;
4) ostime=04;;
5) ostime=05;;
6) ostime=06;;
7) ostime=07;;
8) ostime=08;;
9) ostime=09;;
*);;
esac

echo "$ohtime:$omtime:$ostime"

done





@@@test  
[root@station78 ~]# ./counting_down.sh
++ echo '3608 - 01 * 3600'
++ bc
+ omtimetmp=8
++ expr 8 / 60
+ omtime=0
+ case $omtime in
+ omtime=00
++ expr 3608 % 60
+ ostime=8
+ case $ostime in
+ ostime=08
+ echo 01:00:08
01:00:08
+ '[' 3608 -gt 0 ']'
+ sleep 1



你可能感兴趣的:(count,down)