本文介绍了击:读行的文件,并避免与#线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这样:

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

这code不避免与注释开头的行。即使我没有任何意见,的dnsmasq 告诉有错误。

This code doesn't avoid the lines that begin with comments. Even if I don't have any comments, dnsmasq tells that there are errors.

它会是一个的dnsmasq 的conf文件,它会读取并插入域名,就像这样:地址= \\ mydomain.com \\ 127.0 .0.1

Its going to be a dnsmasq conf file, and it will read and insert domain names like so: address=\mydomain.com\127.0.0.1.

输入文件:

domain1.com
domain2.com
domain3.com
#domain4.com
domain5.com

输出应该是:

address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
address=/domain5.com/127.0.0.1

我会的 /etc/dnsmasq.d / 的下降脚本目录,这样的可以处理它时,启动的dnsmasq

I will drop the script in /etc/dnsmasq.d/ directory so that dnsmaq.conf can process it when dnsmasq is started.

推荐答案

它的安全使用 [$行=\\#*]]

顺便说一句,地址=\\\\ $ {}行\\\\ 127.0.0.1

UPD

如果我理解你的权利,你需要在每个注释掉域更改为地址= \\域\\ 127.0.0.1 。它可以做快速和容易与 SED ,还有在bash程序没有必要。

If I've understand you right you need to change every uncommented domains to address=\domain\127.0.0.1. It could be done fast and easy with sed, there is no need in bash-program.

$> cat ./text
domain1.com
domain2.com
domain3.com
#domain4.com
domain5.com

$> sed -r -e 's/(^[^#]*$)/address=\/\1\/127.0.0.1/g' ./text2
address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
#domain4.com
address=/domain5.com/127.0.0.1

如果您需要删除注释行,用sed可以用 / matched_line / D

If you need to remove commented lines, sed can do it too with /matched_line/d

$> sed -r -e 's/(^[^#]*$)/address=\/\1\/127.0.0.1/g; /^#.*$/d' ./text2
address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
address=/domain5.com/127.0.0.1

UPD2 :如果你想要做的bash脚本里面所有的东西,这里是你的code修改:

UPD2: if you want to do all that stuff inside the bash script, here is your code modification:

file="./text2"
while read -r line; do
    [[ "$line" =~ ^#.*$ ]] && continue
    echo "address=/${line}/127.0.0.1"
done < "$file"

和它的输出:

address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
address=/domain5.com/127.0.0.1

这篇关于击:读行的文件,并避免与#线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 09:40