if condition for filesize

Hi,

I want to check file size in unix, based on file size I am going to execute appropriate command.

I tried below, but getting the error.

System details �

Machine hardware: sun4u
OS version: 5.9

if ( -s $f1 ) then
  echo "filename exists and is > 0 bytes"
else
  echo "filename does not exist or is zero length"
fi

Error - if: Badly formed number

Thankx

using bash:-

[[ -s $f ]] && echo "filename exists and is > 0 bytes" || echo "filename does not exist or is zero length"

:):):slight_smile:

thankx for reply.

but i am getting "filename does not exist or is zero length" even though file is present

Code:

#!/bin/csh
#######################
setenv UPDLOG /data/updlog
setenv f1 /data/FILING.txt

[[ -s $f1 ]] && echo "filename exists and is > 0 bytes" || echo "filename does not exist or
 is zero length"
echo "*****Hello there, you are about to run Update.  ****** "
echo ""
echo    "End Load --->" `date` >> $UPDLOG/upd.log
####################### THE END ##################################

Thankx

you are using csh!!!! and post below for bash shell, so use below:-

[  -s  $f  ] && echo "filename exists and is > 0 bytes" || echo "filename does not exist or is zero length"

when used -

[  -s  $f  ] && echo "filename exists and is > 0 bytes" || echo "filename does not exist or is zero length" 

got -

Missing ]
filename does not exist or is zero length

try test command:-

test -s  $f  && echo "filename exists and is > 0 bytes" || echo "filename does not exist or is zero length"
$ ./justdoit filename
## justdoit ##
#!/bin/csh
set f1="$1"
if ( ! -e $f1 ) then
  echo "file does not exist "
  else
   if ( -z $f1 ) then
    echo "file is exist and file size is zero"
    else
  echo "file is exist and file size is > 0 bytes "
   endif
endif

thank you so much..!! :b: