If condition to check one file newer than the other | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

If condition to check one file newer than the other

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-25-2013
Manueldo Manueldo is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 25 January 2013, 4:24 PM EST
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
If condition to check one file newer than the other

Dear All,

I'm new to unix scripting. I'm trying to write an utility script which has to check if one file is newer than another one. $3 $4 $5 $6 are files .txt. Help me please.


Code:
for i in $3 $4 $5 $6
do
 if ["$1" -nt i ]
then
echo "$1 is newer than $i"
else
echo "$i is newer than $1"
fi

Sponsored Links
    #2  
Old 01-25-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 August 2014, 5:59 PM EDT
Location: San Jose, CA, USA
Posts: 4,391
Thanks: 170
Thanked 1,482 Times in 1,256 Posts
Almost. Try:
Code:
for i in $3 $4 $5 $6
do
        if ["$1" -nt i ]
        then
                echo "$1 is newer than $i"
        else
                echo "$i is not older than $1"
        fi
done

Note NOT (a newer than b) is not the same as (b newer than a); a and b could be have the same timestamp.

Note that if you indent your code to show the depth of the expressions you're processing, a missing fi or done will be much more obvious.
Sponsored Links
    #3  
Old 01-25-2013
Manueldo Manueldo is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 25 January 2013, 4:24 PM EST
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
I did change, but it still doesn work. I modofied files, but still the same MESSAGE "file is not older other file"
    #4  
Old 01-25-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 August 2014, 5:59 PM EDT
Location: San Jose, CA, USA
Posts: 4,391
Thanks: 170
Thanked 1,482 Times in 1,256 Posts
And if you add the following command to your script as a line before your for loop:

Code:
printf '$1 is "%s"\n$2 is "%s"\n$3 is "%s"\n$4 is "%s"\n$5 is "%s"\n' "$1" "$2" "$3" "$4" "$5"

what output does it produce.
Sponsored Links
    #5  
Old 01-25-2013
Manueldo Manueldo is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 25 January 2013, 4:24 PM EST
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts

Code:
./mymk: 18: ./mymk: [avgs.txt -nt i]: not found
first.txt is not older than avgs.txt
./mymk: 18: ./mymk: [avgs.txt -nt i]: not found
second.txt is not older than avgs.txt
./mymk: 18: ./mymk: [avgs.txt -nt i]: not found
third.txt is not older than avgs.txt
./mymk: 18: ./mymk: [avgs.txt -nt i]: not found
avgs is not older than avgs.txt

Sponsored Links
    #6  
Old 01-25-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 20 August 2014, 6:24 AM EDT
Location: Aachen, Germany
Posts: 4,082
Thanks: 66
Thanked 969 Times in 919 Posts
What about correcting the syntax: replace if ["$1" -nt i ] by
Code:
  if [ "$1" -nt "$i" ]
      ^--- space ^--- dollar


Code:
file is not older than file~
file~ is not older than file~
file~ is newer than file1
file~ is newer than file2
file~ is newer than file3
file~ is newer than file4

Sponsored Links
    #7  
Old 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 August 2014, 7:56 AM EDT
Location: France
Posts: 318
Thanks: 19
Thanked 81 Times in 76 Posts
You're missing a [space] after '[' (edit) and the $ too (thanks RudiC^^)
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
Parse tab delimited file, check condition and delete row empyrean Shell Programming and Scripting 4 09-18-2012 02:13 PM
Condition to check whether configuration file is empty or not? anuragpgtgerman Shell Programming and Scripting 2 12-25-2011 11:46 AM
Check a condition in cronjob to execute a sh file shanth_chandra Shell Programming and Scripting 2 02-16-2010 02:08 AM
If condition to check one file newer than the other - first file name uncertain achilles5 Shell Programming and Scripting 9 07-10-2009 03:09 AM
script to check for a condition inside a file kiran1112 Shell Programming and Scripting 11 03-21-2007 09:38 AM



All times are GMT -4. The time now is 08:23 PM.