2014-01-07 130 views
0
start_time=`sed -e 's/\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1/' <<< "$line"` 
start_time_sec=`date -d "$start_time" +%s` 
now=`date +%s` 
pass_time=`$now - $start_time_sec` 
if [ $pass_time <=86400*60 ] 
then 
initial_time= $start_time 
initial_time_sec=`date -d "$initial_time" +%s` 
break 

網絡 /在這裏,我試圖用秒日期比較,但我想在天方面/如何使用shell腳本

回答

0
start_time=`echo $line | sed -e 's/^\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1/'` 
start_time_sec=`date -d "$start_time" +%s` 
now=`date +%s` 
pass_time=`expr $now - $start_time_sec` 
limit_time=$((60 * 60 * 24 * 60)) 
if [[ $pass_time -le $limit_time ]]; then                                           
    echo "in 60 days" 
fi 

更新比較當前日期與過去60天: 或你的想法:

start_time_date=`date -d "$start_time" +%s` 
past_date=`date +"%Y-%m-%d" -d "-60 day"` 
if [[ $past_date -le $start_time_date ]]; then 
    echo 'in 60 days' 
fi 
+0

'expr'會做什麼? – user3165241

+0

@ user3165241'expr'是一個shell命令,用於評估表達式。:) – atupal

+0

@ user3165241你也可以使用'pass_time = $((now-start_time_sec))' – atupal

1

ISO日期(YYYY-MM-DD),可以比較像字符串:

$ date +%Y-%m-%d 
2014-01-07 
$ date +%Y-%m-%d -d '-60 days' 
2013-11-08 
$ [[ "$(date +%Y-%m-%d -d '-60 days')" < "$(date +%Y-%m-%d)" ]] 
$ echo $? 
0