参见英文答案 > 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