LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   changing a line in a config file while installing the program using bash script (https://www.linuxquestions.org/questions/programming-9/changing-a-line-in-a-config-file-while-installing-the-program-using-bash-script-893137/)

Joe_Louis 07-22-2011 07:59 AM

changing a line in a config file while installing the program using bash script
 
Hi Everyone. I am trying to write a bash script that installs a program fully automated for me. I am stuck at the one part where i need to change a line in one config file. I have tried various syntax for sed and none have worked. Maybe someone can suggest how to go about this. Since the line appears in the middle of the file, I can't use echo or cat. Also the file will be diffrent lengths depending on the install. The line I need changed is:

command_line /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out

I need that changed to

command_line /usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl -d HOSTPERFDATA

the last syntax i tried is

whattochange="/usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out"
changeto="/usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl"

sed 's/$whattochange/$changeto/g' /etc/nagios/objects/commands.cfg > /etc/nagios/objects/commands.cfg.new

Yes this is for PNP4Nagios on centos 5.6 64bit. If i can get this figured out. I will be willing to post the whole thing for others to use. it will be used to install PNP after a working nagios install is done.

raevin 07-22-2011 08:06 AM

Quote:

Originally Posted by Joe_Louis (Post 4422257)
Hi Everyone. I am trying to write a bash script that installs a program fully automated for me. I am stuck at the one part where i need to change a line in one config file. I have tried various syntax for sed and none have worked. Maybe someone can suggest how to go about this. Since the line appears in the middle of the file, I can't use echo or cat. Also the file will be diffrent lengths depending on the install. The line I need changed is:

command_line /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out

I need that changed to

command_line /usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl -d HOSTPERFDATA

the last syntax i tried is

whattochange="/usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out"
changeto="/usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl"

sed 's/$whattochange/$changeto/g' /etc/nagios/objects/commands.cfg > /etc/nagios/objects/commands.cfg.new

Yes this is for PNP4Nagios on centos 5.6 64bit. If i can get this figured out. I will be willing to post the whole thing for others to use. it will be used to install PNP after a working nagios install is done.

I'm not great w/ sed (or regex for that matter period), but I believe you have to back slash the $. So your sed should look like this:
Code:

sed 's/\$whattochange/\$changeto/g' /etc/nagios/objects/commands.cfg > /etc/nagios/objects/commands.cfg.new

sycamorex 07-22-2011 08:08 AM

Quote:

Originally Posted by raevin (Post 4422265)
I'm not great w/ sed (or regex for that matter period), but I believe you have to back slash the $. So your sed should look like this:
Code:

sed 's/\$whattochange/\$changeto/g' /etc/nagios/objects/commands.cfg > /etc/nagios/objects/commands.cfg.new

To allow for a variable substitution within sed you shouldn't escape the $ sign. What you need to do is enclose
the sed expression in double quotes

Code:

sed "s/$variable1/$variable2/g" file

grail 07-22-2011 08:29 AM

And if you use '-i' option you don't even have to create a second file.

Joe_Louis 07-22-2011 10:47 AM

Thank you guys for the quick responses.

@raevin, when i try the syntax you suggested i get nothing. It looks like it executed properly but there is no change in the file.

@sycamorex, when i tried your syntax i recieved the error

sed: -e expression #1, char 8: unknown option to `s'

I think that has to do with how the line in the variable is formatted. Unfortunately, I can not change the line format that needs changed by my script.
basically what i am trying to pass is

sed 's//usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t $HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out//usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl/g' /etc/nagios/objects/commands.cfg > /etc/nagios/objects/commands.cfg.new

@grail, I thought the -i option is for inserting a line not replacing it. Please correct me if i am wrong on this.


I would be willing to lose the variables and just make it all one line. Again, Thank you everyone for the quick responses. Except for this little bit of code, I pretty much have the whole script done and tested.

grail 07-22-2011 11:28 AM

Ok ..

1. so not sure about raevin's but I am guessing it has to do with the escaping maybe not working as expected.

2. sycamorex's only issue was that as the variables contain / that sed gets lost on which ones it is using. Simply change to something not in your variables, on a quick glance
it could be a @:
Code:

sed "s@$variable1@$variable2@g" file
3. From man page for sed:
Code:

-i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)

So combined with step 2 above:
Code:

sed -i "s@$variable1@$variable2@g" file
Also, if nervous you can make a backup on the fly with -> -i.bak

sycamorex 07-22-2011 11:36 AM

On a separate note, I don't think you should be using multiple double quotes:

Code:

whattochange="/usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out"
It might not affect your sed script as you don't use double quotes in the changeto variable, but for your information:

Code:

