Google Groups Home
Help | Sign in
piping into set /p
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  23 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Liviu  
View profile
 More options Jul 11, 9:48 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: "Liviu" <lab...@gmail.c0m>
Date: Fri, 11 Jul 2008 20:48:51 -0500
Local: Fri, Jul 11 2008 9:48 pm
Subject: piping into set /p
"C:>echo 123 | set /p xyz=" at a cmd prompt doesn't set/change "xyz".

Is there another way to set a variable from piped standard input
(without temp files etc)? Question is related to
http://www.robvanderwoude.com/variableexpansionbug.html, and could have
interesting uses inside a "for" with enabledelayedexpansion, for example

--------
@echo off
echo.
setlocal enabledelayedexpansion
for %%x in ("%%^^&^!") do (
  echo "%%~x"
  (echo "%%~x") | more
)
--------

gives

--------
"%&"
"%^&!"
--------

where the second line could be useful if "captured" into a variable.

Cheers,
Liviu


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
001.dmi...@gmail.com  
View profile
 More options Jul 12, 12:48 am
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: 001.dmi...@gmail.com
Date: Fri, 11 Jul 2008 21:48:05 -0700 (PDT)
Local: Sat, Jul 12 2008 12:48 am
Subject: Re: piping into set /p
On 12 ÉÀÌ, 05:48, "Liviu" <lab...@gmail.c0m> wrote:

> "C:>echo 123 | set /p xyz=" at a cmd prompt doesn't set/change "xyz".

> Is there another way to set a variable from piped standard input
> (without temp files etc)?

No. Only temp files and `for /f`

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Liviu  
View profile
 More options Jul 12, 1:12 am
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: "Liviu" <lab...@gmail.c0m>
Date: Sat, 12 Jul 2008 00:12:39 -0500
Local: Sat, Jul 12 2008 1:12 am
Subject: Re: piping into set /p
<001.dmi...@gmail.com> wrote
On 12 ÉÀÌ, 05:48, "Liviu" <lab...@gmail.c0m> wrote:

>> "C:>echo 123 | set /p xyz=" at a cmd prompt doesn't set/change "xyz".

>> Is there another way to set a variable from piped standard input
>> (without temp files etc)?

> No. Only temp files and `for /f`

I'd take 'for /f ' if only it worked ;-)

----
C:\>echo 123 | set /p xyz=

C:\>set xyz
Environment variable xyz not defined

C:\>echo 123 | for /f "delims=" %x in ('more') do set xyz=%x

C:\>set xyz=123

C:\>set xyz
Environment variable xyz not defined
----

But I assume you meant saving a temp file then for /f'ing its contents.
That would work, indeed. Only I was hoping for an easier/cleaner way.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
t.m.trzec...@gmail.com  
View profile
 More options Jul 12, 4:16 am
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: t.m.trzec...@gmail.com
Date: Sat, 12 Jul 2008 01:16:33 -0700 (PDT)
Local: Sat, Jul 12 2008 4:16 am
Subject: Re: piping into set /p
On Jul 12, 6:12 am, "Liviu" <lab...@gmail.c0m> wrote:

> <001.dmi...@gmail.com> wrote
> On 12 ÉÀÌ, 05:48, "Liviu" <lab...@gmail.c0m> wrote:
> >> "C:>echo 123 | set /p xyz=" at a cmd prompt doesn't set/change "xyz".

> >> Is there another way to set a variable from piped standard input
> >> (without temp files etc)?

> > No. Only temp files and `for /f`

> I'd take 'for /f ' if only it worked ;-)

It does work. But not the way you used it :) Try this:

----setfromstdin.bat
@echo off
setlocal
for /f "delims=" %%x in ('more') do set xyz=%%x
set xyz
----EOF

C:\> echo 123| setfromstdin.bat
xyz=123

To understand what happens you have to know how anonymous pipes are
handled in NT. Both ends of the pipe are launched in separate child
processes. Once all the commands are executed, those processes are
terminated and their environments destroyed, so any environment
changes will not survive (unless you use some utility capable of
setting the environment of the parent process).

Now, on to the supposed bug with delayed expansion that you link to in
your original post. There is no bug, it's the expected behaviour.
Let's look at the example from the link http://www.robvanderwoude.com/variableexpansionbug.html
:

----BOF
@echo off
setlocal enabledelayedexpansion
set myvar=value
(
    echo.!myvar!
) | more
endlocal
----EOF

The reason it doesn't work is because 'echo.!myvar!' is execuded in a
separate 'cmd.exe' and by default delayed expansion is disabled in
command interpreter. So let's enable it and see what happens:

C:\>set xyz=123 & (cmd.exe /V:ON /C echo.!xyz!)|more
123

Now it works!

I hope that clears a few things.

Cheers,

Tomek


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Liviu  
View profile
 More options Jul 12, 12:15 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: "Liviu" <lab...@gmail.c0m>
