Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 using ls -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:

  1. In this comparison the if condition is not correctly working.
  2. Always it is printing the else part only.
share|improve this question
3  
As has been mentioned often on SX: do not parse the output of ls –  Anthon May 24 '13 at 5:11
    
what platform are you using that doesn't have stat? if it's AIX, check if you have istat. –  Tim Kennedy May 24 '13 at 5:18
    
Hi 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 –  user1984839 May 24 '13 at 7:46
add comment

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.