Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-23-2005, 12:39 PM   #1
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

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


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
# errorlog: what file will contain the logs
# backuplist: the file that will contain the directories to be backupped

readfile script (
# read a file line by line



#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" ]
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" != "#" ]
echo $line

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.

Old 09-23-2005, 12:43 PM   #2
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, 12:58 PM   #3
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.


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

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