简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM