I have a while loop, simplified like this:
while read -r line
do
(delete some lines from file2.txt)
done < file.txt
If file2.txt
is empty, then this while loop has no need to function any longer.
In other words, I need this:
while read -r line AND file2.txt IS NOT EMPTY
do
(delete some lines from file2.txt
done < file.txt
I've tried to combined while read -r line
with -s file2.txt
, but the result does not work:
while [ read -r line ] || [ -s file2.txt ]
do
(delete some lines from file2.txt)
done < file.txt
How can I use this while loop to read the lines in a file while also checking that another file is not empty?
Combine the read and the test as:
while read -r line && [ -s file2.txt ]
do
# (delete some lines from file2.txt)
echo "$line"
done <file.txt
This will check, before every iteration of the loop, whether file2.txt
is non-empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With