LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   modify content in a file using shell script (https://www.linuxquestions.org/questions/linux-newbie-8/modify-content-in-a-file-using-shell-script-470329/)

fjkum 08-03-2006 02:11 AM

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.

konsolebox 08-03-2006 02:46 AM

can you show us a line in /etc/sysconfig/syslog that you want "-r" to add to?

timmeke 08-03-2006 04:46 AM

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.


All times are GMT -5. The time now is 12:14 PM.