This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / I came across this post in chinasmile. Nobody has answered his question yet. I couldn't figure out a way to do it with shell scripts, although
-numnum(numnum);
2001-7-10
{851}
(#127036@0)
-
I can do it easily with FSF's date, but I can't figure out how to do it using *BSD's dateSomething like:
#!/bin/sh
s1=`date -d $1 +%s`
s2=`date -d $2 +%s`
day=$((($s2 - $s1)/3600/24))
echo "Days: " $day
-dennis2(Dennis);
2001-7-10
{124}
(#127064@0)
-
Oops! Only works from 1902 to 2038 (with some modification)! So using date is not a good idea...But how to parse string using shell script? Perl would be a lot easier but usually it is not considered a shell...
-dennis2(Dennis);
2001-7-10
(#127200@0)
-
Thanks, actually you can use awk to parser the date. Where can you get FSF's date utility? I never heard of it. And should you use expr in the second last line when you try to evaluate ($s1-s2)/3600/24?
-numnum(numnum);
2001-7-10
(#127218@0)
-
It came with every Linux distro (I think). I mean date(1) in Linux (which is from FSF) is different from date(1) in *BSD. And I think $(( ... )) will do the same thing with expr? I'm using bash anyway.Actually my script has problems, it overflows much earlier than it should be. Here's an updated one:
#!/bin/sh
s1=`date -d $1 +%s`
s2=`date -d $2 +%s`
day1=$(($s1/3600/24))
day2=$(($s2/3600/24))
echo "Days: " $(($day2 - $day1))
But like I said it can only calculate from 1902 - 2038. I'll try to come up one with awk (why I didn't think of that? stupid me) if I have time. But I have to deal with leap year myself I guess.
-dennis2(Dennis);
2001-7-10
{440}
(#127225@0)
-
Okay, I got it. the $(()) works much better than expr. it looks I am outdated. bash is a gnu thing, FSF date is a gnu thing too.I work on Solaris platform mostly. It looks like GNU stuffs are always better than conventional Unix stuffs. BTW, I'am very happy to meet a guy like you, hope we can keep on. Although I am not doing script programming anymore, the script languages are always my favorite, they are concise and fun.
-numnum(numnum);
2001-7-10
{298}
(#127250@0)
-
Here is an algorithm.
-old(HH);
2001-7-15
{1221}
(#132958@0)