riccardo
riccardo

Reputation: 57

how to use bash to add a string to each named hosts file

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?

Upvotes: 0

Views: 188

Answers (1)

tripleee
tripleee

Reputation: 189317

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.)

Upvotes: 2

Related Questions