sycamorex@mordor:~/temp$ var1="/usr/bin/printf "%b" "$lasst" blah"
sycamorex@mordor:~/temp$ echo $var1
/usr/bin/printf %b blah
sycamorex@mordor:~/temp$ var2='/usr/bin/printf "%b" "$lasst" blah'
sycamorex@mordor:~/temp$ echo $var2
/usr/bin/printf "%b" "$lasst" blah


Joe_Louis 07-22-2011 03:11 PM

grail and sycamorex, thank you both for your help. unfortunatley, it is still not working. I no longer get an error when i try to run the script as suggested. It is like it doesn't find the line to change and just exits.

here is what i have now. I have it echo the variables to verify that they are being passed properly.

whattochange='/usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out'
changeto="/usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl -d HOSTPERFDATA"
echo "$whattochange"
echo "$changeto"

sed -i "s@$whattochange@$changeto@g" /etc/nagios/objects/commands.cfg

just to as a fyi, this is what the full line looks like in the file with tabs.

command_line /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out

don't know if that will help solve this mystery. Thanks again for all the help.

sycamorex 07-22-2011 04:42 PM

Quote:

Originally Posted by Joe_Louis (Post 4422683)
grail and sycamorex, thank you both for your help. unfortunatley, it is still not working. I no longer get an error when i try to run the script as suggested. It is like it doesn't find the line to change and just exits.

here is what i have now. I have it echo the variables to verify that they are being passed properly.

whattochange='/usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out'
changeto="/usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl -d HOSTPERFDATA"
echo "$whattochange"
echo "$changeto"

sed -i "s@$whattochange@$changeto@g" /etc/nagios/objects/commands.cfg

just to as a fyi, this is what the full line looks like in the file with tabs.

command_line /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOS TOUTPUT$\t$HOSTPERFDATA$\n" >> /var/nagios/host-perfdata.out

don't know if that will help solve this mystery. Thanks again for all the help.

Please use the "code" tags to post your code. What you need to do is escape each \ in whattochange

Code:

whattochange='/usr/bin/printf "%b" "$LASTHOSTCHECK$\\t$HOSTNAME$\\t$HOSTSTATE$\\t$HOSTATTEMPT$\\t$HOSTSTATETYPE$\\t$HOSTEXECUTIONTIME$\\t$HOSTOUTPUT$\\t$HOSTPERFDATA$\\n" >> /var/nagios/host-perfdata.out'

Joe_Louis 07-22-2011 05:59 PM

sycamorex, Thank you so much, that was it. It works now. Thank you thank you thank you. I will post the whole script as promised. Please remember that i did this on Centos 5.6 64bit. It already had Nagios Installed from repo's and running at the time. Everyone is more then welcome to leave comments and let me know how this worked for them. I also happen to have a full script that installs both Nagios and this on a clean install of Centos 5.6 64bit. Thank you again to everyone for help with this.


Code:

echo "###################################################################"
echo "Enter the directory and file name for where the custom config service"
echo "definitions are stored"
echo "###################################################################"

read CustomServiceConfig

yum -y install php-gd rrdtool gcc-c++

cd /tmp
wget http://sourceforge.net/projects/pnp4nagios/files/PNP-0.6/pnp4nagios-0.6.13.tar.gz/download
tar -xzf pnp4nagios-0.6.13.tar.gz
cd pnp4nagios-0.6.13
./configure --prefix=/usr/share/nagios
make all
make install
make install-webconf
make install-config
make install-init

ln -s /usr/share/nagios/share/pnp /usr/share/nagios/pnp

sed -e "s/process_performance_data=0/process_performance_data=1/" /etc/nagios/nagios.cfg > /etc/nagios/nagios.cfg.new
cat /etc/nagios/nagios.cfg.new > /etc/nagios/nagios.cfg
rm -f /etc/nagios/nagios.cfg.new

sed -e "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/" /etc/nagios/nagios.cfg > /etc/nagios/nagios.cfg.new
cat /etc/nagios/nagios.cfg.new > /etc/nagios/nagios.cfg
rm -f /etc/nagios/nagios.cfg.new

sed -e "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/" /etc/nagios/nagios.cfg > /etc/nagios/nagios.cfg.new
cat /etc/nagios/nagios.cfg.new > /etc/nagios/nagios.cfg
rm -f /etc/nagios/nagios.cfg.new

sed -e "s|/usr/local/nagios/etc/htpasswd.users|/etc/nagios/htpasswd.users|" /etc/httpd/conf.d/pnp4nagios.conf >  etc/httpd/conf.d/pnp4nagios.conf.new
cat /etc/httpd/conf.d/pnp4nagios.conf.new > /etc/httpd/conf.d/pnp4nagios.conf
rm -f /etc/httpd/conf.d/pnp4nagios.conf.new

sed '/service_description/ a\\        action_url                        /nagios/share/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$' $CustomServiceConfig > $CustomServiceConfig.new
cat $CustomServiceConfig.new > $CustomServiceConfig
rm -f $CustomServiceConfig.new

