Empty file check

Hi gurus ,

I have two files and i want to perform different action based on the condition if both or either is empty

If [ File 1 is not empty && File two is not empty ]
 then 
 Do something 
 elif [ file 1 is empty && file 2 is not empty ]
 then 
 do something 
 elif [ file 1 is not empty && file 2 is empty ]
 then 
 do something 
 else 
 do something
 fi

I have tried the below bt its not working. only the first condition is getting evaluated

 if [[  -s "$f1"  && -s "$f2" ]]
 then
 echo " Both not empty"
 elif [[ ! -s "$f1"  && -s "$f2" ]]
 echo "First file empty and second not empty"
 elif [[  -s "$f1"  && ! -s "$f2" ]]
 echo "Fist file not empty and second file empty"
 else
 echo " Both Files empty"

Yes the part after the && will only get evaluated if the part before the && is true. This is by design, && is not a true logical AND.

--
Also there is a syntax error, since the "then" statements were omitted.

--
Also there are the wrong quotes characters in you file! Replace them with "

This might help you

$ touch test1
$ cat test1
$ touch test2
$ echo 'I am not empty' >test2
$ cat test2
I am not empty
#!/bin/bash



empty(){
	for file in $(echo "$*"); do 
		[ -s  "$file"  ] || return 1
	done
}

# Call function with one empty and non empty file
empty test1 test2

if [ "$?" -eq "0" ]; then
	echo "all fine"
else
        echo "something went wrong"
fi 

# Call function with non empty files
empty test2 test2

if [ "$?" -eq "0" ]; then
	echo "all fine"
else
	echo "something went wrong"
fi 
$ bash test
something went wrong
all fine

---------- Post updated at 01:35 AM ---------- Previous update was at 01:30 AM ----------

what I wanted to suggest you that create function like above according to your need and play with exit code it's easy.

Or just:

if [[ -s "$f1" ]]; then
  if [[ -s "$f2" ]]; then
    echo �Both not empty�
  else
    echo "First file not empty and second file empty"
  fi
else
  if [[ -s "$f2" ]]; then
    echo "First file empty and second not empty"
  else
    echo "Both empty"
  fi
fi

Correction:

if [ -s "$f1" ] && [ -s "$f2" ]
then
        echo "Both not empty"
elif [ ! -s "$f1" ] && [ -s "$f2" ]
then
        echo "First file empty and second not empty"
elif [ -s "$f1" ] && [ ! -s "$f2" ]
then
        echo "First file not empty and second empty"
else
        echo "Both Files empty"
fi