Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-03-2006, 02:11 AM   #1
Registered: Feb 2006
Posts: 38

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


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.

Old 08-03-2006, 02:46 AM   #2
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
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):
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.
   #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;
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:
source /etc/sysconfig/syslog
#Now you have the $SYSLOGD_OPTIONS variable, just append -r to it's value:
#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.


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
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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:20 PM.

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