Comparing file times in ksh
Ksh has an operator to compare the dates of two files (it's also available in ash, bash and zsh, but it isn't in POSIX). You don't need to call ls
or stat
or anything to read the dates: let the shell compare them.
for f in *.xml; do
if [[ $f -nt default.txt ]]; then …
done
Related videos on Youtube
user1984839
Updated on September 18, 2022Comments
-
user1984839 over 1 year
I have developed a script which will compare two dates.
#!/bin/ksh #################################################################### # #DECLARING ALL THE VALUES AND PATHS # #################################################################### LOG_FOLDER=/user/sba-appl/test/sscr/ramu TARGET_DIR=/user/sba-appl/test/sscr/ramu ################################################################### # #GETTING THE TIME STAMP OF THE FILE # #################################################################### cd $TARGET_DIR DEFAULT_DATE=`ls -l /$TARGET_DIR/default.txt| awk '{print $6,$7,$8}'` printf "$DEFAULT_DATE \n" for f in *.xml do echo "Processing $f" FILE_DATE=`ls -l /$TARGET_DIR/$f|awk '{print $6,$7,$8}'` printf "$FILE_DATE \n" echo "dafault filedate is $DEFAULT_DATE" if [ $FILE_DATE -gt $DEFAULT_DATE ] then echo "Call the Jar for the file $f" else echo "Do not call the Jar for the file $f" fi done
Here I'm facing two problems:
1. In this we have a default file and this file timestamp should be set to old time ie for eg 1990. I know this can be done with the help of
touch -t
with the timestamp.I am using a ksh and stat is not present here. So I'm using
ls -l
. But if i am usingls -l
command then it will list the year 1990 (because it will always list the year if the file is 6 months older). In this case my default_date value will have year.So I think my if condition is failing as year is coming here.
2. I changed the timestamp of the files including
default.txt
within 6 months and I checked now but still it is not executing the if part and it is executing the else part.I'm also getting unknown test operator. Can anyone tell me where it is going wrong. It will be great help if anyone can check this.
I am facing two problems here:
- In this comparison the if condition is not correctly working.
- Always it is printing the else part only.
-
Anthon almost 11 yearsAs has been mentioned often on SX: do not parse the output of ls
-
Tim Kennedy almost 11 yearswhat platform are you using that doesn't have
stat
? if it's AIX, check if you haveistat
. -
user1984839 almost 11 yearsHi Friends I found out a way instead of stat . I used the Perl command and using that command i converted that to yyyyddmmtime format. Based on this I subtratcted each file timestamp with the default file timestamp. IF the timestamp is less than zero then i am calling the jar. Thanks for your responses and have a nice day Thanks kannan