LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-12-2007, 06:07 PM   #1
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Rep: Reputation: 32
Question Can you alter a .conf file with a shell script?


I need to make some changes to a whole bunch of systems and on of the things to do is alter systems files... files such as fstab & iscsi.conf

What I'm simply looking to do is have a shell script take out a uncomment symbol, "#", out of fstab. How can I do that in a shell script?
 
Old 08-12-2007, 06:14 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Using sed


Cheers,
Tink
 
Old 08-12-2007, 08:12 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
For instance, for a file testfile.conf, with the contents:

key1:yes
key2:no
#key3:no


The following command will create a backup file testfile.conf.old, and uncomment the third line:

Code:
sed -i\.old 's/\(#\)\(key3.*\)/\2/' testfile.conf
Rob

Last edited by Robhogg; 08-12-2007 at 08:15 PM. Reason: readability
 
Old 08-14-2007, 08:18 AM   #4
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Robhogg View Post
For instance, for a file testfile.conf, with the contents:

key1:yes
key2:no
#key3:no


The following command will create a backup file testfile.conf.old, and uncomment the third line:

Code:
sed -i\.old 's/\(#\)\(key3.*\)/\2/' testfile.conf
Rob
Ok. That makes more sense! Do I have to include all the text from the certain line I want to uncomment? Also I don't really need a backup so what part of the code do I take out? Please forgive me I've never used sed for anything.
 
Old 08-14-2007, 04:49 PM   #5
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
OK - to break down that command:

sed
#erm, sed
-i
#edit the file in place, rather than having to direct
#output to another file
\.old
#this is the suffix for the backup file. The backslash
#"escapes" the full stop (stops it having its special #meaning)
's/<pattern>/<substituted pattern>/'
#the sed substitute command
\(...\)
#defines a block to be matched
# and key3
#the literal strings
.
#matches any single character
*
#matches the previous character 0 or more times. Since the
#preceding character was . it means "match any number of
#any characters"
\2
#Replay what was matched by the second block - i.e.
#key3 followed by any other characters.
testfile.conf
#The file to be processed

No, you don't have to include all the text from the line to be matched. You just have to specify a pattern that will match that line (and no line that you do not want uncommenting!). So (in this very simple example) #key3.* matches #key3:no, but would also match #key31957:fnord! It's worth having a look at man regex, and googling for help on regular expressions (this form of pattern matching). Once you get your head around them, regexes are one of the most powerful tools for working with strings, and are used with many other linux tools (most notably grep) as well as the majority of programming languages.

And as for backups - if you omit the suffix following -i, one won't be created. I usually feel a lot happier if I back up a config file before changing it, though.

Rob

Last edited by Robhogg; 08-14-2007 at 04:52 PM.
 
Old 08-14-2007, 05:01 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by Robhogg View Post
I usually feel a lot happier if I back up a config file before changing it, though.
*Especially* when "I need to make some changes to a whole bunch of systems ..."

Make the backup(s) - easy enough to clean up later. Better you never need a backup you've got than need one you didn't take.
 
Old 08-14-2007, 05:40 PM   #7
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Original Poster
Rep: Reputation: 32
So for this script to work I need to be in the conf file's specific directory right?

Can I do this then?

sed -i\.old 's/\(#\)\(key3.*\)/\2/' /etc/testfile.conf

Last edited by Thaidog; 08-14-2007 at 05:50 PM.
 
Old 08-14-2007, 05:54 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Of course you can - stick it in a script and pass the name on invocation if it changes on some/all of the sysytems if you like.

Tink was gently trying to nudge you in the direction of reading some doco - start with the man page then have a look at this for a tutorial. The fella that wrote it knows a few things about Linux.
 
Old 08-14-2007, 06:12 PM   #9
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Original Poster
Rep: Reputation: 32
Ok, will do! Thanks everybody for your help!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
Bash script to read file and alter lines depending on create date ChristianHein Linux - General 13 08-04-2007 05:39 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
script on log-out to alter file permissions stuffedcat Linux - Newbie 2 06-12-2006 09:18 AM
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:50 PM.

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