Reading lines in a file and avoiding lines with # with Bash

后端 未结 10 2075
清歌不尽
清歌不尽 2020-12-03 06:47

I tried this:

file=\"myfile\"
while read -r line
do
    [[ $line = \\#* ]] && continue
    \"address=\\$line\\127.0.0.1\"
done < \"$file\"
         


        
10条回答
  •  醉话见心
    2020-12-03 07:36

    Comment lines can and often do begin with whitespace. Here's a bash native regex solution that handles any preceeding whitespace;

    while read line; do
      [[ "$line" =~ ^[[:space:]]*# ]] && continue
      ...work with valid line...
    done
    

提交回复
热议问题