|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
doneNote 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
|
|||
|
|||
|
I did change, but it still doesn work. I modofied files, but still the same MESSAGE "file is not older other file"
![]() |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
What about correcting the syntax: replace
if ["$1" -nt i ] by Code:
if [ "$1" -nt "$i" ]
^--- space ^--- dollarCode:
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
|
|||
|
|||
|
You're missing a [space] after '[' (edit) and the $ too (thanks RudiC^^)
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|