Re: execline: use of define, export, unexport, import

From: Colin Booth <cathexis_at_gmail.com>
Date: Mon, 25 Aug 2014 22:04:47 -0700

On Mon, Aug 25, 2014 at 7:59 PM, John Vogel <jvogel4_at_stny.rr.com> wrote:
> If I define a variable to some value at the top level of an execline script,
> there seems no way to redefine it.
>
> Stripped down example:
>
> #!/command/execlineb
> define var 0
> foreground { echo $var }
> define var 1
> foreground { echo $var }
> export var 2
> import var
> foreground { echo $var }
> unexport var
> export var 3
> import var
> echo $var
> # end of script
>
> All output is "0" from the first define. If I change the first define, then
> all output will be what ever I set that to.

Define is weird (I've just been playing around with it recently, so someone
should correct me if I'm wrong, please). As Patrick said, define overwrites
all instances of a key with a value. So in the case of youre script you've
overwritten var with 0 everywhere you go.

For example the following script displays 0 then 1 then 0 again:
#!/command/execlineb
define var "0"
foreground { echo $var }
define 0 "1"
foreground { echo $0 }
echo $var

> Am I missing something basic here? If no state is maintained, should the
> definition persist? The perplexing part is that I would have thought that the
> lines that unexported, exported, then imported would force the redefinition.
(un)export is for setting and unsetting environment variables, not injecting
variables into your script. Once you've exported a variable into the environment
you should import it to be used:

#!/command/execlineb
emptyenv
export var "some text"
foreground { env }
foreground { echo $var }
import var
echo $var

Note that import appears to have the same no-clobber powers as define though
export doesn't:
#!/command/execlineb
emptyenv
export var "some text"
export var "other text"
import var
echo $var

outputs "other text" whereas:

#!/command/execlineb
emptyenv
export var "some text"
import var
export var "other text"
import var
echo $var

outputs "some text"

Presumably this is similar to the define issue above. I can't find a good way
to override the imported variable by name. The closest I've found is to use
`importas var2 var ; echo $var2' to pull in the (current) value of var
to the local variable
var2, and then echo that.

>
> I will work on redesigning the script I was trying to redefine a variable in.
> Maybe I am making it too complex.
>
> --
> John

Cheers!

-- 
"If the doors of perception were cleansed every thing would appear to
man as it is, infinite. For man has closed himself up, till he sees
all things thru' narrow chinks of his cavern."
  --  William Blake
Received on Tue Aug 26 2014 - 05:04:47 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:38:49 UTC