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. |
Quote:
Code:
sed 's/\$whattochange/\$changeto/g' /etc/nagios/objects/commands.cfg > /etc/nagios/objects/commands.cfg.new |
Quote:
the sed expression in double quotes Code:
sed "s/$variable1/$variable2/g" file |
And if you use '-i' option you don't even have to create a second file.
|
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. |
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 Code:
-i[SUFFIX], --in-place[=SUFFIX] Code:
sed -i "s@$variable1@$variable2@g" file |
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" Code:
sycamorex@mordor:~/temp$ var1="/usr/bin/printf "%b" "$lasst" blah" |
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. |
Quote:
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' |
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 "###################################################################" |
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) |
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 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 "###################################################################" |
All times are GMT -5. The time now is 07:40 PM. |