如何使用`while read -r line`同时检查BASH中的另一个文件是否为空?

Vil*_*age 4 bash while-loop

我有一个while循环,简化如下:

while read -r line
do
    (delete some lines from file2.txt)
done < file.txt
Run Code Online (Sandbox Code Playgroud)

如果file2.txt为空,则此while循环不再需要运行.

换句话说,我需要这个:

while read -r line AND file2.txt IS NOT EMPTY
do
    (delete some lines from file2.txt
done < file.txt
Run Code Online (Sandbox Code Playgroud)

我试着结合while read -r line使用-s file2.txt,但结果不工作:

while [ read -r line ] || [ -s file2.txt ]
do
    (delete some lines from file2.txt)
done < file.txt
Run Code Online (Sandbox Code Playgroud)

如何使用while循环读取文件中的行,同时还检查另一个文件是否为空?

Joe*_*Joe 11

将读取和测试结合起来:

while read -r line && [ -s file2.txt ]
do
  # (delete some lines from file2.txt)
  echo "$line"
done <file.txt
Run Code Online (Sandbox Code Playgroud)

这将在循环的每次迭代之前检查是否file2.txt为非空.