[SOLVED] Read from Input File and Append to Multiple Files
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sorry for the long message, I wanted to be as clear as possible. I have to add hundreds of new servers to hundreds of configuration files in Nagios. Here is a sample structure:
------------------------------------------------------------------------
servers.txt has:
define service{
use generic-service
host_name SERVER2
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!xinetd
}
------------------------------------------------------------------------
Now I need to add SERVER3, SERVER4 and SERVER5 to each of the service-servers-*.cfg files, following the same structure as the above. Blank line between the "defined services". I knew how to do this when I was adding ALL servers to a single configuration file. I would then merge that file to my final CFG file. Now, my issue is all those service files are separate and I want to append the new servers at the end of each file.
Hope I made it clear, cause right now I am really confused on how to do this!
You could read in the server names, in a loop, and set a variable. Then create the config file using a HERE document, which will replace variable with it's value, when creating the config file:
Here is an example:
Code:
:> configfile
for server in SERV1, SERV2, SERV3; do cat <<END >>configfile
use generic-server
host_name $server
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
END
done
Thanks again to you both for helping out. I haven't been able to finish the final script yet but I am getting really close.
I tried both of your approaches. jschiwal's requires a little more work on my end but is somewhat easier to work on. grail's I got much closer to what I was expecting as an end result but required me to go deeper trying to understand the script process.
Either way your solutions were great! I will try finishing it tomorrow and post a follow up if I get stuck again.
Always a pleasure to talk to people like you who really know their game!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.