加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 22:29:07 所属栏目:安全 来源:网络整理
导读:我有一个while循环,简化如下: while read -r linedo (delete some lines from file2.txt)done file.txt 如果file2.txt为空,则此while循环不再需要再运行. 换句话说,我需要这个: while read -r line AND file2.txt IS NOT EMPTYdo (delete some lines from
我有一个while循环,简化如下:

while read -r line
do
    (delete some lines from file2.txt)
done < file.txt

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

换句话说,我需要这个:

while read -r line AND file2.txt IS NOT EMPTY
do
    (delete some lines from file2.txt
done < file.txt

我尝试将-r-read行与-s file2.txt结合使用,但结果不起作用:

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

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

解决方法

将读取和测试结合起来:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读