I tried this:
file="myfile"
while read -r line
do
[[ $line = \#* ]] && continue
"address=\$line\127.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.com\127.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.
Comment lines can and often do begin with whitespace. Here's a bash native regex solution that handles any preceeding whitespace;
Maybe you can try
Check the
~
in operand!It has 3 parts. Please read each to understand clearly
awk -F'#' '{print $1}' t.txt
awk 'NF > 0'
awk '{print "address=/"$0"/127.0.0.1"}'
So Total Script Needed is,
Output :
This could also be accomplished with 1
sed
command:This will modify the file in-place (creating a backup copy first), removing all lines starting with a
#
.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=\domain\127.0.0.1
. It could be done fast and easy withsed
, there is no need in bash-program.If you need to remove commented lines, sed can do it too with
/matched_line/d
UPD2: if you want to do all that stuff inside the bash script, here is your code modification:
And it's output: