#!/bin/bash
echo "This program will try to calculate:"echo "how many days before your demobilization date..."
read -p "please input your demobilization date (YYYYMMDD ex>20140909): " date2
date_d=$(echo $date2 | grep '[0-9]\{8\}')
if [ "$date_d" == "" ]; then
echo "You input the wrong date format..."
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 had been demobilization before :"$((-1*$date_d))" ago"
else
declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
echo "You will demobilize after $date_d days and $date_h hours"
fi
这个小程序(摘自鸟哥的一本书),自己在linux下实践时总是报错,不能正常运行。
仔细检查后发现有几个细节的地方没有注意到,特记录于此。
1.文中出现的都是反单引号(`)
2.date和“+”号之间必须跟空格