LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2011, 07:59 AM   #1
Joe_Louis
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Rep: Reputation: Disabled
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.

Last edited by Joe_Louis; 07-22-2011 at 08:02 AM.
 
Old 07-22-2011, 08:06 AM   #2
raevin
Member
 
Registered: Jul 2004
Distribution: Arch Linux, Ubuntu
Posts: 80

Rep: Reputation: 16
Quote:
Originally Posted by Joe_Louis View Post
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
 
Old 07-22-2011, 08:08 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by raevin View Post
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
 
Old 07-22-2011, 08:29 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
And if you use '-i' option you don't even have to create a second file.
 
Old 07-22-2011, 10:47 AM   #5
Joe_Louis
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Joe_Louis; 07-22-2011 at 10:51 AM.
 
Old 07-22-2011, 11:28 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 07-22-2011, 11:36 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
 
Old 07-22-2011, 03:11 PM   #8
Joe_Louis
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-22-2011, 04:42 PM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by Joe_Louis View Post
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'
 
Old 07-22-2011, 05:59 PM   #10
Joe_Louis
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-22-2011, 06:23 PM   #11
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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)
 
Old 07-23-2011, 05:05 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
In a bash script, how do I move a line to a new file. wonderfullyrich Programming 18 04-29-2011 11:52 PM
[SOLVED] [bash]difference between script on command line and in file Wim Sturkenboom Programming 4 09-08-2008 02:20 AM
Bash script only appears to process one line of file Nylex Programming 5 08-19-2007 10:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration