[SOLVED] changing a line in a config file while installing the program using bash script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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:
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.
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:
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
@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
@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.
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
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.
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.
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
Last edited by Joe_Louis; 07-22-2011 at 10:28 PM.
Reason: Noticed that I forgot to change how the Services file gets updated. That should be fixed now.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.