[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: .ini files in bash?



On Tue, Nov 30, 2004 at 09:16:07AM +0200, David Baron wrote:
> Worth a try. Maybe only saving the few I really need will work. As I said,
> the source line accesses the file but then the script aborts.

I was going to write back about this but I thought there were too many
suggestions already!  Well here are four more...

You didn't mention how "the script aborts", i.e. what error message you get.

When I do it, I get error messages like this:

alice$ set > myvars
alice$ source ./myvars 
bash: BASH_VERSINFO: readonly variable
bash: EUID: readonly variable
bash: PPID: readonly variable
bash: SHELLOPTS: readonly variable
bash: UID: readonly variable

This readonly variables can't be restored as they're not in fact "variables" at
all.  The shell is still loading all the other variables you saved though.


here are some ways to avoid these messages:

1. just ignore the messages:
  source ./myvars 2>/dev/null

2. don't save those read-only variables:
  set | grep -v -e ^BASH_VERSINFO= -e ^EUID= -e ^PPID= -e ^SHELLOPTS= -e^UID= >myvars

3. only save some variables:
  set | grep -e ^MYVAR1= -e ^MYVAR2= >myvars

4. or if you get sick of typing -e ^FOO= :

   savevars() {
   	local grepargs=""
   	for varname; do grepargs="$grepargs -e ^$varname="; done
   	set | grep $grepargs
   }
   
   savevars MYVAR1 MYVAR2 >myvars


I would recommend to stick with "set" rather than "export" or "sed" for saving
the variables, anyway.



Sam



Reply to: