Compare file timestamp with current date. Diff must be 1 hour.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare file timestamp with current date. Diff must be 1 hour.
# 1  
Old 12-23-2009
Compare file timestamp with current date. Diff must be 1 hour.

Hello,

I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed.

The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of the first file, and then compare it with the second file. Once the difference is 50 minutes or more, then the files have to be compared based on their content and execute the lines that exist in both files.

I tried to use the "find -mmin" command, but find doesn't know the mmin parameter (HP-UX beryl B.11.11 U 9000/800).

I've found a lot of information on this forum already, so I hope someone can help me out! Thanks!

Code:
 
export SQL_DIR=/opt/staffware/server/sql
export LOG_DIR=/opt/staffware/server/logs
export BIN_DIR=/opt/staffware/server/bin
export ORA_BIN_DIR=/opt/oracle/ora817/bin
export ORACLE_HOME=/opt/oracle/ora817
 
SID=
USER=
PASSWD=
 
OUTPUTFILE1=sw_locked_cases_check_1.txt
OUTPUTFILE2=sw_locked_cases_check_2.txt
SORTEDOUTPUTFILE1=sw_locked_cases_check_1s.txt
SORTEDOUTPUTFILE2=sw_locked_cases_check_2s.txt
FINALOUTPUTFILE=sw_locked_cases_checked.txt
 
# Teller in minuten
COUNTDOWN=50
# --------------------------------------------------------------------
 
# Controle of de juiste persoon is ingelogd
echo "Script gestart op `date`"
echo "Je bent ingelogd als `whoami`";
if [ `whoami` != "swadmin" ]; then
echo "Je dient als swadmin ingelogd te zijn om dit script te starten."
exit
fi
 
# Maak outputfiles aan
cat <<EOF >$BIN_DIR/$OUTPUTFILE1
EOF
cat <<EOF >$BIN_DIR/$OUTPUTFILE2
EOF
cat <<EOF >$BIN_DIR/$FINALOUTPUTFILE
EOF
 
# voer SQL query uit - check 1
$ORA_BIN_DIR/sqlplus -s $USER/$PASSWD@$SID <<!>$BIN_DIR/$OUTPUTFILE1
whenever sqlerror exit 2
set heading off
set pages 0
set feedback off
set verify off
SELECT 'swutil UNLOCKMAIL ' || o_queuename || ' ' || o_reqid || ':sw_nimbus_prd01'
FROM staffo
WHERE o_locker IS NOT NULL
ORDER BY O_QUEUENAME
/
!
 
# gebruik countdown variabele om de lengte van de sleep te bepalen
echo
while [[ ${COUNTDOWN} -ge 0 ]]
do
echo "Nog" ${COUNTDOWN} "minuten totdat het script verder gaat."
sleep 60
COUNTDOWN=$(( ${COUNTDOWN} - 1 ))
done
 
# voer SQL query uit - check 2
echo
$ORA_BIN_DIR/sqlplus -s $USER/$PASSWD@$SID <<!>$BIN_DIR/$OUTPUTFILE2
whenever sqlerror exit 2
set heading off
set pages 0
set feedback off
set verify off
SELECT 'swutil UNLOCKMAIL ' || o_queuename || ' ' || o_reqid || ':sw_nimbus_prd01'
FROM staffo
WHERE o_locker IS NOT NULL
ORDER BY O_QUEUENAME
/
!
 
# voer een compare uit
sort $BIN_DIR/$OUTPUTFILE1 > $BIN_DIR/$SORTEDOUTPUTFILE1
sort $BIN_DIR/$OUTPUTFILE2 > $BIN_DIR/$SORTEDOUTPUTFILE2
comm -12 $BIN_DIR/$SORTEDOUTPUTFILE1 $BIN_DIR/$SORTEDOUTPUTFILE2 > $FINALOUTPUTFILE
 
# voer iedere regel in de finaloutputfile uit
while read LINEFROMFILE
do
print `$BIN_DIR/$LINEFROMFILE` "$LINEFROMFILE"
# echo $LINEFROMFILE
done < $BIN_DIR/$FINALOUTPUTFILE
 
echo
OF1=`ls -ltr $OUTPUTFILE1 | awk '{print $8}'` 
OF2=`ls -ltr $OUTPUTFILE2 | awk '{print $8}'` 
#OF2=`date +%R`
echo "Leeftijd controlebestand 1" $OF1
echo "Leeftijd controlebestand 2" $OF2
 
# verwijder de finaloutputfile
echo
echo "Tijdelijke bestanden worden verwijderd." 
rm -rf $BIN_DIR/$OUTPUTFILE1
rm -rf $BIN_DIR/$OUTPUTFILE2
rm -rf $BIN_DIR/$SORTEDOUTPUTFILE1
rm -rf $BIN_DIR/$SORTEDOUTPUTFILE2
rm -rf $BIN_DIR/$FINALOUTPUTFILE


