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"
$ 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.
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