这个实例主要涉及到的知识点有
- 输入输出语句
- date命令的使用
- 正则表达式过滤 输入参数
- 基本数学运算
1 read -p "please input your birthday 'mm-dd' : " date1
2 echo "today is:"
3 date +%m-%d
4 date_d=$(echo ${date1} |grep '[0-9]\{2\}-[0-9]\{2\}')
5 #echo "$date_d
6 if [ "${date_d}" == "" ]; then
7 echo "you input the wrong date formate..."
8 exit 1
9 fi
10 date3=$(date +%Y)"-"${date1}
11 #echo "${date3}"
12 #start caculate the time
13 declare -i date_dem=$(date --date="${date3}" +%s)
14 #echo "${date_dem}"
15 declare -i date_now=$(date +%s)
16 #echo "${date_now}"
17 declare -i date_total_s=$((${date_dem}-${date_now}))
18 #echo "${date_total_s}"
19 declare -i date_d=$((${date_total_s}/60/60/24))
20 #echo "${date_d}"
21 if [ "${date_total_s}" -lt "0" ]; then
22 echo "your birthday had been missed before: "$((-1*${date_d}))" ago"
23 else
24 declare -i date_h=$(($((${date_total_s}-${date_d}*60*60*24))/60/60))
25 echo "your birthday will comming after ${date_d} days and ${date_h} hours "
26 fi
比如
date_d=$(echo ${date1} |grep '[0-9]\{2\}-[0-9]\{2\}')
这里要求date1的输入格式为 “mm-dd” 如12-12,来表示你输入的生日日期,这里要注意的是“{}”要加转义
然后就是基本的运算
17 declare -i date_total_s=$((${date_dem}-${date_now}))
18 #echo "${date_total_s}"
19 declare -i date_d=$((${date_total_s}/60/60/24))
20 #echo "${date_d}"
这里可以进行连除操作