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 09-23-2005, 11:39 AM   #1
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Rep: Reputation: 18
bash: read lines from a configuration script


Hi,

I'm playing around with bash scripting to get some kind of a backup script operational.
The script has to read some parameters from a configuration file, ofcourse not taking into account the commented lines.
I came up with the following:

config file (backup.conf)
------------------------------
# configuration backupscript
# I have noticed that putting the backupscript in a directory that has to be backupped causes the backup to fail.
#
# backupdir: where the backupped files will be saved
backupdir=/mnt/hdb/bu
#
# errorlog: what file will contain the logs
errolog=/mnt/hdb/bu/error.log
#
# backuplist: the file that will contain the directories to be backupped
backuplist=/mnt/hdb/backupscripts/backup.list
#


readfile script (test.sh)
-----------------------------
#!/bin/bash
# read a file line by line

file=/mnt/hdb/backupscripts/backup.conf

x=0

#wc returns for some reason also the name of the file, I only need the amount of lines:
lns=`wc -l $file`
y=`expr "$lns" : '\([0-9]*\)'`

#read the file, line by line using a head-tail trick:
while [ "$x" -lt "$y" ]
do
let x=x+1
#read the line
line=`head -n $x $file | tail -n 1`
#remove all whitespaces because otherwise, expr substr will complain
#(because it sees each word in $line as a different variable I think)
word=`echo $line | sed -e 's/ //g'`
#finally get the first character
char=`expr substr $word 1 1`
#print the line if it doesn't start with a '#'
if [ "$char" != "#" ]
then
echo $line
fi
done

exit 0


This works fine for now but it seems rather complicated for such a simple task. Can anyone point me to a more elegant solution?
How can I make 'expr substr $var' think that the $var is only one variable, even it it contains whitespaces?

btw; I made the script while looking in tutorials etc. So my knowledge regarding regular expressions is very low.

thanks,
Lieven
 
Old 09-23-2005, 11:43 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
If you trust the configuration file (it is writable only by root), then you can just use the following one-liner in your script:

. /etc/backup.conf

The leading period and space will cause the contents of the file to be included in the current shell environment.
 
Old 09-23-2005, 11:58 AM   #3
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
So if I just do . /etc/backup.conf then that file will be included as a part of the script and everything in it will be considered a part of the script.
Because the config file is indeed only writable by root, I think I will do it like that.
Thanks for the reply.
 
  


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 program Read a configuration File minil Programming 10 01-17-2005 04:37 AM
Help with BASH script PLEASE READ!!! hroman Programming 7 10-08-2004 07:39 PM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM
Why can't I read in data from the serial port using a bash script? tjt Linux - Newbie 1 06-17-2004 12:21 AM
problems with bash script read wedgeworth Programming 3 02-16-2004 08:29 AM

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

All times are GMT -5. The time now is 06:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration