•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 428,224 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,248 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 20619 | Replies: 3
![]() |
•
•
Join Date: Oct 2005
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
I have created a convaluted and hacked script here for comparing unix dates.
Can someone help me use calc or date w/ julian param or something to look back a week, a month or even a year and compare dates or add/subtract the dates somehow in a shell script?
Hope this makes sense!
Thanks for any help or resources.
My script( I apologize if this example doesn't work right, I have been changing it and trying to make sense of it!):
#!/bin/bash
#Day of the week
DAT="`date +%w`"
#Current date
CDAT=`date +%Y-%m-%d | sed 's/-//g' | cut -c5-8 | sed 's/^0//'`
# Get DATETIME in this format: "2005-10-16 20:03:36" from IBM TSM Database - DB2
dsmadmc -id=admin -password=admin select 'DATE(LAST_WRITE_DATE)','TIME(LAST_WRITE_DATE)' from volumes where STGPOOL_NAME="'LTOPOOL2'" | grep -v -e IBM -e Copyright -e Tiv -e Comm -e Sess -e Serv -e ANS -e Vol -e VOL -e "---" -e LAST -e Unnamed | /bin/sed '/ *#/d; /^ *$/d' > $DATETIME
cat $DATETIME | awk '{ print $1; }' > $DBDATE
cat $DBDATE | cut -c6-10 | sed 's/-//g' | sed 's/^0//' | sort -u | uniq > $STRIPDATE
cat $DBDATE | cut -c6-10 | uniq > $DAYMONTH
for NUM in `cat $STRIPDATE`
do
if [[ `expr $CDAT - $NUM` = 22 || `expr $CDAT - $NUM` > 90 ]]
then
echo $NUM | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{2\}\)/\1-\2/;ta' >> $GETDAYMONTH
else
:
fi
done
cat $GETDAYMONTH | uniq >> $CALCDAYMONTH
for CNUM in `cat CALCDAYMONTH`
do
grep "$CNUM" $DATETIME | sed 's/ //' >> $ARCHHOLD
done
cat $ARCHHOLD | sed -e 's/./& /10' > $FINAL_ARCH
done
exit 0;
I have created a convaluted and hacked script here for comparing unix dates.
Can someone help me use calc or date w/ julian param or something to look back a week, a month or even a year and compare dates or add/subtract the dates somehow in a shell script?
Hope this makes sense!
Thanks for any help or resources.
My script( I apologize if this example doesn't work right, I have been changing it and trying to make sense of it!):
#!/bin/bash
#Day of the week
DAT="`date +%w`"
#Current date
CDAT=`date +%Y-%m-%d | sed 's/-//g' | cut -c5-8 | sed 's/^0//'`
# Get DATETIME in this format: "2005-10-16 20:03:36" from IBM TSM Database - DB2
dsmadmc -id=admin -password=admin select 'DATE(LAST_WRITE_DATE)','TIME(LAST_WRITE_DATE)' from volumes where STGPOOL_NAME="'LTOPOOL2'" | grep -v -e IBM -e Copyright -e Tiv -e Comm -e Sess -e Serv -e ANS -e Vol -e VOL -e "---" -e LAST -e Unnamed | /bin/sed '/ *#/d; /^ *$/d' > $DATETIME
cat $DATETIME | awk '{ print $1; }' > $DBDATE
cat $DBDATE | cut -c6-10 | sed 's/-//g' | sed 's/^0//' | sort -u | uniq > $STRIPDATE
cat $DBDATE | cut -c6-10 | uniq > $DAYMONTH
for NUM in `cat $STRIPDATE`
do
if [[ `expr $CDAT - $NUM` = 22 || `expr $CDAT - $NUM` > 90 ]]
then
echo $NUM | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{2\}\)/\1-\2/;ta' >> $GETDAYMONTH
else
:
fi
done
cat $GETDAYMONTH | uniq >> $CALCDAYMONTH
for CNUM in `cat CALCDAYMONTH`
do
grep "$CNUM" $DATETIME | sed 's/ //' >> $ARCHHOLD
done
cat $ARCHHOLD | sed -e 's/./& /10' > $FINAL_ARCH
done
exit 0;
•
•
Join Date: May 2004
Posts: 177
Reputation:
Rep Power: 5
Solved Threads: 9
Date arithmetic in shell is a pain. This link gives you several good scripts that do date compares and date arithmetic:
http://www.unix.com/showthread.php?t=13785
http://www.unix.com/showthread.php?t=13785
•
•
Join Date: Oct 2005
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by jim mcnamara
Date arithmetic in shell is a pain. This link gives you several good scripts that do date compares and date arithmetic:
http://www.unix.com/showthread.php?t=13785
Great link, thanks Jim!
•
•
Join Date: Jun 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
i'm trying to create a script that compares today's date with the date of some files that have been ftp'd over earlier in the day. basically, i want to ensure that today's date and the file's date match.
before i head off down this path, i just want to make sure that i'm not reinventing the wheel.
thanks,
doug
before i head off down this path, i just want to make sure that i'm not reinventing the wheel.
thanks,
doug
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- How to Compare two SYSTEMDATE? (C++)
- Compare two Text File (Perl)
- help with PHP script to return dates (PHP)
- How to compare string value (Java)
Other Threads in the Shell Scripting Forum
- Previous Thread: Reading through lines of text file sequencially
- Next Thread: shell script fo backup archiving


Linear Mode