LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-03-2006, 02:11 AM   #1
fjkum
Member
 
Registered: Feb 2006
Posts: 40

Rep: Reputation: 15
modify content in a file using shell script


Hi,

I would like to add "-r" option into /etc/sysconfig/syslog file using bash shell script. How can I go about doing that?

First I would like to check that file exists first before proceeding, 'cuz I want the script to be portable with other linux flavor.

TIA.
 
Old 08-03-2006, 02:46 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
can you show us a line in /etc/sysconfig/syslog that you want "-r" to add to?
 
Old 08-03-2006, 04:46 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
There are only 2 lines in my /etc/sysconfig/syslog file that aren't commented out.

To check if a file exists, try something like (in Bash):
Code:
file='/etc/sysconfig/syslog';
if [[ -f $file ]]; then
   #file exists and is a regular file (ie no directory, symlink or whatever).
   #you may want to check if it's writable too by using a similar if [[ -w $file ]]; test.
else
   #Add error message here to indicate that file doesn't exist.
   #Something like:
   echo "File $file does not exist on this system. No changes made."
   #Exit with non-zero exit value to indicate an error.
   exit 1;
fi;
Check out "man bash" for details on -f, -w and similar tests.

To append "-r" to a specific line, you can use sed or awk.
Or source the syslog file in Bash to get the variables $SYSLOGD_OPTIONS directly, modify them and then write them back into the file, like so:
Code:
source /etc/sysconfig/syslog
#Now you have the $SYSLOGD_OPTIONS variable, just append -r to it's value:
SYSLOGD_OPTIONS="$SYSLOGD_OPTIONS -r"
#similar for KLOGD_OPTIONS.
#Now write back both variables:
echo "SYSLOGD_OPTIONS=\"$SYSLOGD_OPTIONS\"" >> some_file
mv some_file /etc/sysconfig/syslog
There may be better ways for writing the values back (this method will disregard any lines in the config that you didn't write back explicitly), but hey, this is just an example.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script: substitute a file's content according to a map? Chowroc Programming 11 11-15-2005 04:08 AM
use file content as tcl script parameters powah Linux - General 3 07-16-2005 11:10 AM
how to modify a text file or a string in shell programming luckyvietman Programming 5 07-12-2005 05:08 PM
Modify content of .deb file njes Debian 2 05-21-2005 09:11 AM
How to manually modify the content of the K menu in KDE? pujolasdf Linux - Software 2 03-27-2005 11:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:08 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