微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux – 如何在bash for循环中跳过带注释(#space)的行

参见英文答案 > Reading lines in a file and avoiding lines with # with Bash                                    10个
使用以下代码

#!/bin/bash
export LC_ALL=C

for input_file in $(<inputflist.txt)
do
    case "$input_file" in \#*) continue ;; esac
    echo $input_file

done

和inputflist.txt有以下内容

# foo.txt
bar.txt

我希望它只打印最后一行bar.txt,但它会打印出来:

foo.txt
bar.txt

什么是正确的方法呢?

解决方法:

这应该工作:

while IFS= read -r line; do
   [[ $line =~ ^[[:blank:]]*# ]] && continue
   echo "$line"
done < inputflist.txt

输出

bar.txt

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