1

I'm trying to add some text to the end of a few files. I have made a file, where I have 5 servername. Each servername corresponds to a separate config file. (The path of these config files is not known).

I am using below code to get the file path,

MyCode:

#!/bin/bash
for i in $(cat serverlist-file | while read f; do find . -name "*$f*"; done);
do
echo $i
done

Output:

/data/servers/customer01/server01.cfg
/data/servers/customer01/server02.cfg
/data/servers/customer02/server03.cfg
/data/servers/customer03/server04.cfg
/data/servers/customer03/server05.cfg

I am using below code to get the list of servers,

MyCode:

#!/bin/bash
for j in $(cat serverlist-file);
do
echo $j
done

Output:

server01
server02
server03
server04
server05

Now I want to edit those config files and add text to it.

I am using below code to add the required text:

#!/bin/bash
for i in $(cat serverlist-file | while read f; do find . -name "*$f*"; done);
do
for j in $(cat serverlist-file);
do 
sed -i -e "\$a\
this\ is\ a\ config\ file\nfor\ $j" $i
done
done

Expected Output:

/data/servers/customer01/server01.cfg
this is a config file
for server01
/data/servers/customer01/server02.cfg
this is a config file
for server02
/data/servers/customer02/server03.cfg
this is a config file
for server03
/data/servers/customer03/server04.cfg
this is a config file
for server04
/data/servers/customer03/server05.cfg
this is a config file
for server05

Edit for a reply to @ShawnMilo:

I am trying to bulk add some config to some nagios config files, but not to all server config files.

So, searching with find . -name '*.config' isn't going to work, because then all the config files will get edited. I only want specific files to get edited, just the servers from the serverlist-file.

Nagios configs need to have the hostname of the server in them, like:

define service {
use                             generic-service
host_name                       server01
service_description             SSH
contact_groups                   linux
check_command                check_something
}

1 Answer 1

1

Seems like an odd requirement. What are you actually trying to do?

In any case, this will do what was requested:

$ find . -name '*.config' | while read x; do echo $x; cat $x; echo; done
./data/servers/customer02/server03.config
default stuff here

./data/servers/customer03/server05.config
default stuff here

./data/servers/customer03/server04.config
default stuff here

./data/servers/customer01/server01.config
default stuff here

./data/servers/customer01/server02.config
default stuff here

$ find . -name '*.config' | while read x; do name=$(basename $x); echo -e "this is a config file\nfor ${name%%.*}" >> $x; done

$ find . -name '*.config' | while read x; do echo $x; cat $x; echo; done
./data/servers/customer02/server03.config
default stuff here
this is a config file
for server03

./data/servers/customer03/server05.config
default stuff here
this is a config file
for server05

./data/servers/customer03/server04.config
default stuff here
this is a config file
for server04

./data/servers/customer01/server01.config
default stuff here
this is a config file
for server01

./data/servers/customer01/server02.config
default stuff here
this is a config file
for server02
Sign up to request clarification or add additional context in comments.

1 Comment

You can use the same find command to find all the files and add them to a text file. Then grep them against your list, then use that list. ``` find . -name '*.conf' > all_files.txt grep -F -f serverlist all_files.txt > to_change.txt ``` Then use to_change.txt for the initial loop I describe instead of the output of the find command.

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.