Last edited by taipan; 12-24-2009 at 03:47 AM..
# 2  
Old 12-23-2009
Quote:
Originally Posted by taipan
Hello,

I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed.

The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of the first file, and then compare it with the second file. Once the difference is 50 minutes or more, then the files have to be compared based on their content and execute the lines that exist in both files.

I tried to use the "find -mmin" command, but find doesn't know the mmin parameter (HP-UX beryl B.11.11 U 9000/800).

I've found a lot of information on this forum already, so I hope someone can help me out! Thanks!

Code:
 
export SQL_DIR=/opt/staffware/server/sql
export LOG_DIR=/opt/staffware/server/logs
export BIN_DIR=/opt/staffware/server/bin
export ORA_BIN_DIR=/opt/oracle/ora817/bin
export ORACLE_HOME=/opt/oracle/ora817


Why are you exporting these variables?
Quote:
Code:
 
SID=
USER=
PASSWD=
 
OUTPUTFILE1=sw_locked_cases_check_1.txt
OUTPUTFILE2=sw_locked_cases_check_2.txt
SORTEDOUTPUTFILE1=sw_locked_cases_check_1s.txt
SORTEDOUTPUTFILE2=sw_locked_cases_check_2s.txt
FINALOUTPUTFILE=sw_locked_cases_checked.txt
 
# Teller in minuten
COUNTDOWN=50
# --------------------------------------------------------------------
 
# Controle of de juiste persoon is ingelogd
echo "Script gestart op `date`"
echo "Je bent ingelogd als `whoami`";
if [ `whoami` != "swadmin" ]; then
echo "Je dient als swadmin ingelogd te zijn om dit script te starten."
exit
fi
 
# Maak outputfiles aan
cat <<EOF >$BIN_DIR/$OUTPUTFILE1
EOF
cat <<EOF >$BIN_DIR/$OUTPUTFILE2
EOF
cat <<EOF >$BIN_DIR/$FINALOUTPUTFILE
EOF


There's no need for cat:

Code:
> "$BIN_DIR/$OUTPUTFILE1"
> "$BIN_DIR/$OUTPUTFILE2"
> "$BIN_DIR/$FINALOUTPUTFILE"

Quote:
Code:
# voer SQL query uit - check 1
$ORA_BIN_DIR/sqlplus -s $USER/$PASSWD@$SID <<!>$BIN_DIR/$OUTPUTFILE1
whenever sqlerror exit 2
set heading off
set pages 0
set feedback off
set verify off
SELECT 'swutil UNLOCKMAIL ' || o_queuename || ' ' || o_reqid || ':sw_nimbus_prd01'
FROM staffo
WHERE o_locker IS NOT NULL
ORDER BY O_QUEUENAME
/
!
 
# gebruik countdown variabele om de lengte van de sleep te bepalen
echo
while [[ ${COUNTDOWN} -ge 0 ]]
do
echo "Nog" ${COUNTDOWN} "minuten totdat het script verder gaat."
sleep 60
COUNTDOWN=$(( ${COUNTDOWN} - 1 ))
done
 
# voer SQL query uit - check 2
echo
$ORA_BIN_DIR/sqlplus -s $USER/$PASSWD@$SID <<!>$BIN_DIR/$OUTPUTFILE2
whenever sqlerror exit 2
set heading off
set pages 0
set feedback off
set verify off
SELECT 'swutil UNLOCKMAIL ' || o_queuename || ' ' || o_reqid || ':sw_nimbus_prd01'
FROM staffo
WHERE o_locker IS NOT NULL
ORDER BY O_QUEUENAME
/
!
 
# voer een compare uit
sort $BIN_DIR/$OUTPUTFILE1 > $BIN_DIR/$SORTEDOUTPUTFILE1
sort $BIN_DIR/$OUTPUTFILE2 > $BIN_DIR/$SORTEDOUTPUTFILE2
comm -12 $BIN_DIR/$SORTEDOUTPUTFILE1 $BIN_DIR/$SORTEDOUTPUTFILE2 > $FINALOUTPUTFILE
 
# voer iedere regel in de finaloutputfile uit
while read LINEFROMFILE
do
print `$BIN_DIR/$LINEFROMFILE` "$LINEFROMFILE"
# echo $LINEFROMFILE
done < $BIN_DIR/$FINALOUTPUTFILE
 
echo
OF1=`ls -ltr $OUTPUTFILE1 | awk '{print $8}'` 
OF2=`ls -ltr $OUTPUTFILE2 | awk '{print $8}'` 
#OF2=`date +%R`
echo "Leeftijd controlebestand 1" $OF1
echo "Leeftijd controlebestand 2" $OF2
 
