2012-09-19 79 views
11

我正在循环遍历文件中的行。我只需要跳过以“#”开头的行。 我该怎么做?bash循环跳过注释行

#!/bin/sh 

while read line; do 
    if ["$line doesn't start with #"];then 
    echo "line"; 
    fi 
done < /tmp/myfile 

感谢您的帮助!

回答

15
while read line; do 
    case "$line" in \#*) continue ;; esac 
    ... 
done < /tmp/my/input 

坦率地说,然而,它往往是更清晰的转向grep

grep -v '^#' < /tmp/myfile | { while read line; ...; done; } 
+0

你也可以做一些与'expr'或后缀去除(例如,'[-z“$ {太聪明行%%#*}“]'),但我认为这些可能会比'case'选项同样或更少。 – pilcrow

+2

也可以使用'grep -v'^ \ s *#' mklement0

+1

如果[[$ line =〜^#]];则另一个选项(如果某人想要避免使用'grep')可能是'然后继续; fi'。 –