Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Compare current time to timestamp on a file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-01-2010
cillmor cillmor is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 11 October 2016, 3:27 PM EDT
Posts: 15
Thanks: 5
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 Unix or Linux Image
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 1 February 2016, 9:47 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 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 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
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 09:05 AM.