# verwijder de finaloutputfile
echo
echo "Tijdelijke bestanden worden verwijderd." 
rm -rf $BIN_DIR/$OUTPUTFILE1
rm -rf $BIN_DIR/$OUTPUTFILE2
rm -rf $BIN_DIR/$SORTEDOUTPUTFILE1
rm -rf $BIN_DIR/$SORTEDOUTPUTFILE2
rm -rf $BIN_DIR/$FINALOUTPUTFILE


Why are you calling rm 5 times?

Code:
rm -rf "$BIN_DIR/$OUTPUTFILE1" "$BIN_DIR/$OUTPUTFILE2" "$BIN_DIR/$SORTEDOUTPUTFILE1" \
       "$BIN_DIR/$SORTEDOUTPUTFILE2" "$BIN_DIR/$FINALOUTPUTFILE"

Most of your script has nothing to do with the question you asked.

Code:
## difftime by Chris F.A. Johnson
## Created Fri Dec 29 03:09:25 EST 2006
## Copyright 2006 Chris F.A. Johnson
## This script is released under the terms of
## the GNU General Public Licence, Version 3

version=0.2

if [ $# -ne 2 ]
then
  cat <<-EOF

	   difftime - calculate sub-second difference between two times

	   USAGE: difftime MM:SS.Frac MM:SS.Frac

	EOF
  exit
fi

awk  -v begin="$1" -v end="$2" 'BEGIN {
   split ( begin, t1, ":" )
   split ( end, t2, ":" )
   d2 = 60 * t2[1] + t2[2]
   d1 = 60 * t1[1] + t1[2]
   diff = d2 - d1

   minutes = int(diff / 60)
   seconds = int(diff) % 60
   split ( diff, d, "." ) 
   printf "%1d:%02d.%d\n", minutes,  seconds,  d[2]
   exit 
}'


Last edited by DukeNuke2; 04-14-2010 at 11:05 AM..
# 3  
Old 12-24-2009
Hey,

Thanks for your reply. I know the script has some things in it which should be removed or changed, but I first would like to make it run fine. Once achieved, I will do a cleanup action. Thanks for the comments.

Also it was indeed not necessary to place the script within this post, but I thought it would be handy to help me out.

I think I can use your script very well, I`m going to test it right away!
# 4  
Old 12-28-2009
Works great! Tx!

p.s. would you please remove the username/password from the quoted lines. I accidentally posted them as well Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

Day/Hour diff between two date

Dear All I need to find out day diff between two dates. date -d or date -- day is not working in mine system. Currently i am using below code but it gives me wrong value while month change. IP: Date 1: 20150802 11:30:45 Date 2: 20150728 16:30:45 code used: awk '{t1=$2; t2=$4;... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

3. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

4. AIX

Change specific (not current) date to timestamp

Hello to all. I work at AIX system without perl installed and I am restricted user, so I am limited to bash. In script that I am writing, I have to read line from file and transform date that I found inside to Unix timestamp. Line in file look something like this: Tue Mar 29 06:59:00... (5 Replies)
Discussion started by: Hyperborejac
5 Replies

5. Shell Programming and Scripting

Shell Script to compare files, check current date and count

Hello - I have written the following basic shell script to count files, compare files and look for a particular strings in a file. Problem 1: How do I define more than 1 file location? #!/bin/bash #this is a test script FILES=$(ls /home/student/bin/dir1, home/student/bin/dir2)... (0 Replies)
Discussion started by: DallasT
0 Replies

6. Shell Programming and Scripting

Compare current time to timestamp on a file

I'm trying to compare 2 dates between current time and the timestamp on a file. The date format is mmdd Both return Apr 1 but when using if statement line 11: Apr 1: command not found error is returned #!/bin/sh log="DateLog" Current_Date=`date +%b%e` Filepmdate=`ls -l /file.txt |... (1 Reply)
Discussion started by: cillmor
1 Replies

7. UNIX for Advanced & Expert Users

In ksh find out the timestamp of current date?

Normally we can use %s to find out the time in second since 1970. But in my ksh, this format option is not available. Example- date +%s 1268103151 above script command won't work in ksh. Can you guys provide its equivalent ? (3 Replies)
Discussion started by: boy18nj
3 Replies

8. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the log file each line starts with timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file... (1 Reply)
Discussion started by: achu
1 Replies

9. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the logfile , lines contains timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file format.Please... (1 Reply)
Discussion started by: achu
1 Replies

10. Shell Programming and Scripting

Get date and time for past 1 hour from current date

Hi, I need to get the date and time for past 1 hour from the current date. Anyone know how to do so? Thanks (5 Replies)
Discussion started by: spch2o
5 Replies
Login or Register to Ask a Question