Hosttochange='/usr/bin/printf "%b" "$LASTHOSTCHECK$\\t$HOSTNAME$\\t$HOSTSTATE$\\t$HOSTATTEMPT$\\t$HOSTSTATETYPE$\\t$HOSTEXECUTIONTIME$\\t$HOSTOUTPUT$\\t$HOSTPERFDATA$\\n" >> /var/nagios/host-perfdata.out'
changehostto="/usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl -d HOSTPERFDATA"

Servicetochange='/usr/bin/printf "%b" "$LASTSERVICECHECK$\\t$HOSTNAME$\\t$SERVICEDESC$\\t$SERVICESTATE$\\t$SERVICEATTEMPT$\\t$SERVICESTATETYPE$\\t$SERVICEEXECUTIONTIME$\\t$SERVICELATENCY$\\t$SERVICEOUTPUT$\\t$SERVICEPERFDATA$\\n" >> /var/nagios/service-perfdata.out'
changeserviceto=" /usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl"

sed "s|$Hosttochange|$changehostto|g" /etc/nagios/objects/commands.cfg > /etc/nagios/objects/commands.cfg.new1
sed "s|$Servicetochange|$changeserviceto|g" /etc/nagios/objects/commands.cfg.new1 > /etc/nagios/objects/commands.cfg.new2
cat /etc/nagios/objects/commands.cfg.new2 > /etc/nagios/objects/commands.cfg
rm -f /etc/nagios/objects/commands.cfg.new1
rm -f /etc/nagios/objects/commands.cfg.new2



mv /usr/share/nagios/share/install.php /usr/share/nagios/share/install.php.old

yum -y remove libstdc++-devel kernel-headers glibc-headers glibc-devel gcc cpp gcc-c++

service httpd restart
service nagios restart

I tested this on my install and it worked flawless for me. YMMV

sycamorex 07-22-2011 06:23 PM

I'm glad you got it working. I don't use nagios at the moment. If I decide to install it, I'll check your script:)

If you think the problem has been solved, you might now mark the thread as solved (in the thread tools at the top of the page)

grail 07-23-2011 05:05 AM

I would add that you can get rid of all the cat new to old and rm of new by simply using the '-i' option in sed, eg.
Code:

sed -e "s/process_performance_data=0/process_performance_data=1/" /etc/nagios/nagios.cfg > /etc/nagios/nagios.cfg.new
cat /etc/nagios/nagios.cfg.new > /etc/nagios/nagios.cfg
rm -f /etc/nagios/nagios.cfg.new

# becomes

sed -i "s/process_performance_data=0/process_performance_data=1/" /etc/nagios/nagios.cfg

Now you may need to confirm, as I to have not used nagios, if the data on the Hostto and Serviceto lines are the only ones with my changes in them, but I see
no reason to use such a long string to identify the lines. See if this may work for you or at least give you some ideas:
Code:

echo "###################################################################"
echo "Enter the directory and file name for where the custom config service"
echo "definitions are stored"
echo "###################################################################"

read CustomServiceConfig

yum -y install php-gd rrdtool gcc-c++

cd /tmp
wget http://sourceforge.net/projects/pnp4nagios/files/PNP-0.6/pnp4nagios-0.6.13.tar.gz/download
tar -xzf pnp4nagios-0.6.13.tar.gz
cd pnp4nagios-0.6.13
./configure --prefix=/usr/share/nagios
make all
make install
make install-webconf
make install-config
make install-init

ln -s /usr/share/nagios/share/pnp /usr/share/nagios/pnp

sed -i -e '/process_performance_data/s/0/1/' \
      -e '/#host_perfdata_command/s/^#//' \
      -e '/#service_perfdata_command/s/^#//' /etc/nagios/nagios.cfg

sed -i 's|/usr/local/nagios/etc/htpasswd.users|/etc/nagios/htpasswd.users|' /etc/httpd/conf.d/pnp4nagios.conf

sed -i '/service_description/ a\\        action_url                        /nagios/share/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$' $CustomServiceConfig

perl_script='/usr/bin/perl /usr/share/nagios/libexec/process_perfdata.pl'

Hosttochange='.*>> /var/nagios/host-perfdata.out'
changehostto="$perl_script -d HOSTPERFDATA"

Servicetochange='.*>> /var/nagios/service-perfdata.out'

sed -i -e "s|$Hosttochange|$changehostto|g" \
      -e "s|$Servicetochange|$perl_script|g" /etc/nagios/objects/commands.cfg

mv /usr/share/nagios/share/install.php /usr/share/nagios/share/install.php.old

yum -y remove libstdc++-devel kernel-headers glibc-headers glibc-devel gcc cpp gcc-c++

service httpd restart
service nagios restart



All times are GMT -5. The time now is 07:40 PM.