Compare current time to timestamp on a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Compare current time to timestamp on a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-01-2010
cillmor cillmor is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 9 February 2014, 3:24 PM EST
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
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


Code:
line 11: Apr 1: command not found error is returned



Code:
#!/bin/sh

log="DateLog"

Current_Date=`date +%b%e`
Filepmdate=`ls -l /file.txt | awk '{print $6,$7}'`

printf "$Current_Date \n"
printf "$Filepmdate \n"

if ( "$Current_Date" = "$Filepmdate" ) ; then
	echo "Dates are the same" > /$log
 	exit 0
else
	echo "Dates are Not the same" > /$log
	exit 0
fi

any ideas ?

---------- Post updated at 11:26 AM ---------- Previous update was at 11:22 AM ----------

ok saw my error should used square brackets [] instead of ()


Code:
log="DateLog"

Current_Date=`date +%b%e`
Filepmdate=`ls -l /message.txt | awk '{print $6,$7}'`

printf "$Current_Date \n"
printf "$Filepmdate \n"

if [ "$Current_Date" = "$Filepmdate" ] ; then
	echo "Dates are the same" > /$log
 	exit 0
else
	echo "Dates are Not the same" > /$log
	exit 0
fi


Last edited by pludi; 04-01-2010 at 06:39 AM.. Reason: code tags, please...
Sponsored Links
    #2  
Old 04-01-2010
frans's Avatar
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 4 December 2013, 9:39 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 53 Times in 52 Posts
look at 'man date' if your date supports the -r option (retrieve from file).
The syntax would be very simple:

Code:
log="DateLog"
DATEC="$(date +%b%e)"
DATEF="$(date -r /message.txt +%b%e)"
echo "Current date : $DATEC"
echo "File date    : $DATEF"
if [ "$DATEC" = "$DATEF" ]
then    echo "Dates are the same" > /$log
else     echo "Dates are Not the same" > /$log
fi
exit 0

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare file timestamp with current date. Diff must be 1 hour. taipan Shell Programming and Scripting 3 12-28-2009 02:40 AM
compare file modification date/time to current date/time Bill Ma Shell Programming and Scripting 5 08-27-2009 12:02 PM
how to grep and compare timestamp in a file with the current date achu AIX 1 05-25-2009 09:05 AM
how to grep and compare timestamp in a file with the current date achu AIX 1 05-25-2009 08:20 AM
How to compare the mtime of a file with the current time? Krsh Shell Programming and Scripting 2 01-19-2009 02:02 AM



All times are GMT -4. The time now is 01:01 AM.