Something like this should work:

   sizeof () {
      ls -l $1 | sed 's/  */ /g' | cut -f5 -d' '
   }

   if [ `sizeof $1` -lt `sizeof $2` ]; then
       echo $1 is smaller than $2
   else
       echo $1 is the same or bigger than $2
   fi

In the above, the sizeof function returns the size extracted from the
results of an ls -l command. I am using cut -f5 with a blank as the
delimiter to get the fifth column which contains the file size.  The sed
command is used to remove multiple blanks.  Note that there are *two* blanks
as in:

     's/<blank><blank>*/<blank>/g'


Of course, I would not use the above code snippet as is.  It will bomb if
either $1 or $2 are blank or refer to a file that does not exist.  You
should do your normal command line checking, then use a "test -f" check to
ensure that your files exist and are regular files.

I'm a linux newbie, but I know a little bit about scripting.  I hope this
helps.

John Baskette 

> ----------
> From:         Steve Youngs[SMTP:[EMAIL PROTECTED]]
> Sent:         Thursday, October 28, 1999 6:18 PM
> To:   Linux-Newbie
> Subject:      comparing file sizes
> 
> 
> Okay, I'm trying to get a script to do one of the following:
> 
>       if [ sizeof(file1) < sizeof(file2)]; then
>               cp file2 file3
>       fi
> 
> or
> 
>       if [ sizeof(file1) < 1024 ]; then
>               cp file2 file3
>       fi
> 
> 
> The above examples are illustrations only.  It is designed just to
> give you an idea of what I am trying to do.  Anyone got any ideas?
> 
> Richard, you're the "script guru" :-), what do you think?
> 
> -- 
> ---Regards, Steve Youngs------------------------------------------
> | On the box it said "Requires Windows 95, Windows NT or better" |
> |                   So I installed Linux...                    |
> -----------------Email:-<[EMAIL PROTECTED]>--ICQ:-34307457---
> 

Reply via email to