Can't set an env variable via pipe

Jun 19, 2008
2
0
#1
If I write a BTM file that does this:
ECHO aaa | ECHO %@line[con,0]
it echos aaa. OK.

If I write a BTM file that does this
SET XXX=aaa
it sets the environment variable XXX as aaa. OK.

If I write a BTM file that does this:
ECHO aaa | SET XXX=%@line[con,0]
it doesn't set the variable xxx.

Why?

I want to set an env variable from the output of a program. In my test I simulated the program with ECHO aaa

Nick
 

rconn

Administrator
Staff member
May 14, 2008
10,533
94
#2
> If I write a BTM file that does this:
> ECHO aaa | ECHO %@line[con,0]
> it echos aaa. OK.
>
> If I write a BTM file that does this
> SET XXX=aaa
> it sets the environment variable XXX as aaa. OK.
>
> If I write a BTM file that does this:
> ECHO aaa | SET XXX=%@line[con,0]
> it doesn't set the variable xxx.
>
> Why?
It *does* set the variable -- but it sets it in the child pipe process, so
it's lost when the pipe exits.

If you're using v10, you can use the new "in-process" pipes; if you're using
v9 or earlier you'll have to save & read the value from a temp file.

Rex Conn
JP Software
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,587
46
Albuquerque, NM
prospero.unm.edu
#3
If you're using v10, you can use the new "in-process" pipes; if you're using v9 or earlier you'll have to save & read the value from a temp file.
If you just need the first (only?) line of output, there is also the handy @EXECSTR function. Think of it as a sort of reversed pipe.

Code:
set xxx=%@execstr[echo aaa]
 
#5
On Sun, 15 Feb 2009 08:16:37 -0600, rconn <> wrote:


>---Quote---
>> If I write a BTM file that does this:
>> ECHO aaa | SET XXX=%@line[con,0]
>> it doesn't set the variable xxx.
>>
>> Why?
>---End Quote---
>It *does* set the variable -- but it sets it in the child pipe process, so
>it's lost when the pipe exits.
>
>If you're using v10, you can use the new "in-process" pipes; if you're using
>v9 or earlier you'll have to save & read the value from a temp file.
Or use

SET XXX=%@EXECSTR[ECHO aaa]
--
- Vince
 
#6
On Sun, 15 Feb 2009 08:16:37 -0600, rconn <> wrote:


>---Quote---
>> If I write a BTM file that does this:
>> ECHO aaa | SET XXX=%@line[con,0]
>> it doesn't set the variable xxx.
>>
>> Why?
>---End Quote---
>It *does* set the variable -- but it sets it in the child pipe process, so
>it's lost when the pipe exits.
>
>If you're using v10, you can use the new "in-process" pipes; if you're using
>v9 or earlier you'll have to save & read the value from a temp file.
Or use

SET XXX=%@EXECSTR[ECHO aaa]
--
- Vince
 
#7
Nick Tatham wrote:
| Rex: Thanks for the explanation. I have indeed used a temporary file
| as a work around.
|
| Charles: EXCSTR looks like a good solution. I had forgotten about
| that - thanks.

It's @EXECSTR, and in V10 it is enhanced to allow selecting any line of the
output, not just the first.
--
HTH, Steve
 
#8
Nick Tatham wrote:
| Rex: Thanks for the explanation. I have indeed used a temporary file
| as a work around.
|
| Charles: EXCSTR looks like a good solution. I had forgotten about
| that - thanks.

It's @EXECSTR, and in V10 it is enhanced to allow selecting any line of the
output, not just the first.
--
HTH, Steve
 
#9
Nick Tatham wrote:
| Rex: Thanks for the explanation. I have indeed used a temporary file
| as a work around.
|
| Charles: EXCSTR looks like a good solution. I had forgotten about
| that - thanks.

It's @EXECSTR, and in V10 it is enhanced to allow selecting any line of the
output, not just the first.
--
HTH, Steve