Re: Merging Bash sources

2009-11-11 Thread Simon Wistow
On Wed, Nov 11, 2009 at 01:19:30AM +, James Laver said:
 Perhaps I'm overlooking something obvious, but why don't you just parse twice?

Because I don't know where the config file is until I parse the command 
line options.

I could

getArgs
. ${config_file}
getArgs

but I think Bash getopts is destructive.
 


Merging Bash sources

2009-11-10 Thread Simon Wistow
I have a small problem in that I'm trying to modify a bash script so 
that currently does this

. config
# then inspect command line args
getArgs

so that you can specify the config file on the command line. Which 
necessarily requires do

config_file=config
# inspect command line args first
getArgs
. ${config_file}

however that means that anything in the config file will override 
anything passed in on the command line. Which sort of defeats the point. 

Is there an easy way to say source this config file but don't override 
any variable already set? or some sort of standard recipe? Or amy I 
going to have to write something that reads the config file line by 
line, splits out any variable name left of a '=' checks to see if it's 
set and then evals the line? Cos that's potentially prone to failure.




Re: Merging Bash sources

2009-11-10 Thread Simon Wistow
On Wed, Nov 11, 2009 at 01:03:08AM +, me said:
 Is there an easy way to say source this config file but don't override 
 any variable already set? or some sort of standard recipe? Or amy I 
 going to have to write something that reads the config file line by 
 line, splits out any variable name left of a '=' checks to see if it's 
 set and then evals the line? Cos that's potentially prone to failure.

Something like this in fact:

% cat try.sh

#!/usr/local/bin/bash

function loadConfig {
for line in `cat $1`;
do
name=`echo $line | cut -d '=' -f 1`
if [[ -z `echo $line | grep '='` || -z $name ]]; 
then
continue;
fi
value=`eval echo \\$${name}`
if [[ -z ${value} ]];
then
eval ${line}
fi
done
}

config=try.conf
echo Config is: 
cat ${config}
echo 
echo 

foo=a
loadConfig ${config}

echo Variables are now:;
echo foo=${foo}
echo bar=${bar}


% ./try.sh
Config is: 
foo=quirka
# foo
bar=fleeg


Variables are now:
foo=a
bar=fleeg



Re: Merging Bash sources

2009-11-10 Thread James Laver
On Wed, Nov 11, 2009 at 1:03 AM, Simon Wistow si...@thegestalt.org wrote:
 I have a small problem in that I'm trying to modify a bash script so
 that currently does this

    . config
    # then inspect command line args
    getArgs

 so that you can specify the config file on the command line. Which
 necessarily requires do

    config_file=config
    # inspect command line args first
    getArgs
    . ${config_file}

Perhaps I'm overlooking something obvious, but why don't you just parse twice?

Failing that, you could opt to use environment variables for extra
config files, so you'd call it like

MYSCRIPT_CONFIG_FILE=/path/to/config ./myscript.sh

That way you eliminate the first pass of the arguments.

--James



Re: Merging Bash sources

2009-11-10 Thread Ash Berlin


On 11 Nov 2009, at 01:03, Simon Wistow wrote:


I have a small problem in that I'm trying to modify a bash script so
that currently does this

   . config
   # then inspect command line args
   getArgs

so that you can specify the config file on the command line. Which
necessarily requires do

   config_file=config
   # inspect command line args first
   getArgs
   . ${config_file}

however that means that anything in the config file will override
anything passed in on the command line. Which sort of defeats the  
point.


Is there an easy way to say source this config file but don't  
override

any variable already set? or some sort of standard recipe? Or amy I
going to have to write something that reads the config file line by
line, splits out any variable name left of a '=' checks to see if it's
set and then evals the line? Cos that's potentially prone to failure.


Bash does have some fairly complex substitution possibilities[0] such  
as:


${foo:-bar} # If $foo exists and is not null, return $foo. If it  
doesn't exist or is null, return bar.

${foo:=bar} # as above, but set foo=bar as well as returning

But I don't think any thse are ideal for your purposes, and you  
probably don't want to require who ever is writing the config file to  
have to do anything but foo=bar


So either re-process the args after you've loaded the config file, or  
use different internal variables for cmd line flags and merge  
afterwards.


-ash

[0] http://linux.die.net/abs-guide/parameter-substitution.html