bash loop skip commented lines

2019-04-20 14:19发布

I'm looping over lines in a file. I just need to skip lines that start with "#". How do I do that?

 #!/bin/sh 

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

Thanks for any help!

标签: bash shell
1条回答
我只想做你的唯一
2楼-- · 2019-04-20 14:49
while read line; do
  case "$line" in \#*) continue ;; esac
  ...
done < /tmp/my/input

Frankly, however, it is often clearer to turn to grep:

grep -v '^#' < /tmp/myfile | { while read line; ...; done; }
查看更多
登录 后发表回答