藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月26日 11:18


請教一個根據當前時間求4天前的日期的shell腳本

在unix下,根據當前時間,得到4天前的那個day,如:今天27號,則4天前的值為23號
要考慮到大小月,閏年等各種情況,自己寫了一個,太複雜,想找個比較短小的腳本,多謝,急用!!

在線等!!

請教一個根據當前時間求4天前的日期的shell腳本

linux下可以這樣:
date +%d -d'-4 day'
unix下你可以在論壇的精華區裡找 :mrgreen:

請教一個根據當前時間求4天前的日期的shell腳本

#date -d '-4 day' +%Y-%m-%d

2005-06-23

請教一個根據當前時間求4天前的日期的shell腳本

上面二個好像我試了都不行也:

bash-2.05$ date -d '-4 day' +%Y-%m-%d
date: illegal option -- d
date: illegal option -- 4
date: illegal option --
date: illegal option -- d
date: invalid argument -- y
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
語法不對?


我是在

2)
bash-2.05$ date +%d -d'-4 day'
27
bash-2.05$ date
Mon Jun 27 15:58:45 CST 2005
還是當前的日期?

我的環境是solaris9

請教一個根據當前時間求4天前的日期的shell腳本

-->
你的什麼solas有cal這個命令嗎

請教一個根據當前時間求4天前的日期的shell腳本

cal當然有
bash-2.05$ cal
   June 2005
S  M Tu  W Th  F  S
          1  2  3  4
5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

請教一個根據當前時間求4天前的日期的shell腳本

方法有點傻, 沒作輸入校驗 :m01:


#!/bin/bash
echo "please enter the date with the format yyyymmdd"
echo -n "the date is: "
read mydate
year=${mydate:0:4}
month=${mydate:4:2}
day=${mydate:6:2}
yearflag=0
monthflag=0

year=$(($year % 4))
if [ $year -eq 0 ]
then
        yearflag=1
fi

case $month in
02|04|06|08|09|11|01)
        monthflag=0
        ;;
05|07|10|12)
        monthflag=1
        ;;
03)
        if [ $yearflag -eq 0 ]
        then
                monthflag=3
        else
                monthflag=2
        fi
        ;;
esac
echo $monthflag
result=$((($day-4+31-$monthflag)%31))
echo "the day before you enter is : $result"

請教一個根據當前時間求4天前的日期的shell腳本

好像沒有考慮到閏年吧?
閏年需要year mod 400==0 或year mod 4=0 & year mod 100<>0

請教一個根據當前時間求4天前的日期的shell腳本

我給mod4了,暈,不過我好像也高錯樓主的意思了,沒把年和月算出

請教一個根據當前時間求4天前的日期的shell腳本

我給mod4了,暈,不過我好像也高錯樓主的意思了,沒把年和月算出

請教一個根據當前時間求4天前的日期的shell腳本

#計算某日前若干天日期
#date=YYYYMMDD
#Usage:command date day
get_before_days()
{
str=$1
days=$2
yy=`echo $str|cut -c 1-4`
mm=`echo $str|cut -c 5-6`
dd=`echo $str|cut -c 7-8`
sav_dd=$days
days=`expr $days - $dd`
while [ $days -ge 0 ]
do
mm=`expr $mm - 1`
[ $mm -eq 0 ] && mm=12 && yy=`expr $yy - 1`
aaa=`cal $mm $yy`
bbb=`echo $aaa|awk '{print $NF}'`
days=`expr $days - $bbb`
done
dd=`expr 0 - $days`
expr $dd : "^.$" > /dev/null && dd=0$dd
expr $mm : "^.$" > /dev/null && mm=0$mm
echo $yy$mm$dd
}

請教一個根據當前時間求4天前的日期的shell腳本

expr $dd : "^.$" > /dev/null && dd=0$dd
expr $mm : "^.$" > /dev/null && mm=0$mm
這兩行是什麼意思?



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com