Think before you speak, read before you think.

shell 毕业倒计时

参考鸟哥linux私房菜p389

#!/bin/bash
#for use:
#author: jpuyy date  jpuyy.com stubusy@gmail.com
#modified by xx at date
#version:
#history:
#relate on:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "count down script"
echo "how many time to graduate"
read -p "please input your graduate time format like 20120620:" date2

date_d=$(echo $date2 | grep '[0-9]\{8\}')
if [ "$date_d" == "" ]; then
    echo "your input is wrong"
    exit 1
fi

declare -i date_dem=`date --date="$date2" +%s`
declare -i date_now=`date +%s`
declare -i date_total_s=$(($date_dem-$date_now))
declare -i date_d=$(($date_total_s/60/60/24))

if [ "$date_total_s" -lt "0" ]; then
    echo "you have graduated $((-1*$date_d)) days"
else
    declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
    echo "you left $date_d days and $date_h hours."
fi

执行

其实不光有倒计时功能,也可以看已发生的事件到现在多长时间了

收获:

打代码时候vim颜色提醒了标点符号,节省了很多力气

找码一定要打出来才知道掌握了多少,空想不可取,再少代码也不可复制。

用到知识:date 和 时间换算常识

root@www:/home/jpuyy/shellstudy# bash sh11.sh
count down script
how many time to graduate
please input your graduate time format like 20120620:20120620
you left 203 days and 11 hours.

离毕业不远了啊

 


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *