LinuxQuestions.org
Review your favorite Linux distribution.
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 03-25-2005, 06:50 AM   #1
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
BASH variables getting unset outside of loop


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.
 
Old 03-25-2005, 07:09 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I am sorry to ask that but why not just use :
Code:
source /path/to/configuration/file
instead of your parsing function ?
 
Old 03-25-2005, 07:14 AM   #3
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Original Poster
Rep: Reputation: 32
Oh...source.

I'd forgotten about that one actually, it works perfectly. Thanks!
 
  


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
how to unset environment variable in bash suneel Linux - Newbie 7 09-14-2009 10:17 AM
Bash - Getting a for loop to process variables with a space in them davee Linux - General 4 10-08-2005 11:21 AM
bash script for loop drisay Programming 5 12-25-2004 12:32 AM
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM
bash for loop problem deadlock Programming 5 09-04-2003 04:32 AM

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

All times are GMT -5. The time now is 10:35 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
Open Source Consulting | Domain Registration