使用 Bash 读取文件中的行并避免使用 # 行 [英] Reading lines in a file and avoiding lines with # with Bash

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

问题描述

我试过了:

file="myfile"while read -r 行做[[ $line = #* ]] &&继续地址=$line127.0.0.1"完成<$文件"

这段代码没有避免以注释开头的行.即使我没有任何评论,dnsmasq 也会告诉我有错误.

它将是一个 dnsmasq conf 文件,它将像这样读取和插入域名:address=mydomain.com127.0.0.1.><小时>

1

输入文件:

domain1.com域名2.com域名3.com#domain4.com域名5.com

输出应该是:

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

我会将脚本放到 /etc/dnsmasq.d/ 目录中,以便 dnsmaq.conf 可以在 dnsmasq 启动时进行处理.

解决方案

使用 [[ "$line" = "#*" ]]

更安全

顺便说一句,address="\${line}\127.0.0.1"

UPD:

如果我理解正确,您需要将每个未注释的域更改为 address=domain127.0.0.1.使用 sed 可以快速轻松地完成,不需要 bash 程序.

$>猫./文本域名1.com域名2.com域名3.com#domain4.com域名5.com$>sed -r -e 's/(^[^#]*$)/address=/1/127.0.0.1/g' ./text2地址=/domain1.com/127.0.0.1地址=/domain2.com/127.0.0.1地址=/domain3.com/127.0.0.1#domain4.com地址=/domain5.com/127.0.0.1

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

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

UPD2:如果您想在 bash 脚本中执行所有这些操作,这里是您的代码修改:

file="./text2"while read -r line;做[[ "$line" =~ ^#.*$ ]] &&继续回声地址=/${line}/127.0.0.1"完成<$文件"

它的输出:

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

I tried this:

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

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.

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


EDIT:1

Input file:

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

Output should be:

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

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

解决方案

It's safer to use [[ "$line" = "#*" ]]

Btw, address="\${line}\127.0.0.1"

UPD:

If I've understand you right you need to change every uncommented domains to address=domain127.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

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: 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"

And it's output:

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

这篇关于使用 Bash 读取文件中的行并避免使用 # 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