Hi everyone. I'm currently working on a BASH script, which has to take some default parameters from a configuration file. I'm parsing this line by line in a while loop, and setting variables inside said loop. The trouble is, those variables seem to get unset on leaving the loop. Am I just missing something really obvious here?
The relevant function is as follows:
Code:
function parse_config {
# This reads the file one line at a time, and discards any whitespace at the start of the line, and any comments anywhere in the line
# So comment lines and blank lines are filtered out here
cat $configfile | sed -e 's/#.*//;/^\s*$/d' "$@" | while read line; do
option=`echo "$line" | cut -d"=" -f1`
value=`echo "$line" | cut -d"=" -f2`
case $option in
database_directory) dbdir=$value
;;
postgres_bin) pgbin=$value
;;
temp_directory) tmpdir=$value
;;
db_backup_directory) dbbkdir=$value
;;
restore_config_files)
if [ "$value" != "y" ] && [ "$value" != "n" ]; then
echo "Error while parsing configuration file:"
echo "restore_config_files was set to $value"
echo "Permitted values are y or n"
exit
fi
restoreconf=$value
;;
wal_archive_directory) walarchivedir=$value
;;
current_wal_directory) currentwaldir=$value
;;
esac
# dbdir is still set here
echo "Database dir. is $dbdir"
done
# But it isn't set here (and neither are the others)
echo "Configuration information"
echo "---------------------------------------"
echo "Database directory: $dbdir"
echo "PostgreSQL binaries directory: $pgbin"
echo "Temporary directory: $tmpdir"
echo "Database backup directory: $dbbkdir"
echo "Restore configuration from backup: $restoreconf"
echo "WAL archive directory: $walarchivedir"
echo "Current WAL is in: $currentwaldir"
}
The configuration file I'm using looks like this:
Code:
database_directory=/cygdrive/c/postgres
postgres_bin=/cygdrive/c/postgresql/8.0/bin
temp_directory=/tmp
db_backup_directory=/cygdrive/c/pgbackup/main
restore_config_files=y
wal_archive_directory=/cygdrive/c/pgbackup/wal
current_wal_directory=/cygdrive/c/pgbackup/current
You may have noticed from some of those paths that I'm actually using Cygwin, not a real Linux system, but unfortunately I don't have much choice about that (I'm at work

). I doubt that it's really relevant.