Controlling scope of bash shell vars

Controlling scope of bash shell vars

Post by pt » Mon, 10 Jun 2002 01:27:41



        I have some bash scripts, say A, B, and C.  Each use a bunch of shell
variables, and each script defines the vars to the same value.
Obviously a maintenance headache.
        I would like to define another script that is called by A, B, or C to set
all the variables and make them available to the calling script.
Problem is, I don't want to pollute my normal shell variable namespace
with all these variables .... they're really just needed within the
context of these scripts, and are not required to be persistent between
subsequent runs of A, B, or C.
        Maybe "export" them from the var-set-script, then undefine them afterwards???
        Any clues would be appreciated.
 
 
 

Controlling scope of bash shell vars

Post by Bit Twist » Mon, 10 Jun 2002 01:48:36



>    I have some bash scripts, say A, B, and C.  Each use a bunch of shell
> variables, and each script defines the vars to the same value.
> Obviously a maintenance headache.
>    I would like to define another script that is called by A, B, or C to set
> all the variables and make them available to the calling script.
> Problem is, I don't want to pollute my normal shell variable namespace
> with all these variables .... they're really just needed within the
> context of these scripts, and are not required to be persistent between
> subsequent runs of A, B, or C.
>    Maybe "export" them from the var-set-script, then undefine them afterwards???

Sure, put the export SOMETHINE=stuff  in a var file,
source your var file. Example:

.----------- gotta have the period space
|
v
. /some/dir/var.file
(your commands and whatnot)

 
 
 

Controlling scope of bash shell vars

Post by Floyd Davidso » Mon, 10 Jun 2002 02:02:59



>I have some bash scripts, say A, B, and C.  Each use a bunch of
>shell variables, and each script defines the vars to the same
>value.  Obviously a maintenance headache.

>I would like to define another script that is called by A, B,
>or C to set all the variables and make them available to the
>calling script.  Problem is, I don't want to pollute my normal
>shell variable namespace with all these variables .... they're
>really just needed within the context of these scripts, and are
>not required to be persistent between subsequent runs of A, B,
>or C.

>Maybe "export" them from the var-set-script, then undefine them
>afterwards???  Any clues would be appreciated.

Write a one more file.  In that file you can define all
variables which are common to this set of scripts.  Each script
the sources this fourth file.

Say this forth file is named foo.config, A, B and C should do
this

   #!/bin/bash
   #
   . foo.config    # source configuration file

   ...

This way all of your variables can be modified at once by
editing that one file.  But they are not defined for any scripts
which do not source that file.

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

Controlling scope of bash shell vars

Post by p » Mon, 10 Jun 2002 23:32:21




[snip]
>    Maybe "export" them from the var-set-script, then undefine them afterwards???

> Sure, put the export SOMETHINE=stuff  in a var file,
> source your var file. Example:

> .----------- gotta have the period space
> |
> v
> . /some/dir/var.file
> (your commands and whatnot)

Ah, as they say, Ha!!!  That be the secret sauce.  My trusty O'Reilly
book is a bit shy on "source".  Guess I better go read the man pages.
perl -e 'print "thanks "x(2**1024)'
 
 
 

Controlling scope of bash shell vars

Post by p » Mon, 10 Jun 2002 23:41:12


[snip]

        I also noticed your reply's headers ... Are you *REALLY* getting a
*REAL*  newsfeed from ma bell?  Do they do that?  Are you running DSL?
 WOuld you recommend them?  Thanks again.
        Appologies in advance about the off-topic.

 
 
 

Controlling scope of bash shell vars

Post by Bit Twist » Tue, 11 Jun 2002 00:16:09



Quote:>    I also noticed your reply's headers ... Are you *REALLY* getting a
> *REAL*  newsfeed from ma bell?

No and yes.
No, wc -l .newsrc
  34,985 .newsrc
as you can see I can have access to about 1/3 of the news groups.

Yes ATTBI is AT&T Broadband

Quote:>  Do they do that?  Are you running DSL?

No, too far from the CO, stuck on Digital cable with a throttled headend.

Quote:>  WOuld you recommend them?

Romor mill had indicated they were going sell out of the ISP cable
business. They have been jacking up prices pretty recently.
I have a bad taste im my mouth about them. Cut my download throuput
by 2/3'rds and plan to sell it back to me at new and improved prices.

What a joke, I cannot get "UP TO " the speed posted for what they sell me now.

Quote:>    Appologies in advance about the off-topic.

 
 
 

1. shell var scope

HI,
I run into the variable scope problem on shell.

I want to increment the var between files.
But it seems var has its own scope.

any workaround?

thanks,
Paul.

Below is the code:

FILES="file1 file2"
hc=$1
for file in $FILES
do
   while read line
   do
      echo $hc
      hc=`expr $hc + 1`
   done < $file
done

% ./run 100
100 ====>
101
102
103
104
105
106
107
108
109
100  ===> I want it to be 110
101
102
103
104
105
106
107
108
109

2. DNS Bind don't resolve local names

3. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

4. Anyone got natd working? - - Second part

5. bash : shell var, quoting multiple cmds, and 'su'

6. What is S3?

7. get control on bg job of other bash shell

8. help: Apache can't find cgi scripts

9. su and "bash: no job control in this shell"

10. sockaddr var. and scope problem !!!

11. zsh: How to control variable scope in sourced files?

12. scope of variables assigned by read builtin command in bash

13. BASH : variable scope issue