Date: Sat, 12 Jul 2008 11:15:35 -0500
Local: Sat, Jul 12 2008 12:15 pm
Subject: Re: piping into set /p

<t.m.trzec...@gmail.com> wrote
On Jul 12, 6:12 am, "Liviu" <lab...@gmail.c0m> wrote:

> <001.dmi...@gmail.com> wrote
>> On 12 ÉÀÌ, 05:48, "Liviu" <lab...@gmail.c0m> wrote:
>> >> "C:>echo 123 | set /p xyz=" at a cmd prompt doesn't set/change
>> >> "xyz".

>> >> Is there another way to set a variable from piped standard input
>> >> (without temp files etc)?

>> > No. Only temp files and `for /f`

>> I'd take 'for /f ' if only it worked ;-)

> It does work. But not the way you used it :) Try this:

Thanks, but it still does not work the way I asked ;-)

> ----setfromstdin.bat
> @echo off
> setlocal
> for /f "delims=" %%x in ('more') do set xyz=%%x
> set xyz
> ----EOF

> C:\> echo 123| setfromstdin.bat
> xyz=123

The .bat is not setting the environment in the caller process. In fact
it's not too different from the example I gave with the one-line for /f
at the cmd prompt, where you could see the set xyz=123 being executed,
but then "xyz" was still undefined once control returned to the prompt.
Using your batch does pretty much the same...

----
C:>echo 123 | setfromstdin.bat
xyz=123

C:>set xyz
Environment variable xyz not defined
----

> To understand what happens you have to know how anonymous pipes
> are handled in NT. Both ends of the pipe are launched in separate
> child processes. Once all the commands are executed, those processes
> are terminated and their environments destroyed, so any environment
> changes will not survive (unless you use some utility capable of
> setting the environment of the parent process).

Would you know of such utility usable in batch files?

That said, I believe your explanation is close to what's going on
internally. However, it is not an intrinsic limitation of anonymous
pipes. If anything, it's a careless (mis)use of them by cmd.

> Now, on to the supposed bug with delayed expansion that you link to
> in your original post. There is no bug, it's the expected behaviour.
> [...]
> The reason it doesn't work is because 'echo.!myvar!' is execuded in a
> separate 'cmd.exe' and by default delayed expansion is disabled in
> command interpreter. So let's enable it and see what happens [...]

Yes and no. Yes, I see your point. No, because you could make literally
the same point if 'echo.!myvar!' was not enclosed in parantheses, yet it
works as expected in that case.

Cheers,
Liviu


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
foxidrive  
View profile
 More options Jul 12, 12:40 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: foxidrive <got...@woohoo.invalid>
Date: Sun, 13 Jul 2008 02:40:18 +1000
Local: Sat, Jul 12 2008 12:40 pm
Subject: Re: piping into set /p

On Sat, 12 Jul 2008 11:15:35 -0500, "Liviu" <lab...@gmail.c0m> wrote:
>> are terminated and their environments destroyed, so any environment
>> changes will not survive (unless you use some utility capable of
>> setting the environment of the parent process).

>Would you know of such utility usable in batch files?

What do you want to do, in specific terms?

Nothing to do with malware is it? ;-)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Liviu  
View profile
 More options Jul 12, 12:55 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: "Liviu" <lab...@gmail.c0m>
Date: Sat, 12 Jul 2008 11:55:10 -0500
Local: Sat, Jul 12 2008 12:55 pm
Subject: Re: piping into set /p
"foxidrive" <got...@woohoo.invalid> wrote in message

news:ognh74lb3ohsotsjsck04lve2abqnhmgt8@4ax.com...

> On Sat, 12 Jul 2008 11:15:35 -0500, "Liviu" <lab...@gmail.c0m> wrote:

>>> are terminated and their environments destroyed, so any environment
>>> changes will not survive (unless you use some utility capable of
>>> setting the environment of the parent process).

>>Would you know of such utility usable in batch files?

> What do you want to do, in specific terms?

Fight the "!" demons ;-) Quoting from the top post:  [...] could have
interesting uses inside a "for" with enabledelayedexpansion, for example

--------
@echo off
echo.
setlocal enabledelayedexpansion
for %%x in ("%%^^&^!") do (
  echo "%%~x"
  (echo "%%~x") | more
)
--------

gives

--------
"%&"
"%^&!"
--------

where the second line could be useful if "captured" into a variable.

> Nothing to do with malware is it? ;-)

Not at all, unless you classify enabledelayedexpansion as malware ;-)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
001.dmi...@gmail.com  
View profile
 More options Jul 12, 1:06 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: 001.dmi...@gmail.com
Date: Sat, 12 Jul 2008 10:06:15 -0700 (PDT)
Local: Sat, Jul 12 2008 1:06 pm
Subject: Re: piping into set /p
On 12 ÉÀÌ, 20:55, "Liviu" <lab...@gmail.c0m> wrote:

@echo off

for %%x in (%%^&!) do set "var=%%x" && call:1
goto:eof

