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
|