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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...