:1
setlocal enabledelayedexpansion
echo !var!
endlocal


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
t.m.trzec...@gmail.com  
View profile
 More options Jul 12, 2:57 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: t.m.trzec...@gmail.com
Date: Sat, 12 Jul 2008 11:57:38 -0700 (PDT)
Local: Sat, Jul 12 2008 2:57 pm
Subject: Re: piping into set /p
On Jul 12, 5:15 pm, "Liviu" <lab...@gmail.c0m> wrote:

> <t.m.trzec...@gmail.com> wrote
> > It does work. But not the way you used it :) Try this:

> Thanks, but it still does not work the way I asked ;-)
> The .bat is not setting the environment in the caller process. In fact
> it's not too different from the example I gave with the one-line for /f
> at the cmd prompt, where you could see the set xyz=123 being executed,
> but then "xyz" was still undefined once control returned to the prompt.
> Using your batch does pretty much the same...

Yes, that's correct. What I wanted to show was that you can read from
stdin in your parrent batch and set the evironment accordingly. If
that doesn't work for you then you are left with temp files or some
3rd party utilities.

> > changes will not survive (unless you use some utility capable of
> > setting the environment of the parent process).

> Would you know of such utility usable in batch files?

I think it was 'setenv' but I'm not sure. Just search this group and
you will surely find it.

> That said, I believe your explanation is close to what's going on
> internally. However, it is not an intrinsic limitation of anonymous
> pipes. If anything, it's a careless (mis)use of them by cmd.

It's actually easy to test if this explanation is true. Run
C:\>echo|(echo&ping 1.1.1.1)
and check windows task manager. You will briefly see an extra cmd.exe
process while piped commands are executed. So my explanation wasn't
100% accurate. Only receiving end of the pipe is launched in a
separate process.

If that's a 'careless (mis)use' I don't know but that's how things
work on NT. What can you do about it?

> > Now, on to the supposed bug with delayed expansion that you link to
> > in your original post. There is no bug, it's the expected behaviour.
> > [...]
> > The reason it doesn't work is because 'echo.!myvar!' is execuded in a
> > separate 'cmd.exe' and by default delayed expansion is disabled in
> > command interpreter. So let's enable it and see what happens [...]

> Yes and no. Yes, I see your point. No, because you could make literally
> the same point if 'echo.!myvar!' was not enclosed in parantheses, yet it
> works as expected in that case.

Yes, you are right. I've jumped too far with my conclusions. It is a
bug, indeed. Interestingly enough, if you enable delayed expansion
globally in windows' registry then the following works (even without
restarting of command interpreter!)

C:\>set xyz=123 & (echo.!xyz!)|more
123

Cheers,

Tomek


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Todd Vargo  
View profile
 More options Jul 12, 4:01 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: "Todd Vargo" <tlva...@sbcglobal.netz>
Date: Sat, 12 Jul 2008 16:01:05 -0400
Local: Sat, Jul 12 2008 4:01 pm
Subject: Re: piping into set /p

t.m.trzec...@gmail.com wrote:
> Liviu wrote:

>> Thanks, but it still does not work the way I asked ;-)
>> The .bat is not setting the environment in the caller process. In
>> fact it's not too different from the example I gave with the one-
>> line for /f at the cmd prompt, where you could see the set xyz=123
>> being executed, but then "xyz" was still undefined once control
>> returned to the prompt. Using your batch does pretty much the same...

> Yes, that's correct. What I wanted to show was that you can read from
> stdin in your parrent batch and set the evironment accordingly. If
> that doesn't work for you then you are left with temp files or some
> 3rd party utilities.

What is wrong with using temp files? As long as a batch is written to remove
them, there should be no reason to avoid them. Sure you have to write one or
two extra lines of code but it's not that big a deal.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
foxidrive  
View profile
 More options Jul 12, 4:03 pm
Newsgroups: alt.msdos.batch.nt, microsoft.public.win2000.cmdprompt.admin
From: foxidrive <got...@woohoo.invalid>
Date: Sun, 13 Jul 2008 06:03:15 +1000
Local: Sat, Jul 12 2008 4:03 pm
Subject: Re: piping into set /p

On Sat, 12 Jul 2008 11:55:10 -0500, "Liviu" <lab...@gmail.c0m> wrote:
>"foxidrive" <got...@woohoo.invalid> wrote in message
>news:ognh74lb3ohsotsjsck04lve2abqnhmgt8@4ax.com...
>> On Sat, 12 Jul 2008 11:15:35 -0500, "Liviu" <lab...@gmail.c0m> wrote:

>>>> are terminated and their environments destroyed, so any environment
>>>> changes will not survive (unless you use some utility capable of
>>>> setting the environment of the parent process).

>>>Would you know of such utility usable in batch files?

>> What do you want to do, in specific terms?
>Fight the "!" demons ;-) Quoting from the top post:

Please explain specifically what you need to do and under what
circumstances.