0

I need to add a record to several hosts file in Linux named directory with a bash script. I want to open each hosts file and add the line:

webmail.domain.com. IN A 192.168.1.1

for each domain.com.hosts file in the named directory. Could you give me some hints?

4
  • thanks the problem is that each file has different "domain.com" so the script should add the string based on domain.com.hosts file Commented Nov 28, 2013 at 6:07
  • show some of the file names Commented Nov 28, 2013 at 6:10
  • code this is file domain.com.hosts domain.com. IN SOA ns1.domain.com. root.domain.com ( 1173351722 10800 3600 432000 38400 ) NS ns1.domain.com. NS ns2.domain.com. A 192.168.1.1 www A 192.168.1.1 webmail.domain.com IN A 192.168.1.1 this is file domain2.com.hosts domain2.com. IN SOA ns1.domain2.com. root.domain2.com ( 1173351722 10800 3600 432000 38400 ) NS ns1.domain2.com. NS ns2.domain2.com. A 192.168.1.1 www A 192.168.1.1 webmail.domain2.com IN A 192.168.1.1 Commented Nov 28, 2013 at 6:22
  • Edit the question instead, comments have insufficient formatting capabilities. Commented Nov 28, 2013 at 7:05

1 Answer 1

2

Assuming xx.com.hosts should have webmail.xx.com added,

for f in *.com.hosts; do
    echo "${f%hosts} IN A 192.168.1.1" >>"$f"
done

The construct ${var%suffix} produces the value of $var with suffix removed if present. (There is also a corresponding #prefix construct.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.