Always gives false even though file exists and is not empty

It is enough to check for -s, because it says:

FILE exists and has a size greater than zero

http://unixhelp.ed.ac.uk/CGI/man-cgi?test

also your output is switched, so it outputs does not exists when a file exists, because -s will give TRUE if file exists AND has a size > 0.

So correctly you should use:

echo " enter file name "
read file
if [ -s "$file" ]
then 
   echo " file exists and is not empty "
else
   echo " file does not exist, or is empty "
fi

This will give you the expected output.

Also it should be

read file

instead of

read $file

If you want further informations, I recommand reading man test and man read

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)