bash loop skip commented lines

2019-04-20 15:05发布

问题:

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!

回答1:

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; }


标签: bash shell