Answer the question
In order to leave comments, you need to log in
How to solve a problem in Bash?
There is a code:
#!/bin/bash
if [ -s file.txt ]
then
echo "File is empty"
else
count=1
cat file.txt | while read line
do
echo "$line"
count=$(( $count + 1 ))
done
fi
Answer the question
In order to leave comments, you need to log in
-s checks that the file exists and is NOT empty.
if you want to use test, then the logic is this:
if [ -f file.txt ]; then
if [ -s file.txt ]; then
echo "file.txt is not empty
else
echo "file.txt is empty"
fi
else
echo "file.txt doesn't exists"
fi
if ; then
echo "file.txt is empty"
else
echo "file.txt doesn't exist or not empty"
fi
if [ -s file.txt ]
then
echo "File is empty"
-s FILE
FILE exists and has a size greater than zero
To you in the first answer resulted from docks the quote. It says the file exists and has a size greater than zero, so when the condition is met, it cannot be empty in any way), it is empty in else.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question